Next.js Discord

Discord Forum

Can you revalidate a Route Handler from the full route cache on demand?

Answered
Irish Red and White Setter posted this in #help-forum
Open in Discord
Irish Red and White SetterOP
Assume a simple route handler such as:

/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?

3 Replies

Answer
Irish Red and White SetterOP
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 unstable_cache, see example in marked solution.