fix(sanity): wrap sanityFetch in 'use cache' for cacheComponents compatibility - #208
Merged
Conversation
next.config enables cacheComponents (since 50485f8). next-sanity's sanityFetch calls cacheTag() internally, which is only legal inside a 'use cache' function, so /sanity and /sanity/[slug] crashed with "cacheTag() can only be called inside a use cache function". Wrap each fetch in a 'use cache' function — next-sanity's intended pattern under Cache Components (cached fetch + tag-based revalidation via SanityLive). This supersedes the React cache() wrapper from #204 (which was not a 'use cache' scope and did not fix the crash); 'use cache' also dedupes across the page render and generateMetadata, so the original dedup goal is still met. Verified in-browser: /sanity and /sanity/lorem-ipsum render with no console errors.
arzafran
added a commit
that referenced
this pull request
Jun 8, 2026
- CHANGELOG [Unreleased]: log the boundary validation + cart-type work (#198), the effort:low fixes (#205), the WebGL cleanup (#199/#206), lenis-prevent (#207), the Sanity 'use cache' fix (#208), and react-scan as an Orchestra toggle (#209). - components/README: drop the deleted dropdown/ row (now Base UI Select). - lib/dev/README: add the 🧊 webgl and 🔬 react-scan toggles to the Orchestra table; note react-scan is opt-in and how it relates to Stats. - sanity/README: show the 'use cache' fetch pattern required under cacheComponents (bare sanityFetch throws cacheTag()).
arzafran
added a commit
that referenced
this pull request
Jun 9, 2026
- AGENTS.md: document the 'use cache' rule for cacheTag()-calling fetches (the #208 crash) and parseApiResponse boundary response-validation (#198) - shopify README: updateItemQuantity/removeItem now take client-held lineId; note GraphQL envelope validation - webgl README: flowmap/fluid sims are opt-in (#199, #206) - biome-custom-rules: flag the cited dropdown example as since-removed (#202)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What this does
/sanityand/sanity/[slug]were crashing at runtime with:This fixes both pages so they render again.
Root cause
next.config.tsenablescacheComponents: true(added in50485f8, "test experimental setting"). next-sanity'ssanityFetch(fromdefineLive) callscacheTag()internally to register the cache tag for live revalidation — and under Cache Components,cacheTag()is only legal inside a'use cache'function. The Sanity example pages calledsanityFetchoutside any'use cache'scope, so they threw.This was latent on
main— not introduced by a recent PR. Confirmed by reverting thecache()wrapper from #204 to the prior direct calls: the error persisted, so Reactcache()was never the cause (it is not a'use cache'boundary).Fix
Wrap each fetch in a
'use cache'function — next-sanity's intended pattern under Cache Components (a cached fetch whosecacheTagis revalidated by<SanityLive>). For[slug], the slug is passed as an argument so the cache keys per article. This supersedes #204's Reactcache()approach and still satisfies its goal:'use cache'also dedupes across the page render andgenerateMetadata, so the same document is fetched once per request.Test Plan
bun run checkgreen — biome,tsgo --noEmit, 414 tests/sanityand/sanity/lorem-ipsumrender with no console errors (previously both threwcacheTag())next buildvalidates the prerender path undercacheComponents