Skip to content

[Pages] fix: interpolate query-only navigation when asPath has a query string - #101198

Open
Alvi-Rahman wants to merge 2 commits into
vercel:canaryfrom
Alvi-Rahman:fix/resolve-href-query-in-asPath
Open

Alvi-Rahman wants to merge 2 commits into
vercel:canaryfrom
Alvi-Rahman:fix/resolve-href-query-in-asPath

Conversation

@Alvi-Rahman

Copy link
Copy Markdown

Fixes #84942

What?

In the Pages Router, a query-only router.push({ query }) or <Link href="?…"> on a route with a dynamic segment followed by a static one (e.g. /[id]/detail) keeps the old segment when the current URL already has a query string.

current URL: /b/detail?fromId=a
router.push({ query: { id: 'c', fromId: 'b' } })

expected: /c/detail?fromId=b
actual:   /b/detail?id=c&fromId=b

This is a regression from 15.5.0, introduced by #82236.

Why?

resolveHref (packages/next/src/client/resolve-href.ts) decides whether the current page was rewritten by running the anchored route regex against router.asPath. asPath includes the query string, so ^/([^/]+?)/detail(?:/)?$ never matches /b/detail?fromId=a. The page is then treated as rewritten, and the query is never interpolated into the path.

Routes like /[id] only appeared to work because the segment pattern happened to swallow the query string.

How?

Match the regex against the pathname only, using the existing parsePath helper.

Tests

Added a dynamic-mid-path/[id]/detail page and two cases (router.push and Link) to the test/e2e/use-router-with-rewrites suite from #82236.

Both new cases fail on canary (expected b/c, got a). All 11 tests in the suite pass with the fix in Turbopack dev, webpack dev, and production builds with both bundlers.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

useRouter replace/push works wrong with query-only navigation on mid-path dynamic routes

1 participant