Skip to content

fix(router): preserve multi-value search params in segment cache key - #93154

Closed
NiteshCodes7 wants to merge 9 commits into
vercel:canaryfrom
NiteshCodes7:fix/multi-value-search-param-stale-cache
Closed

NiteshCodes7 wants to merge 9 commits into
vercel:canaryfrom
NiteshCodes7:fix/multi-value-search-param-stale-cache

Conversation

@NiteshCodes7

Copy link
Copy Markdown

Closes #93104

What's happening

When navigating from ?k=A&k=B (multi-value) to ?k=B (single-value) via router.replace(), the client tree never commits the update — props stay stale even though the server renders correctly.

Root cause

In createSegmentFromRouteTree inside ppr-navigations.ts, the client builds a segment cache key from the current renderedSearch string:

// Before (buggy)
const stringifiedQuery = JSON.stringify(
  Object.fromEntries(new URLSearchParams(renderedSearch))
)

Object.fromEntries on a URLSearchParams with 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 matchSegment treats 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 to Object.fromEntries)
  • "works on second toggle" ✅ (by then currentRenderedSearch has caught up so the comparison path differs)

Fix

Use getAll() to preserve repeated values, matching exactly what the server does in addSearchParamsIfPageSegment:

// After (fixed)
const urlSearchParams = new URLSearchParams(renderedSearch)
const queryObj: Record<string, string | string[]> = {}
for (const key of new Set(urlSearchParams.keys())) {
  const vals = urlSearchParams.getAll(key)
  queryObj[key] = vals.length === 1 ? vals[0] : vals
}
const stringifiedQuery = JSON.stringify(queryObj)

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).

@vercel vercel Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Additional Suggestions:

  1. Object.fromEntries(new URLSearchParams(...)) in route-params.ts and scheduler.ts collapses repeated search param keys (e.g., ?k=A&k=B becomes {k: "B"}), producing incorrect cache keys and segment comparisons.
  1. Object.fromEntries(new URLSearchParams(...)) in scheduler.ts and route-params.ts collapses multi-value search params (e.g., ?k=A&k=B becomes {k: "B"} instead of {k: ["A", "B"]}), causing incorrect segment cache matching.

Fix on Vercel

@NiteshCodes7

Copy link
Copy Markdown
Author

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. ?k=A&k=B -> { k: ["A", "B"] }) instead of collapsing them via Object.fromEntries(...).

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.

@gadkadosh

Copy link
Copy Markdown

I can confirm that this PR solves this problem for me as well and the implementation seems sound to me.
Before finding this I made a minimal repo of my own. I'm posting it here in case that helps speed up the review and merge process:
https://github.com/gadkadosh/next-rsc-searchparams-repro

@NiteshCodes7

Copy link
Copy Markdown
Author

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!

@NiteshCodes7 NiteshCodes7 closed this by deleting the head repository Aug 6, 2026
@github-actions github-actions Bot added the locked label Sep 5, 2026
@github-actions github-actions Bot locked as resolved and limited conversation to collaborators Sep 5, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

multi→single search-param unset does not commit RSC

2 participants