nextjs - ✅(Solved) Fix Pages router: Using `shallow` "push/replace" with proxy/middleware rewrites, results in rendering previous route [1 pull requests, 1 comments, 1 participants]

Official PRs (…)
ON THIS PAGE

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#86261Fetched 2026-04-08 02:11:49
View on GitHub
Comments
1
Participants
1
Timeline
13
Reactions
0
Author
Participants
Timeline (top)
referenced ×8commented ×1cross-referenced ×1issue_type_added ×1

Fix Action

Fix / Workaround

There is a workaround that worked until 15.4.0, but does not work anymore starting from at least 15.5.x (I did not yet test all the point releases in between). I'm not even sure if the "workaround" is a bug or this is how it is suppose to work.

PR fix notes

PR #86580: Router: Fix shallow route handling for "routes" in combination with proxy/middleware rewrites

Description (problem / solution / changelog)

When using dynamic route parameters combined with middleware/proxy rewrites, using shallow routing behaves unexpectedly for route parameters that are rewritten and cannot be resolved through the URL (see the test case of device). This PR will prevent trying to resolve those parameters and instead will apply the previous pathname, when pathname itself does not change.

Without the changes, the provided test case would load the Home component on second click, instead of preserving the current route.

This fixes #86261.

Changed files

  • packages/next/src/shared/lib/router/router.ts (modified, +48/-32)
  • test/e2e/middleware-proxy-shallow-push/index.test.ts (added, +33/-0)
  • test/e2e/middleware-proxy-shallow-push/pages/[device]/index.tsx (added, +15/-0)
  • test/e2e/middleware-proxy-shallow-push/pages/[device]/some-route.tsx (added, +11/-0)
  • test/e2e/middleware-proxy-shallow-push/pages/[device]/sub/[id].tsx (added, +12/-0)
  • test/e2e/middleware-proxy-shallow-push/proxy.ts (added, +15/-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.12.1
  npm: 10.5.0
  Yarn: 1.22.19
  pnpm: 8.15.6
Relevant Packages:
  next: 16.0.3 // Latest available version is detected (16.0.3).
  eslint-config-next: N/A
  react: 18.3.1
  react-dom: 18.3.1
  typescript: 5.9.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/thirsty-bas-d3cdjr

To Reproduce

  1. You require a proxy.ts / middleware.ts that rewrites the current route. In the example we have a device parameter, which includes the current type of device derived from the user agent.
  2. Have at least 2 routes. The issue occurs if you navigate e.g. from the homepage to the /some-route example.
  3. Have a shallow route transition to your own route, e.g. by updating query parameters
  4. Execute the route push multiple times
  5. You will end up on the previous route (in this case home). The address bar still shows the correct url though.

There is a workaround that worked until 15.4.0, but does not work anymore starting from at least 15.5.x (I did not yet test all the point releases in between). I'm not even sure if the "workaround" is a bug or this is how it is suppose to work.

Current vs. Expected behavior

Pushing shallow route updates multiple times should not render the previous route.

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.12.1
  npm: 10.5.0
  Yarn: 1.22.19
  pnpm: 8.15.6
Relevant Packages:
  next: 16.0.3 // Latest available version is detected (16.0.3).
  eslint-config-next: N/A
  react: 18.3.1
  react-dom: 18.3.1
  typescript: 5.9.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), next build (local), next start (local), Other (Deployed)

Additional context

The issue is reproducible down to next.js 14.

extent analysis

TL;DR

The most likely fix involves modifying the proxy.ts or middleware.ts to correctly handle shallow route transitions and prevent navigating back to the previous route.

Guidance

  • Review the proxy.ts or middleware.ts implementation to ensure it correctly rewrites the current route and handles shallow route transitions.
  • Verify that the device parameter is being correctly derived from the user agent and updated during route transitions.
  • Test the application with different route transition scenarios to identify the specific conditions that cause the issue.
  • Consider updating the Next.js version to the latest available, as the issue may have been fixed in a later version.

Example

No specific code example can be provided without more information about the proxy.ts or middleware.ts implementation.

Notes

The issue seems to be related to the handling of shallow route transitions in Next.js, and the provided workaround no longer works in versions 15.5.x and later. The root cause of the issue is unclear, and further investigation is needed to determine the correct solution.

Recommendation

Apply a workaround by modifying the proxy.ts or middleware.ts to correctly handle shallow route transitions, as updating to a fixed version is not a clear option due to the uncertainty of the issue's cause.

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