👋 Next.js Conf 2024: Come build, party, run, and connect with us! See all events

Optimizing a query function to fetch filtered videos

1 replies
Last updated: Jun 22, 2023
export async function getFilteredVideos( limit,
filterGrade,
filterCategory,
search,
sort,
type = "Lesson Video",
subCategory
) {
console.time("videos fetch");
let query = `*[_type == "video"]{${videoFields}}`;

query += `[type.name == "${type}"]`;

if (filterGrade && filterGrade !== "All Grades") {
query += `[ "${filterGrade}" in grade[].label ]`;
}

if (filterCategory && filterCategory !== "All Categories") {
query += `[ "${filterCategory}" in category[].title ]`;
}

if (subCategory && subCategory !== "All Subtopic") {
query += `[subCategory.title match "${subCategory}"]`;
}

if (search && search !== "All") {
query += `[name match "${search}*"]`;
}

if (sort && sort === "longest") {
query += `| order(duration desc)`;
}

if (sort && sort === "shortest") {
query += `| order(duration asc)`;
}

if (sort && sort === "relevance") {
query += `| order(type.name desc)`;
}

query += `${limit}`;

const results = await client.fetch(query);
console.timeEnd("videos fetch");
return results;
}
anyone who can help my make this query faster
Jun 19, 2023, 1:27 PM
Do not post your question multiple times.
Jun 22, 2023, 4:37 PM

Sanity– build remarkable experiences at scale

Sanity is a modern headless CMS that treats content as data to power your digital business. Free to get started, and pay-as-you-go on all plans.

Was this answer helpful?