All articles

Next.js 16 cacheTag keeps 128 tags: put the ones you revalidate first

In Next.js 16, every cacheTag() call inside a "use cache" function accepts at most 128 tags of up to 256 characters each. Past the limit it logs a warning and drops the rest of the list, so whatever you pass last is what goes. If you forward a CMS's cache tags alongside your own, put your own (the entity, its list tags, the path) first and the CMS's tags after them, or the tags you actually revalidate by can be the ones that fall off.

Where 128 tags stops being a lot

I hit this while moving a decoupled Drupal front end to the Next.js 16 App Router with cacheComponents enabled. Each page's data function is a "use cache" function. It tags its entry with a handful of tags of its own, such as the node's id, list tags for the content types and vocabularies the page prints, and tags for the path, and it also passes through the cache tags Drupal reports for the page, so that a save in Drupal can revalidate exactly the pages that show the saved entity.

Drupal is generous with cache tags. A page gets one for every entity it renders: each referenced node, media item, taxonomy term, menu, block and config object. A simple article page carries a few dozen. A page that lists people, events, documents and related content can carry well over a hundred, and nothing on the Drupal side warns you, because Drupal itself has no such limit.

So the original call looked reasonable:

cacheTag(...drupalTags, `node:${node.id}`, ...listTags, ...pathTags);

On most pages it worked. On the busiest ones the last tags in that list never reached the cache entry.

What Next.js does past the limit

The check lives in the tag validation that fetch, unstable_cache and cacheTag() share. In 16.4.5 it walks the list in order: anything that isn't a string, or is longer than 256 characters, is rejected, and once the count of valid tags passes 128 it prints a warning along the lines of "exceeded max tag count for cacheTag(), dropped tags:" followed by the remainder, and stops. There is no error and the page renders normally.

Two details matter here:

  • Order decides what survives. The cut is positional. It doesn't prefer short tags or unique tags; it keeps the front of the array.
  • The limit is applied per call. Several cacheTag() calls in one function add to the same entry's tag list, and each call is checked on its own. That is how the source reads today; I wouldn't build on it, because a custom cache handler, a CDN tag header or a later Next.js release can apply its own limit to the whole entry. Ordering by priority holds up either way.

Why the dropped tags were the important ones

The tags at the end of the old call were exactly the ones the front end relies on most. The node's own tag is what a save of that node revalidates. The list tags are what make a page showing "latest news" refresh when a new article is published, since a brand new article cannot already be among the page's Drupal tags. The path tags are what an on-demand revalidation by URL uses.

Drupal's per-entity tags, on the other hand, are mostly a long tail: the fortieth referenced person on a directory page is worth tagging, but losing it costs less than losing the page's own tag.

This only became serious when the cache lifetimes changed. While tags from Drupal weren't reaching the front end yet, every entry lived for a few minutes, so a missing tag meant a few minutes of stale content. Once tag invalidation was in place, content entries moved to long lifetimes measured in days. At that point a dropped tag means a page that ignores edits until its timer runs out.

How to order tags so the important ones survive

The fix is to build the list by priority, deduplicate it, and pass it in one call. A small helper keeps that consistent across routes:

const MAX_TAGS = 128;

export function buildTags(groups: (string[] | undefined)[]): string[] {
  const seen = new Set<string>();
  for (const group of groups) {
    for (const raw of group ?? []) {
      const tag = raw.trim();
      if (tag && tag.length <= 256) seen.add(tag);
    }
  }
  const tags = [...seen];
  if (tags.length > MAX_TAGS) {
    console.warn(`cache tags over budget: ${tags.length}, keeping ${MAX_TAGS}`);
  }
  return tags.slice(0, MAX_TAGS);
}

And in the data function, highest priority first:

async function getPageData(path: string, drupalTags: string[]) {
  "use cache";
  cacheLife(CONTENT_CACHE_LIFE);

  const node = await loadNode(path);
  cacheTag(
    ...buildTags([
      [`node:${node.id}`],     // a save of this node
      LIST_TAGS[node.type],    // new items in the lists it shows
      pathTags(path),          // revalidation by URL
      drupalTags,              // everything else it renders
    ])
  );
  return node;
}

A Set keeps insertion order, so a tag that appears both in your group and in Drupal's keeps its early position. Trimming to the limit yourself also means your warning names the page, which is easier to act on than the framework's warning in a busy server log.

Tagging the "not found" answer

The same thinking applies to cached misses. When a path doesn't resolve to a published page, the data function still caches its answer, so a crawler requesting dead URLs doesn't hit the CMS each time. That entry needs the path's own tags, and only those, because publishing a page at that path is the event that should clear it. Calling cacheTag() in the code path that returns "not found" keeps the miss small and makes its invalidation explicit, rather than relying on tags that were added before the code knew which answer it would give.

Tags don't cover everything a timer covers

Long lifetimes only make sense where every change arrives as a tag. A page that lists upcoming events changes when the clock passes an event's start time, and no entity save happens then. Those pages keep a short cacheLife profile alongside their tags, while plain content pages can live for days. I'd decide this per page type, not globally: a homepage or department page that embeds an "upcoming" list belongs with the short timers even though most of its content is tag-driven.

Key takeaways

  • Next.js 16 keeps roughly the first 128 valid tags passed to a cacheTag() call and drops the rest with only a console warning.
  • Put the tags you revalidate by, such as the entity's own tag, list tags and path tags, ahead of the cache tags a CMS forwards.
  • Deduplicate and trim the tag list yourself so the warning names the page and the cut is a choice rather than an accident.
  • The cost of a dropped tag grows with the entry's lifetime, so check tag counts on your busiest pages before moving to long cache lifetimes.
  • Keep short timers on pages whose content changes with the date, because the passage of time never sends a cache tag.