Next.js Discord

Discord Forum

Revalidate when not using fetch?

Answered
American posted this in #help-forum
Open in Discord
AmericanOP
Hello!

I'm trying to use server action, but without using fetch. Here's a short example:
"use server";
export async function getSchools() {
  await verifyRole("ROOT");
  const schools = await db.query.schools.findMany();
  return schools;
}

export const getSchoolsCache = cache(getSchools);

export async function createSchool2(formData: FormData) {
  await Promise.resolve();
  // mutate data
  // revalidate cache, how?
}


When using fetch I can tag the fetch request with [api] and then do revalidateTag("api");. Is it simply not possible to do without fetch?
Answered by American
Can I just pass undefined then?
export const getSchoolsCache = unstable_cache(getSchools, undefined, {
  tags: ["api"],
});
View full answer

18 Replies

AmericanOP
Thank you 🙏 so like this?
export const getSchoolsCache = unstable_cache(getSchools, ["api"]);

export async function createSchool2(formData: FormData) {
  await Promise.resolve();
  // mutate data
  revalidateTag("api");
}
The second one is for the cache key to differentiate between caches
AmericanOP
Can I just pass undefined then?
export const getSchoolsCache = unstable_cache(getSchools, undefined, {
  tags: ["api"],
});
Answer
If it breaks try empty array lol
Now it lgtm
AmericanOP
It worked, thank you! 🙏
AmericanOP
So, is it not possible to create dynamic tags with unstable_cache? For fetch I can do tags: [user=${userId]], do I need like a factory for unstable_cache?
AmericanOP
Thanks!
AmericanOP
Hey, sorry, do you know why this error happens using your lib?
@American Hey, sorry, do you know why this error happens using your lib?
Can i see the callback, a few lines above?
AmericanOP
@aardani sorry, I figured it out, I didn't have an input param on the action
great, glad you figured it out