Nextjs Fundamentals · Caching & Revalidation

Tagging cached data

cacheTag

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.

  1. cacheTag labels a cached entry so it can be invalidated on demand.
  2. You invalidate a tagged cache entry with revalidateTag or ___.
  3. cacheTag changes what data is stored in the cache entry.
  4. Reusing the same tag in several functions lets one invalidation refresh them all.
  5. A common pattern pairs a long cacheLife like ___ with a cacheTag.
  6. cacheTag is called inside a 'use cache' scope.