Revalidation and cache tags
Tag cached data with cacheTag, then refresh it with updateTag, revalidateTag, revalidatePath, or refresh, and pick the right one after a mutation.
Revalidation and cache tags
The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.
Not started
What you'll learn
- Revalidation updates cached data in one of two ways: time-based with cacheLife, or on demand with a tag or path after something changes.
- cacheTag labels cached output so a single call can invalidate every entry that carries the label, across pages and components.
- updateTag expires a tag immediately for read-your-own-writes and can only be called in a Server Action; the next read waits for fresh data.
- revalidateTag(tag, 'max') marks data stale and serves the stale copy while it refreshes in the background; it works in Server Actions and Route Handlers, and the one-argument form is deprecated.
- Prefer tags over revalidatePath, because a path invalidates everything on a route while a tag invalidates only the data you meant.
The staleness problem
A cache with no way to refresh it is a time bomb. You cached the post list for a day, an editor fixes a typo, and for the next 23 hours everyone sees the typo.
You have two levers. One is a clock: cacheLife lets entries refresh on a schedule. The other is an event: when you know data changed, say so. The event approach is what this lesson is about.
Labelling data with cacheTag
To invalidate on an event, you need a name for the data. cacheTag attaches one or more labels to a cached function or component:
import { cacheLife, cacheTag } from 'next/cache'
export async function getPost(slug: string) {
'use cache'
cacheLife('max')
cacheTag('posts', `post-${slug}`)
return db.post.findFirst({ where: { slug } })
}
Tags are case-sensitive strings of at most 256 characters; a tag over the limit is never attached, so revalidating it does nothing. The same tag on many functions means one call refreshes them all. For content that rarely changes, the docs suggest a long cacheLife like max plus a tag, so the content stays static until something tells Next.js otherwise.
Four ways to invalidate
updateTag(tag) expires the tag at once. The next read waits for fresh data. It only works in Server Actions. Use it when the person who made the change must see it: read-your-own-writes.
'use server'
import { updateTag } from 'next/cache'
import { redirect } from 'next/navigation'
export async function createPost(formData: FormData) {
const post = await db.post.create({ data: { title: String(formData.get('title')) } })
updateTag('posts')
redirect(`/posts/${post.id}`)
}
redirect throws, so code after it never runs. Invalidate before you redirect.
revalidateTag(tag, profile) marks the tag stale with stale-while-revalidate semantics. The next request is served the old content while a refresh runs behind it. It works in Server Actions and Route Handlers, which makes it the one for webhooks.
import { revalidateTag } from 'next/cache'
export async function POST() {
revalidateTag('posts', 'max')
return Response.json({ ok: true })
}
The second argument sets how long stale content may still be served; 'max' is a one-year window, recommended in the docs. { expire: 0 } makes the next request a blocking miss. I called revalidateTag('posts') with one argument on 16.4.6 and next build failed type checking with “Expected 2 arguments, but got 1”. The docs call the one-argument form deprecated; migrate to updateTag in actions or add 'max'. Revalidation is triggered by a request, not by the call, so pages refresh as they are visited.
revalidatePath(path) invalidates all cached data for a route. It is handy when you do not know which tags apply, and the docs say to prefer tags when you can.
refresh() is different in kind. It refetches the current route’s payload in the client router without invalidating any cached data, for state that lives outside the cache, such as a counter your action just changed.
Choosing after a mutation
| Situation | Use |
|---|---|
| User must see their own change now | updateTag in a Server Action |
| Background refresh is fine, or you are in a Route Handler | revalidateTag(tag, 'max') |
| One route is affected and tagging is overkill | revalidatePath |
| The change is outside the cache | refresh() |
The docs add a detail worth knowing: updateTag, revalidatePath, and refresh make Next.js re-render the current route and send the result in the same response as the action, so the page updates in one round trip. revalidateTag with a stale-while-revalidate profile skips that immediate re-render, and the page shows the change on a later read.
What the video covers
Ali Alaa’s 68-minute Cache Components video covers much more than this topic. This lesson starts at the 3256-second mark, where his chapters begin “cacheLife and revalidatePath”, followed by “cacheTag, revalidateTag and updateTag”. Everything before covers the earlier model, first look, partial prerendering, and a product example. A shorter, tag-focused runner-up from a small channel is linked below, based on its description (updateTag, revalidateTag, revalidatePath and combining them).
Where this fits
Your cached data can now stay fresh. The remaining piece of caching is what you must not cache: data that belongs to one request. Next: dynamic APIs: cookies, headers, and searchParams.
Check yourself
A user submits a form and must see their new post immediately. Which function do you call, and where?
updateTag with the tag on the posts data, inside a Server Action, because it expires the entry immediately so the next read waits for fresh data rather than serving the stale copy.
A CMS webhook announces a changed article. Which function fits, and why not updateTag?
revalidateTag with the tag and a profile such as 'max', called in a Route Handler, because updateTag can only run in Server Actions and a background refresh is acceptable for a webhook.
What does the second argument of revalidateTag control?
How long stale content may still be served while the fresh version generates; 'max' gives the longest window, and a request after that window blocks until fresh content is ready.
Why does the docs' advice favor tags over revalidatePath?
A path invalidates all cached data on that route and may over-invalidate, while a tag targets exactly the cached entries that carry it.
Go deeper
Notes
Notes are saved to your account.
Everything else on this page works without one — progress and bookmarks keep themselves in this browser and come with you when you sign in. Notes are the one thing worth not trusting to a browser you might clear.
Your notes could not be loaded.