Confusing cache invalidation with revalidateTag

Is there a way you can 100% reliably clear all cache wherever a tag was used?

for example, I have a route handler.

export async function POST(req: Request) {
	const body = await req.json()
	const post = await db.post.create({
		data: {
			title: body.title,
			content: body.content,
		},
	})

	revalidateTag('posts')

	return NextResponse.json({ success: true, message: 'Created post' }, { status: 200 })
}

I call this route handler in /admin/edit/posts - a route completely seperate to where this data is being used.

I want to invalidate ALL of the cache with anything tagged as posts.

getPosts() is a function that queries the db, gets all of the posts, caches the data with unstable_cache, and tags it as posts. That function is used on /blog and /top-100-posts etc etc (used on multiple pages)

If I call this route handler, but before I do 100 people visit /blog and /top-100-posts (where the data is tagged as posts), they will not get the new updated data until they hard refresh or the client-side router cache is invalid.

Is there a way I can get around this? I want to take advantage of the data cache but I don’t want to serve stale data to a user just because they have visited that route and it’s now cached in the client-router cache.

Is this maybe solved in Next 15?

Hey @bdlowery. Client-side Router Cache is not a forever cache (unless you did something extra to somehow make it that way). But you can also invalidate it in a Server Action or by calling router.refresh. But also note that page segments are opted out by default with Next.js 15.

I’m also going to link a few resource that can help you learn more about the cache options:

I hope that helps!

1 Like

Using a server action invalidates it, but from what I understand that only invalidates it for the user who calls it.

Like if i’m in my custom admin panel in /admin/job/532/edit - and I edit this job post through a form, using a server action wont invalidate the client side router cache for the 100 people who went to /job/my-job-post-532 before I updated it. It’ll still be stale for them.

But also note that page segments are opted out by default with Next.js 15.

Does this mean that only the client router cache is opted out by default now in Next 15?

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.