Repository navigation
fix(pages): land the stale-while-revalidate prefetch refresh instead of discarding it - #227
Merged
Merged
Conversation
…of discarding it When a prefetch cacheFor uses a [stale, expires] tuple with stale < expires, navigating to a stale-but-usable entry correctly served the stale page and fired a background revalidate — but then called _prefetchCache.set() to store the refreshed promise. set() refuses to overwrite a still-usable entry, so the revalidated data was discarded and the cache kept serving stale data (and re-firing a discarded refresh) for the entire expires window. Flush the stale entry before set() in the revalidate branch so the refreshed data replaces it, matching stale-while-revalidate semantics. The default cacheFor (stale == expires) is unaffected.
❌ Deploy Preview for staging-void-cloud failed.
|
hyfdev
marked this pull request as ready for review
June 18, 2026 16:52
Brooooooklyn
approved these changes
Jun 20, 2026
cpojer
pushed a commit
that referenced
this pull request
Sep 11, 2026
Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
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.
Prefetched pages configured with a
[stale, expires]cache window now actually serve the revalidated data after they go stale — instead of serving stale data for the entireexpireswindow.The bug
With
cacheFor: [stale, expires](stale < expires), navigating to a stale-but-usable prefetch entry correctly serves the stale page and fires a background revalidate. But it then stored the refreshed promise via_prefetchCache.set(...), andset()refuses to overwrite a still-usable entry — so the revalidated data was silently discarded. The cache kept serving the stale page (and re-firing a discarded refresh) until the entry fully expired, defeating stale-while-revalidate.Expected-correct behavior
SWR = serve stale, then replace with the revalidated data so the next read is fresh.
The fix
In the revalidate branch (
navigation-engine.tsloadNavigationShell),flush()the stale entry before re-set()ing it with the background promise, so the refreshed data replaces the stale one. This is a one-line call-site change that leavesset()'s "don't overwrite a usable entry" guard intact for all other callers. The default single-durationcacheFor(stale == expires) is unaffected — that path never reaches this branch as a usable entry.Tests
Regression test added in
pages-client.test.ts: prefetch withcacheFor: ['5ms','10s'], let it go stale, navigate (serves v1 + revalidates), then assert the next read returns the revalidatedv2. Fails onorigin/main(returns the discarded-stalev1) and passes after. Local gate:pages-client.test.ts17/17; full--project unitzero-collateral vs a cleanorigin/mainbaseline;packages/voidtypecheck + lint + fmt clean. Verified the only otherset()call in this function is a fresh insert (no usable entry present) and correctly needs no flush.Prepared with AI assistance.