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

5 messages · Page 1 of 1 (latest)

manic plank
#

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?

mortal larkBOT
#

🔎 This post has been indexed in our web forum and will be seen by search engines so other users can find it outside Discord

🕵️ Your user profile is private by default and won't be visible to users outside Discord, if you want to be visible in the web forum you can add the "Public Profile" role in id:customize

✅ You can mark a message as the answer for your post with Right click -> Apps -> Mark Solution
(if you don't see the option, try refreshing Discord with Ctrl + R)

crystal steeple
crystal steeple
manic plank
#

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.