Can you revalidate a Route Handler from the full route cache on demand?
Answered
Ratonero Mallorquin posted this in #help-forum
Ratonero MallorquinOP
Assume a simple route handler such as:
This will be cached on build time and never revalidated.
You can make it revalidate with time based revalidation such as:
But how can you clear this response from the full route cache on demand?
I would assume you can call
Is there a way?
/api/test/route.ts:export async function GET() {
const random = Math.random();
console.log(`generated new random value ${random}`);
return Response.json({ random });
}This will be cached on build time and never revalidated.
You can make it revalidate with time based revalidation such as:
export const revalidate: 60; for 60 second duration of cache for example.But how can you clear this response from the full route cache on demand?
I would assume you can call
revalidatePath or revalidateTag with value /api/test but doing this in production mode of NextJS does not clear the cache.Is there a way?
Answered by B33fb0n3
This thread is solved by: https://nextjs-forum.com/post/1286338763319939102#message-1287161938081611787
3 Replies
To keep everyone on the same page: https://nextjs-forum.com/post/1286338763319939102#message-1286663226859454569
This thread is solved by: https://nextjs-forum.com/post/1286338763319939102#message-1287161938081611787
Answer
Ratonero MallorquinOP
In summary for anyone else reading this later:
It is not possible utilizing the full route cache, but you can do the next best thing to move your full response into
It is not possible utilizing the full route cache, but you can do the next best thing to move your full response into
unstable_cache, see example in marked solution.