Repository navigation
fix(router): preserve multi-value search params in segment cache key - #93154
NiteshCodes7 wants to merge 9 commits into
Conversation
There was a problem hiding this comment.
Additional Suggestions:
Object.fromEntries(new URLSearchParams(...))inroute-params.tsandscheduler.tscollapses repeated search param keys (e.g.,?k=A&k=Bbecomes{k: "B"}), producing incorrect cache keys and segment comparisons.
Object.fromEntries(new URLSearchParams(...))in scheduler.ts and route-params.ts collapses multi-value search params (e.g.,?k=A&k=Bbecomes{k: "B"}instead of{k: ["A", "B"]}), causing incorrect segment cache matching.
|
Updated this PR to address the additional occurrences flagged by the bot. I extracted the search param conversion into a shared helper that preserves repeated keys (e.g. The helper is now reused in the related cache key / segment generation paths to keep behavior consistent and avoid collisions caused by multi-value params. |
|
I can confirm that this PR solves this problem for me as well and the implementation seems sound to me. |
|
Hey team, Just a quick reminder — there's an open PR for our Next.js Vercel deployment that still needs a review. Would appreciate it if someone could take a look when they get a chance. Thanks! |
Closes #93104
What's happening
When navigating from
?k=A&k=B(multi-value) to?k=B(single-value) viarouter.replace(), the client tree never commits the update — props stay stale even though the server renders correctly.Root cause
In
createSegmentFromRouteTreeinsideppr-navigations.ts, the client builds a segment cache key from the currentrenderedSearchstring:Object.fromEntrieson aURLSearchParamswith repeated keys silently drops all but the last value. This means:?k=A&k=B→{ k: "B" }→__PAGE__?{"k":"B"}?k=B→{ k: "B" }→__PAGE__?{"k":"B"}Both produce the same segment key, so
matchSegmenttreats them as identical, the stale cache node is reused, and the client never re-renders with the new props.This explains the asymmetric behavior reported in the issue:
empty → single✅ (keys are genuinely different)single → multi✅ (keys are genuinely different)multi → single❌ (keys wrongly collide due toObject.fromEntries)currentRenderedSearchhas caught up so the comparison path differs)Fix
Use
getAll()to preserve repeated values, matching exactly what the server does inaddSearchParamsIfPageSegment:Now:
?k=A&k=B→{ k: ["A", "B"] }→__PAGE__?{"k":["A","B"]}?k=B→{ k: "B" }→__PAGE__?{"k":"B"}These are distinct keys, the cache node is correctly replaced, and the client commits the updated props.
Testing
Reproduction repo from the issue: https://github.com/dave-wwg/next-multi-value-searchparam-repro
The existing playwright test suite in that repo passes after this fix (it was designed to fail when the bug is present).