Invalidate selected data
A cached database or CMS read needs targeted invalidation after related content changes.
Next.js 16+ · Function
cacheTag labels cached data for selective, on-demand invalidation. Use it in a 'use cache' scope, then invalidate its tags with updateTag or revalidateTag.
Call cacheTag inside a 'use cache' scope and pass one or more string labels to associate with the cached result. Later, invalidate matching entries with updateTag when a user mutation must immediately read fresh data, or with revalidateTag when stale-while-revalidate behavior is acceptable.
cacheTag(...tags: string[]): void| Name | Type | Description |
|---|---|---|
tags | string[] | One or more string tags associated with the cached data. |
No return value; it associates the supplied tags with the cached data.
import { cacheTag } from 'next/cache'
export async function getProducts() {
'use cache'
cacheTag('products')
return db.query('SELECT * FROM products')
}This cached read is labeled so a later invalidation call can target the products entry.
A cached database or CMS read needs targeted invalidation after related content changes.
A form or other user mutation should make the next read reflect the write immediately.
A webhook, cron job, or route-triggered refresh can tolerate stale data while revalidation runs.
cacheTag labels cached data for selective, on-demand invalidation. It is used inside a 'use cache' scope and accepts one or more strings.
Follow-up: How would you handle a form mutation? Call updateTag from the Server Function so the next read waits for fresh data.
Use updateTag when the user should immediately see the result of their mutation. Use revalidateTag when serving stale content during background revalidation is acceptable.
Follow-up: What is the key context restriction? updateTag is limited to Server Functions, while revalidateTag can also be used from Route Handlers.
cacheTag is stable in Next.js 16.0.0, replacing unstable_cacheTag without the unstable_ prefix. The current import is from next/cache.
Follow-up: What configuration accompanies it? Enable the cacheComponents flag in next.config.js.
cacheComponentsuse cachecacheLifeupdateTagrevalidateTagServer FunctionsRoute Handlers