Revalidate when not using fetch?
Answered
American posted this in #help-forum
AmericanOP
Hello!
I'm trying to use server action, but without using fetch. Here's a short example:
When using fetch I can tag the fetch request with
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"],
});18 Replies
@American Hello!
I'm trying to use server action, but without using fetch. Here's a short example:
ts
"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?
Its possible but not yet stable yet. Use unstable_cache for stuff you want to cache in the data cache.
It has similar configuration like that of fetch
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");
}@American Thank you 🙠so like this?
ts
export const getSchoolsCache = unstable_cache(getSchools, ["api"]);
export async function createSchool2(formData: FormData) {
await Promise.resolve();
// mutate data
revalidateTag("api");
}
Use the third parameter's revalidate attribute string array for next's tag revalidation
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