Pages router: Using `shallow` "push/replace" with proxy/middleware rewrites, results in rendering previous route
Nobody has claimed this yet.
- Dominant language
- JavaScript
- Stars
- 142k
- Forks
- 32.4k
- Avg merge
- 2d 14h
- Merged PRs (30d)
- 351
Description
Link to the code that reproduces this issue
https://codesandbox.io/p/devbox/thirsty-bas-d3cdjr
To Reproduce
- You require a
proxy.ts/middleware.tsthat rewrites the current route. In the example we have adeviceparameter, which includes the current type of device derived from the user agent. - Have at least 2 routes. The issue occurs if you navigate e.g. from the homepage to the
/some-routeexample. - Have a
shallowroute transition to your own route, e.g. by updating query parameters - Execute the route push multiple times
- 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.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with the linked CodeSandbox and reproduce the issue using proxy.ts or middleware.ts rewrites, two routes, and repeated shallow query-parameter updates. Compare behavior across the reported Next.js versions, including 15.4.0 and 15.5.x. Done means repeated shallow push or replace operations continue rendering the current route rather than the previous route while the address bar remains correct.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- next.js, node.js, react, typescript
- Domain
- frontend, web-dev
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100