Practical Use of Sanity Sync Tags for Granular Next.js Revalidation

verified November 2026
Oleg Proskurin Tech Lead
19 min

TL;DR

  • The sync tags that Sanity attaches to a query response depend only on the shape of the query. They do not depend on the dataset schema, or on the content or existence of documents.

  • The smallest unit of revalidation is a document. A change to any single field emits an event for the whole document.

  • For revalidation tags, field names matter and field types do not. This shows most clearly with slug fields: Sanity recognizes the query by the slug.current path.

  • The exact tags that a document publish emits, measured on a real site.

  • The dangerous case: a query that does not get full tag coverage and leaves pages stale.

Sanity provides sync tags, a mechanism that detects which part of the content changed. Sanity announced them together with the Live Content API on October 30, 2024, after its talk at Next.js Conf (Your content is now Live by default), and the API left beta in March 2025. The rules that Content Lake uses to make the tags are on a separate documentation page, Sync tags, dated October 13, 2026. Together with Next.js revalidation tags, sync tags let you keep a site up to date and update it granularly as content changes are published. General approaches to granular cache invalidation in a headless CMS are in the article Granular Cache Invalidation for Headless CMS.

A correct system for granular and timely revalidation of a Next.js site on a headless CMS stands on three pillars:

  • GROQ queries

  • Sanity sync tags

  • Next.js revalidation tags

The tags you get from Sanity depend on how you write the GROQ query, specifically on which selectors it uses (Sync tags → How Content Lake generates sync tags). A selector is a filter condition on _id, slug.current or _type.

This article is based on hands-on experience and research into how query shapes affect cache tags. It shows how Sanity’s documentation rules behave in a real-world scenario, which I tested on an isolated dataset before changing anything live.

I uncovered these findings while optimizing queries for a production Next.js and Sanity site with 418 cached routes, which handles revalidation via a Sync Tag Invalidate Function. The fixes adjusted the queries, updated one field in the page schema, and removed the “latest articles” list from individual article pages. The table below shows how many of those 418 routes were revalidated by a single publish event before and after the fixes:

What was published Routes revalidated, before Routes revalidated, after Routes that show the document, after
landing page 417 2 1
blog article 416 15 13
blog author 385 2 2
awards section 407 11 11
testimonial 388 5 5
site header 416 414 414

After the fixes, publishing a document revalidates only the routes that display it along with the feeds and indexes that must refresh on every new publish: the sitemap, RSS, and the author archive. An unnecessary revalidation occurs when a route is revalidated for a document it does not display. Across all 454 documents read by at least one route, publishing each document once would produce 174,958 unnecessary revalidations before the fixes and 4,480 after—a 97.4% reduction. Among what remains, helper translation documents stand out: no page displays them directly, yet publishing any of them still revalidates nearly the entire site.

When a page goes stale

Revalidation by sync tags involves two sides: the query response and the event.

When a page fetches data, Content Lake returns a syncTags array alongside the result. next-sanity prefixes each tag with sanity: and passes it to Next.js as a cache tag—via cacheTag when Cache Components are enabled, or via next.tags otherwise (Sync tags → Get the sync tags for a query).

When a document changes, the Live Content API sends an event with the tags of this change. It sends the event for any write to a published document: a publish from Studio, an import, a migration, a script (Sync tags → How sync tags affect API usage). <SanityLive> or a Sync Tag Invalidate Function, one of the Sanity Functions, receives the event, and revalidateTag runs on the server for each tag.

A page goes stale when the tags on its response intersect with the tags on the event. Response tags depend on the query; event tags depend only on the changed document.

sync-tags-mechanismsync-tags-mechanism

Expected problems when revalidating by sync tags:

  • Overly broad revalidation: publishing changes to one document revalidates a disproportionate number of pages.
  • Missed revalidation: the response misses the tag it needs, leaving the page stale.

Four kinds of tags, three levels

The documentation names four kinds of tags: by document _id, by slug value, by document type, and the dataset-wide tag. The first three come from selectors, and a filter without a single selector gets the dataset-wide tag. A slug is usually unique, and Sanity treats it as a document address in the same way as _id, so by selectivity the tags form three levels.

