Tagging cached data
Time-based revalidation is not always enough. When a user edits a post you want
the cache invalidated now, not when a timer eventually fires. cacheTag labels a
cached entry so you can invalidate it on demand.
Call it inside a 'use cache' scope with one or more string tags:
import { cacheTag } from 'next/cache';
export async function getProducts() {
'use cache';
cacheTag('products');
return db.query('SELECT * FROM products');
}
The tag is just a label — it does not change what is stored. It marks the entry so
that revalidateTag or updateTag can find it later. You can reuse the same tag
across several functions, and invalidating that tag refreshes them all at once.
A common pattern is to pair a long lifetime with a tag, then invalidate from a webhook or a Server Action when the content actually changes, instead of revalidating on a timer:
'use cache';
cacheLife('max');
cacheTag('posts');
Tags integrate across the server and client cache layers, so one tag declaration drives invalidation everywhere.
You are about to answer a short quiz on cacheTag.