nextjs - ✅(Solved) Fix useRouter replace/push works wrong with query-only navigation on mid-path dynamic routes [1 pull requests, 1 participants]

Official PRs (…)
ON THIS PAGE

Recommended Tools

×6

Utilities matched from this issue’s tags and category — try them while you read without losing context.

GitHub issue graph ai analysis

Paste a GitHub issue URL. We fetch that issue, discover linked issues from bodies/comments/timeline, collect linked pull requests, and produce a structured English report.

The report is written in English Markdown for sharing and archival.

Helpful · Quick feedback

Loading…
GitHub stats
vercel/next.js#84942Fetched 2026-04-08 02:17:57
View on GitHub
Comments
0
Participants
1
Timeline
3
Reactions
3
Author
Participants
Timeline (top)
issue_type_added ×1labeled ×1subscribed ×1

PR fix notes

PR #82236: [Pages] fix: use asPath for query-only navigation with useRouter

Description (problem / solution / changelog)

Why?

The issue was brought up when the app had rewrites (say / -> /foo), and the page used useRouter.push/replace to add the search queries; it used the destination's pathname (/foo?query=1) instead of just adding the query (/?query=1).

Current Behavior: https://github.com/vercel/next.js/actions/runs/16647831682/job/47112716254?pr=82236#step:34:156

How?

Therefore, as we handle the hash (#), if the target path only needs a query ?, we may use router.asPath by default for static routes to resolve cases with rewrites. However, if it is a dynamic route, it needs to be able to interpolate the query (for /[id] segment, /?id=1 interpolates to /1 route), so use router.pathname. Finally, if it is suspected to be a rewritten path, use router.asPath.

Note:

There is an edge case where the pathname is dynamic, and also a rewrite path to the same segment.

E.g. in /[slug] path, rewrite /foo -> /bar

In this case, it will be treated as a non-rewritten path and possibly interpolate the query string.

E.g., /any?slug=foo will become the content of /foo, not rewritten as /bar.

This is currently a trade-off of not resolving rewrite paths on every Router/Link call, but using a lighter route regex pattern check.

Fixes https://github.com/vercel/next.js/issues/81476

Changed files

  • packages/next/src/client/resolve-href.ts (modified, +35/-4)
  • test/e2e/use-router-with-rewrites/next.config.js (added, +33/-0)
  • test/e2e/use-router-with-rewrites/pages/foo.tsx (added, +34/-0)
  • test/e2e/use-router-with-rewrites/pages/rewrite-to-another-segment/[id]/foo.tsx (added, +35/-0)
  • test/e2e/use-router-with-rewrites/pages/rewrite-to-same-segment/[id]/index.tsx (added, +35/-0)
  • test/e2e/use-router-with-rewrites/use-router-with-rewrites.test.ts (added, +97/-0)

Code Example

Operating System:
  Platform: linux
  Arch: x64
  Version: #1 SMP PREEMPT_DYNAMIC Sun Aug  6 20:05:33 UTC 2023
  Available memory (MB): 4102
  Available CPU cores: 2
Binaries:
  Node: 20.9.0
  npm: 9.8.1
  Yarn: 1.22.19
  pnpm: 8.10.2
Relevant Packages:
  next: 15.5.5 // Latest available version is detected (15.5.5).
  eslint-config-next: N/A
  react: 19.0.0
  react-dom: 19.0.0
  typescript: 5.1.3
Next.js Config:
  output: N/A
RAW_BUFFERClick to expand / collapse

Link to the code that reproduces this issue

https://codesandbox.io/p/devbox/busy-hooks-nznk9l?workspaceId=ws_AYcZxx2mGgGyE3daYCqVTz

To Reproduce

  1. start codesandbox
  2. click Preview: 3000
  3. click push to [a] page/a/detail
  4. click push to [b] page/b/detail?fromId=a
  5. click push to [c] page/b/detail?id=c&fromId=b

Current vs. Expected behavior

current

  1. click push to [c] page/b/detail?id=c&fromId=b

expected

  1. click push to [c] page/c/detail?fromId=b

Provide environment information

Operating System:
  Platform: linux
  Arch: x64
  Version: #1 SMP PREEMPT_DYNAMIC Sun Aug  6 20:05:33 UTC 2023
  Available memory (MB): 4102
  Available CPU cores: 2
Binaries:
  Node: 20.9.0
  npm: 9.8.1
  Yarn: 1.22.19
  pnpm: 8.10.2
Relevant Packages:
  next: 15.5.5 // Latest available version is detected (15.5.5).
  eslint-config-next: N/A
  react: 19.0.0
  react-dom: 19.0.0
  typescript: 5.1.3
Next.js Config:
  output: N/A

Which area(s) are affected? (Select all that apply)

Pages Router

Which stage(s) are affected? (Select all that apply)

next dev (local)

Additional context

Side effect of https://github.com/vercel/next.js/pull/82236 since https://github.com/vercel/next.js/releases/tag/v15.5.0

This bug occurs when a dynamic route is in the middle of a path (e.g. /[id]/detail) This bug does not occur when a dynamic route is at the end of a path (e.g. /[id])

extent analysis

TL;DR

The most likely fix for this issue is to update the link generation logic to correctly handle dynamic routes in the middle of a path.

Guidance

  • Review the next configuration and version to ensure it's compatible with the current setup, as the issue is related to a side effect of a specific pull request.
  • Check the link generation logic to ensure it correctly handles dynamic routes in the middle of a path, as the issue occurs when a dynamic route is in the middle of a path (e.g., /[id]/detail).
  • Verify that the issue is specific to next dev (local) and not present in other environments.
  • Consider testing with a different version of next to see if the issue persists, as the latest available version is detected (

Vote matrix · Quick signals

Works
Did the solution work? Tap to confirm.
Easy Fix
Was it a quick fix?
Time Saver
Did it save you time?
Blocking
Was it severely blocking?
Common Issue
Are others likely hitting this too?
Flaky / Intermittent
Is it intermittent?
Verified / Reproducible
Can you reproduce it reliably?
Loading…

Still need to ship something?

×6

Another batch ranked right after the header list — different links, same matching logic.

Back to top recommendations

TRENDING

nextjs - ✅(Solved) Fix useRouter replace/push works wrong with query-only navigation on mid-path dynamic routes [1 pull requests, 1 participants]