sync-tags-levelssync-tags-levels

Document level: _id or slug. The most granular tags point to a specific document by _id or to a specific slug path (slug.current). Publishing that document revalidates only the routes and feeds whose queries match those tags.

// document tag page-about-en
*[_id == "page-about-en"][0]


// slug tag article-3
*[slug.current == "article-3"][0]

Type level. The tag covers every document of that type: publishing any one of them revalidates all pages that fetch documents of that type.

// type tag page
*[_type == "page"]

Dataset-wide tag (all). Every event carries it, so a page with this tag is revalidated on a publish of any document in the dataset. If every page has it, each publish clears the entire site’s cache—revalidating all pages, not redeploying.

// dataset-wide tag: a filter with no selectors
*[title == "About us"][0]

The narrowest selector wins

If you join conditions with &&, the response gets the tag of the narrowest one only:

// slug tag article-3, no type tag article
*[_type == "article" && slug.current == "article-3"][0]

The documentation sets the order: _id is more precise than slug, and slug is more precise than _type. In the same way, *[_type == "article" && _id in ["article-01","article-02"]] gets only two document tags and no type tag, so you can safely add _type to a lookup by _id or slug. _type still counts as one tag toward the limit; more about the limit below.

The narrowest tag covers a whole document

Even the most precise tag points to a document, not to a field. The event carries the document tag when any field of the document changes, so a page that shows one field of a document is revalidated when any other field changes. Sanity describes a similar rule for a whole query: “a change to any document a superquery touches refetches the entire superquery” (High-performance GROQ → Balance query size and request count).

An example: the navigation on every page shows only the URL of the “About” page. You edit the title of this page, and all routes with the navigation are revalidated, although the URL did not change. On a test site with 8 routes, where all queries are already fixed, a publish of a page outside the navigation revalidates 2 routes, and a publish of a page in the navigation revalidates 7.

The query shape cannot fix this. The only options are to change the schema (move the URL to a separate document) or to accept it as it is.

What counts as a selector

Selectors are three kinds of filters that Content Lake uses to find documents by value (Sync tags → How Content Lake generates sync tags). Each of them works in two forms, with == and with in:

Selector Forms Tags
_id _id == $id, _id in $ids one tag per id
slug slug.current == $slug, slug.current in $slugs one tag per value
_type _type == "post", _type in ["post", "author"] one tag per type

For slug, any path that ends in slug.current and does not go through an array works, for example seo.slug.current, and all these paths share one tag per value.

A condition on any other field does not narrow the tag. Your own fields, references(), match and defined() are not selectors: with _type in the same filter, the response gets the type tag, and without _type, the dataset-wide tag.

Apart from the top-level filter, each subquery adds its own tags by the same rules, and each -> adds the tag of the document it links to. The rule about the dataset-wide tag has an exception (see “Where tags get lost”).

The response gets the tag of an _id even if no document has this _id. The tags come from the values in the query, not from the documents found:

// document tag no-such-doc, result null
*[_type == "page" && _id == "no-such-doc"][0]

A cached 404 updates as soon as someone creates a document with this _id.

The tag from -> depends on the data. -> runs as a lookup by the _id stored in the reference, so the same query gets different tags on different pages:

// tag page-about-en and the tag of the header this page links to
*[_id == "page-about-en"][0]{header->{title}}

With ||, the response gets the tags of both sides only if both sides are selectors. If one side is not a selector, it can match any document (any document of the type, if the filter also has _type), and the response gets one broad tag: the dataset-wide tag or the type tag. This tag also covers the side with the selector:

Query Tags
*[_id == "page-about-en" || _id == "page-home-en"] page-about-en, page-home-en
*[slug.current == "article-3" || title == "About us"] dataset-wide
*[_type == "page" && (slug.current == "/about" || title == "About us")] type page

I found one exception where || gives a tag that is too narrow: a check for an empty slug field. See the example in “Where tags get lost”.

Looks like a selector, but is not

