Repository navigation
fix(router): a document-cache entry stores the default theme, not the first visitor's (#978) - #987
Merged
Merged
Conversation
… first visitor's (#978) The document cache stored metadata.initialTheme from the theme cookie of the visitor who rendered the entry, and served it to every later visitor. A visitor with no stored theme kept that visitor's useTheme().theme for the life of the page, because ThemeProvider re-syncs only from an explicitly stored theme. The <html> class was right, since the theme script reads document.cookie. The document-cache middleware now marks the render it may store (_documentCacheRender, set after the store lookup, so it covers the MISS and the stale refresh). payloadInitialTheme renders the no-cookie default when that mark is set and the response stub already passes the cache's own shouldCacheResponse (documentCacheStoresRender), as a PPR shell capture does. So a render whose response opted in before next() starts at the default; any other render keeps the visitor's theme. The unmatched-route 404 reads _readTheme() directly: middleware can opt its URL in while the stub is still a 200, and a 404 is never stored. A Cache-Control written after await next() or in onResponse is not on the stub when the payload is built. For that case payloadInitialTheme marks _payloadVisitorTheme when it emits a theme other than the default, and shouldCacheResponse refuses to store the response, so a late opt-in only stores a default-theme render.
commit: |
This was referenced Sep 30, 2026
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.
Fixes #978.
Problem. The document cache (opt-in via
s-maxage) stored the full HTML, includingmetadata.initialThemeread from the theme cookie of the visitor who rendered it. Every visitor served that cached document got the first visitor'sinitialTheme. A visitor with no stored theme then kept that theme'suseTheme().theme/resolvedThemefor the life of the page, becauseThemeProvideronly re-syncs from an explicit cookie or localStorage value. The<html>class was still right, since the inline theme script readsdocument.cookie, so the bug showed up as a wrong toggle label or icon. #979 fixed the same thing for PPR shells.Before / after:
Consumer example: set
Cache-Controlin the handler, or in middleware beforeawait next().Semantics:
initialThemenext()(handler, or middleware beforenext())next()/ inonResponse, dark visitordark)next(), default-theme visitorCode.
cache/document-cache.ts:_documentCacheRenderafter its store lookup.documentCacheStoresRender(reqCtx)runs the cache's ownshouldCacheResponseagainst the pending response headers.shouldCacheResponserefuses a response that rendered a non-default theme (_payloadVisitorTheme), which catches a late opt-in.rsc/full-payload.ts:payloadInitialThemereturnsdefaultThemewhen both hold, and otherwise the visitor's theme (marking it).rsc/handler.ts: the unmatched 404 reads the visitor's theme through the unguarded accessor, because a 404 is never stored.PublicRequestContextomit list.Test plan.
src/testing/__tests__/document-cache-theme.rsc-test.tsx(throughserveShellRequest):document-cache.test.ts(5 new),cookie-store.test.ts(1 new).origin/main, e.g.expected '…"initialTheme":"dark"…' to contain '"initialTheme":"system"'.theme-doc-cache(a scopedcreateDocumentCacheMiddlewareon/theme/doc-cache/*), plustheme-pprandtheme: 66 passed with dependencies;document-cache-theme,ppr-theme,blog-cache,theme: 32 passed, 1 skipped (the existing fixme).test:unit:all(router 6618 unit + 300 rsc; all consumer apps), lint, format, docs-api, bucketing, parity--strict.Notes.
s-maxagealso lets a platform CDN store the response. This fix covers the in-app document cache, because it needs the middleware's flag.ctx.themeon a document-cached route is still unguarded. The theme docs list this in their safe-read table.