Next.js Discord

Discord Forum

Is revalidating the unstable_cache at a time interval with an empty tag array okay?

Answered
Order posted this in #help-forum
Open in Discord
I'm doing a simple test project and I've decided to use the unstable_cache function so that my db doesn't get overloaded with calls when browsing all events. That's obviously a hypothetical given the fact that the website's not a real project but I still wanted to use unstable cache to wrap my prisma ORM queries, however I ran into the problem that the unstable_cache function expects a tag array and I can't even get to the revalidate option without at least inputing an empty array like so:

export const getEvents = unstable_cache( async (city: string, page = 1) => { if (page === 0) redirect("/"); const events = await prisma.eventoEvent.findMany({ where: { city: city === "all" ? undefined : { equals: city, mode: "insensitive", }, }, orderBy: { date: "asc", }, take: 6, skip: (page - 1) * 6, }); let totalCount; if (city === "all") { totalCount = await prisma.eventoEvent.count(); } else { totalCount = await prisma.eventoEvent.count({ where: { city: { equals: city, mode: "insensitive", }, }, }); } if (events.length === 0) { return notFound(); } return { events, totalCount }; }, [], { revalidate: 3600 } );

Is this okay to do?
Answered by joulev
Yes look good to me. Btw that’s not the tag array, but the “key parts” array which is usually not relevant so you can simply keep it empty
View full answer

5 Replies

bump
Answer
ah I see, as far as best practices go, is my way of doing things proper?
Hmm I would not use redirect() and notFound() inside unstable_cache because they throw errors. Instead I would return a value then based on that value use redirect() or notFound() outside the function call
great point, thank you