Condition Why it is not a selector
pathname.current, where pathname is a field of type slug the field name is not slug
myslug.current, slugs[0].current the segment before .current must be exactly slug
slug as a string, slug.value the path does not end in slug.current
translations[0].slug.current the path ends in slug.current, but it goes through an array
_ref, header._ref this is the value of a reference, not the _id of the document itself
_updatedAt, _rev these are system fields, but of the system fields only _id and _type are selectors
language, locale, title, path, url, handle, sku these are ordinary fields, even if their values are unique

What a publish event carries

Content Lake builds the event only from the document itself: from its versions before and after the change. Drafts and release versions do not go into the regular event stream: only clients with a token and includeDrafts get their events.

On test documents, I measured the tags carried by the event:

Change Event tags
create or patch with no slug document · type · dataset-wide
the document has slug.current: "a" + slug tag a
slug.current changes from a to b + slug tag a and slug tag b
seo.slug.current: "e" + slug tag e (the same tag as for the query slug.current == "e")
slug: "d" as a string nothing more
language, locale, name, id, key nothing more
slug.current in the document, although the schema has no such field + slug tag: Content Lake reads the value, not the schema
a publish from Studio (it deletes the draft) + draft id

On the production site, I matched this table against 22 real events across 11 documents of different types, with no discrepancies. Slug changes and draft IDs I checked only on the test dataset.

The URL key must be on the path slug.current

The field that a query uses to find a page by its URL (from here on, the URL key) must be named slug, so that the path to its value ends in slug.current. Otherwise, the response falls back to the type tag, and publishing any page of this type revalidates all pages of this type.

*[_type == "page" && slug.current == $slug][0]        // slug tag: one value
*[_type == "page" && pathname.current == $slug][0]    // type tag page: a publish of any page revalidates them all
*[pathname.current == $slug][0]                       // dataset-wide tag
defineField({ name: 'pathname', type: 'slug' }) *// ✗ the slug type does not help*
defineField({ name: 'slug', type: 'slug' })     *// ✓*

The field name defines the selector, not the field type. Content Lake does not read the Studio schema: for Content Lake, a pathname field of type slug is the same as a title field.

When the URL key is in the slug field, the slug tag is on both sides: in the query response and in the publish event.

The slug tag is tied to the value itself, not to a specific document or type. If two documents share a slug, publishing one also revalidates routes that look up the other by that slug.

For example, this happens with translations of one page, or a page and an article in different sections of the site.

When you change a document’s slug, the event carries both the old and new values. Routes for both the old URL and the new URL are revalidated.

Subqueries: ^ in the outer filter is not a selector

A subquery by itself does not widen the tags: in _id in *[...] and in a projection, they stay narrow. Correlation causes the problem: a subquery with ^ in the filter of the outer query. Sanity describes the ^ operator in High-performance GROQ → The parent operator.

In the examples below, the relatedId field of the article article-05 stores the _id of another article, article-03. Each query returns article-03:

Query Tags
*[_id in *[_id == "article-05"].relatedId] article-05, article-03: narrow
*[_id == "article-05"][0]{"linked": *[_id == ^.relatedId][0]} article-03, article-05: ^ in a projection is narrow too
*[_type == "article" && count(*[_id == "article-05" && relatedId == ^._id]) > 0] type article, article-05
*[_id in ["article-03","article-05"] && count(*[… == ^._id]) > 0] article-03, article-05
*[count(*[_id == "article-05" && relatedId == ^._id]) > 0] only article-05: nothing covers the result article-03

A subquery with ^ does not give the outer filter a tag of its own. If the same filter has _type or _id in [...], the response gets their tags. If the filter has nothing else, the outer query gives no tag at all, and nothing covers article-03 in the last row. This is a special case of tag loss (see “Where tags get lost”).

An example: a helper document of type translation.metadata links the language versions of a page and has references to all translations. A link in the content must lead to the version of the page in the required language. In the projection of the link, where ref is the reference to the page, you can write this lookup in two forms. They return the same result, but they get different tags:

