Repository navigation
Fix(segment-cache): replace hash instead of concatenating on repeated same-path navigation - #93855
Varshil-B-Shah wants to merge 2 commits into
Conversation
ac5ac6d to
41e75ba
Compare
41e75ba to
dc2bcae
Compare
Failing test suitesCommit: dc2bcae | About building and testing Next.js
Expand output● app dir - navigation › query string › should set query correctly ● app dir - navigation › query string › should set query with semicolon correctly ● app dir - navigation › query string › should handle unicode search params ● app dir - navigation › query string › should not reset shallow url updates on prefetch ● app dir - navigation › query string › useParams identity between renders › should be stable in app ● app dir - navigation › query string › useParams identity between renders › should be stable in pages ● app dir - navigation › hash › should scroll to the specified hash ● app dir - navigation › hash › should not scroll to hash when scroll={false} is set ● app dir - navigation › hash-with-scroll-offset › should scroll to the specified hash ● app dir - navigation › hash-link-back-to-same-page › should scroll to the specified hash ● app dir - navigation › relative hashes and queries › should work with a hash-only href ... truncated ... |
Navigating to a route whose segment-cache entry was recorded with a hash appends the new fragment instead of replacing it (vercel/next.js#93126, regression in 16.2.0). Applies the one-line fix from the upstream PR (vercel/next.js#93855) via pnpm patch; drop the patch once it ships in a stable release. Assisted by Claude Fable 5
Fixes: #93126, #95551 Repro: - from `/` navigate to `/abc#foo` - on `/abc#foo` - now click `/abc#bar` - the url is now at `/abc#foo#bar` Unlike, #93855, this PR fixes what gets stored in the segment cache navigation, rather than slicing away an existing hash. It is plausible that dropping the hash from the segment cache is not desired though.
Fixes: vercel#93126, vercel#95551 Repro: - from `/` navigate to `/abc#foo` - on `/abc#foo` - now click `/abc#bar` - the url is now at `/abc#foo#bar` Unlike, vercel#93855, this PR fixes what gets stored in the segment cache navigation, rather than slicing away an existing hash. It is plausible that dropping the hash from the segment cache is not desired though.
What?
Fixes a bug where navigating between hash fragments on the same pathname results in hash concatenation instead of hash replacement.
Steps to reproduce:
/startto/destination#foovia a<Link>/destination#foo, click another<Link>to/destination#baz/destination#foo#bazinstead of/destination#bazThis also affects
router.push('/path#newHash')when the current URL already contains a hash on the same path.Why?
When navigating to a URL that has already been prefetched and cached in the segment cache,
navigateUsingPrefetchedRouteTreeconstructs the newcanonicalUrlby appendingurl.hashdirectly toroute.canonicalUrl:The problem is that
route.canonicalUrlis stored in the cache with the hash included from the first navigation (e.g./destination#foo). When the user navigates to the same path with a different hash (e.g./destination#baz), the new hash is blindly appended to the already-hashed canonical URL, producing/destination#foo#baz.The cache intentionally keys entries without the hash (so
/destination#fooand/destination#bazshare the same cache entry), but thecanonicalUrlstored in the entry retains the full URL including the hash from when the entry was first populated. The code comment in the file even acknowledges this design tension:This regression was introduced as part of the Segment Cache / Cached Navigations feature that shipped in Next.js 16.2.0.
How?
Strip any existing hash from
route.canonicalUrlbefore appending the new hash:split('#')[0]takes everything before the first#, ensuring no stale hash can survive into the new URL. The correct new hash fromurl.hashis then appended cleanly.This is safe because:
route.canonicalUrlhas no hash:split('#')[0]returns the full string unchanged — zero behavior change for the common caseroute.canonicalUrlhas a hash: the stale hash is stripped before the new one is appended — the bug is fixedurl.hashis''(navigating to a path with no hash): the result is just the clean path — correctThe cache lookup logic (which ignores hashes) is completely untouched.
Testing
The test fixtures and test case are adapted from the draft PR #93132 by @icyJoseph which identified the bug with a failing e2e test. This PR adds the fix that makes the test pass.
The test covers the exact reproduction scenario:
/hash-cross-path-push<Link href="/hash-cross-path-push/destination#foo">→ URL becomes/destination#foo✓<Link href="/hash-cross-path-push/destination#baz">→ URL becomes/destination#baz✓ (was/destination#foo#bazbefore the fix)Fixes #93126