// correlated subquery + _type: type tag page
"target": *[_type == "page" && language == $lang
  && _id in *[_type == "translation.metadata" && references(^.^.ref._ref)][0]
      .translations[].value._ref][0]


// dereference: tag of the page in the requested language
"target": *[_type == "translation.metadata" && references(^.ref._ref)][0]
  .translations[language == $lang][0].value->

sync-tags-translationsync-tags-translation

Both forms keep the type tag of translation.metadata: references() is not a selector. If you have a similar structure, where the helper document is necessary only for the lookup and no page shows it, its type tag is on every route that looks up a translation, and a publish of any such document revalidates all these routes. In my opinion, the query shape cannot prevent this: the page does not store the id of its helper document, and no selector can find it.

Where tags get lost: an edit does not reach the response

According to the documentation, each part of a query that reads documents adds its own tags, and a filter with no selector and no _type gets the dataset-wide tag. In practice, the dataset-wide tag is added only if the whole query gives no other tag. If at least one part of the query gives any other tag, even a type tag, nothing covers the parts without selectors.

Query Tags Covered
*[title == "v1"] dataset-wide yes
{"a": *[title == "v1"], "b": *[name == "Ann"]} dataset-wide yes
{"a": *[title == "v1"], "b": *[_id == "site-settings"][0]} document site-settings no
{"a": *[title == "v1"], "b": *[_type == "author"]} type author no for a
*[_id == "author-ann"][0]{name, "posts": *[references(^._id)]} author-ann no
the same + _type == "post" in the inner query + type post yes

I confirmed this with real events: the query result changed, yet the tags did not overlap. This behavior is identical across API versions v2026-10-22 and v2026-11-02, as well as every perspective I tested: published, raw, drafts, and release.

This is how it looks in ordinary code:

// author page: a new post does not appear until the author changes
*[_type == "author" && slug.current == $slug][0]{
  name,
  "posts": *[references(^._id)] | order(publishedAt desc){title}
}


// superquery: nothing covers nav
{
  "page": *[_type == "page" && slug.current == $slug][0],
  "nav": *[showInNav == true]{title, "slug": slug.current},
  "settings": *[_id == "site-settings"][0]
}

sync-tags-lostsync-tags-lost

The workaround: _type in every scan, that is, in every filter that finds documents not by _id and not by slug. *[_type == "post" && references(^._id)] gives the type tag post, and the author page updates on a publish of any post. This behavior has been reported to Sanity: issue.

A response where || checks a slug field for an empty value is not covered either. A lookup of the home page, which has no slug, can look like this:

*[_type == "page" && (slug.current == $slug || !defined(slug.current))][0]

The response gets only the slug tag with the value $slug, and no type tag page. The home page has no slug, so its event carries no slug tag, and judging by what the event carries, its edits do not reach this response. slug.current == null and a check of another slug path work the same way: with || !defined(seo.slug.current), the query returned all 12 pages of the test dataset, but the response got one slug tag. A check for an empty value of an ordinary field, for example !defined(language) or language == null, gives the correct type tag.

Where tags collapse: broad tags replace precise ones

The 100-tag limit. A response can have no more than 99 tags: at 100 or more, broad tags replace the precise ones (Sync tags → The sync tag limit). With _type in the filter, the response collapses to the type tag, and without it, to the type tags of the documents it read or to the dataset-wide tag. I checked this on five API versions, from v2021-10-21 to v2026-11-02, and the behavior is the same. These numbers are for the published perspective. In the drafts perspective, each id gives two tags, and the response reaches the limit with half as many ids.

If some of the IDs match a document, the collapse is partial. IDs without a matching document keep their document tags. A query for 100 IDs with 60 matches and 40 misses gets the type tag plus 40 document tags.

The second collapse: correlated subqueries with _type. It happens long before 100 tags. When a correlated subquery of the form _type == T && _id in *[... ^ ...] repeats inside an array projection (items[]{...}), the document tags change to type tags after several repeats. On the test dataset, one projection with such a subquery, repeated k times in the response, gave 15 tags for k from 1 to 6: 12 document tags and 3 type tags. At k = 7, 6 tags were left: almost all document tags, including the ones from ->, changed to type tags.

The collapse depends on the number of repeats, not on the number of tags: below k = 7, the response has only 15 tags. The same projection in the dereference form does not collapse until k ≈ 41–44.

On the production site, this collapse brought the tags of all 383 article routes down to the dataset-wide tag: through nested fragments, the article query repeated the translation lookup dozens of times. After these subqueries moved to the dereference form, no collapsed routes were left, and after all the fixes the median number of tags per route grew from 10 to 22.

How to find it: measure the whole query and each top-level projection separately. The symptom is that the whole query carries fewer tags than the sum of its parts: the document tags have turned into type tags or into one dataset-wide tag.

Recipes

_type in every query. The documentation recommends it because of the limit: on overflow, the response collapses to the type tag and not to the dataset-wide tag (Sync tags → Write queries that produce precise sync tags). The second reason is lost tags: a scan with _type carries the type tag even if other parts of the query have selectors.

Dereference instead of a correlated subquery. -> is more precise and collapses only after about six times as many repeats. Sanity calls -> “a subquery in disguise” and recommends against repeating it (High-performance GROQ → Avoid repeated resolving of references), but for tags this form is the better one. TypeGen gets in the way: it types a lookup from translations[] as a union of all translatable types. The easy way to narrow the type is _type == T && _id in *[...^...], and this is the form that collapses. Check the -> form with TypeGen before you refactor.

A category page that revalidates only for its own articles. The task: a publish of an article revalidates the page of its category and does not touch the other categories. Query options for the category page:

Query Tags Effect
*[_type == "article" && mainCategory._ref == "category-news"] type article any article revalidates all categories
*[_type == "article" && references("category-news")] type article the same
*[_type == "article" && mainCategory->slug.current == "news"] type article + categories the same
*[_id == "category-news"][0]{"articles": *[references(^._id)]} only the category article edits do not reach it
*[_type == "article" && categoryKey.slug.current == "news"] slug news precise

The last row is denormalization: the article stores a copy of the category slug on a path that ends in slug.current.

defineField({ name: 'categoryKey', type: 'object', fields: [{ name: 'slug', type: 'slug' }] })

The article event carries the slug tag news, and when the article moves to another category, it carries the old slug and the new one. The cost: the copy must stay in sync (for example, with a Document Function), a rename of the category slug means that you must rewrite all its articles, and the recipe depends on what the event carries today.

The guide High-performance GROQ → Avoid joins in filters recommends vertical._ref == "football-doc-id" instead of vertical->slug.current == "football". For performance this is correct, but for tags both forms give the type tag. In the same guide, Sanity recommends “a little denormalizing for frequently used ‘core’ fields”: this is the denormalization above.

Listings carry the type tag. The block “three latest articles in the same category” scans all articles to find the three newest ones, and _ref in $cats is not a selector, so a publish of any article revalidates every route with this block. The options: curated related articles, where the references resolve through -> and give document tags, or removing the block.

Schema and queries now cover tags

Until now, schema design and query shapes only accounted for data structure, access rules, and business logic. Sync tags add a fourth requirement: your queries must return the exact tags needed for cache revalidation.

If your site revalidates with sync tags, start with four checks:

  • Keep the URL key in slug. Route-level invalidation maps cleanly when URL keys follow a predictable convention.
  • Include _type on every scan. Explicit types keep type-level revalidation tags accurate.
  • Resolve related documents with ->. Avoid correlated subqueries, which obscure document dependencies and complicate tag tracking.
  • Stay well below the 100-tag limit. Audit heavy pages and hubs so responses never approach the cap.

Reading query code is not enough to verify this. On our production site, manual predictions missed the actual runtime behavior across 77 page-template and document-type combinations. You have to measure the tags returned by the fully assembled query instead.

To automate that check, I built a revalidation-audit skill for Claude Code. It executes every route’s assembled queries, maps each returned tag to its source, validates predictions against live publishes, and generates a separate view showing which routes revalidate whenever a specific document changes.