nextjs - 💡(How to fix) Fix router.replace with shallow: true persists shallow flag in history state, causing getServerSideProps to be skipped on browser back navigation

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…

Code Example

n/a
RAW_BUFFERClick to expand / collapse

Link to the code that reproduces this issue

https://github.com/JanKaifer/next-repro-34365.

To Reproduce

When router.replace is called with shallow: true, Next.js writes the shallow flag into the window.history state of that entry. This is correct for the immediate navigation, it suppresses the getServerSideProps call as intended. However, the flag is never cleared, so when the user later navigates back to that entry via the browser, Next.js reads the flag from the history state and incorrectly skips the data fetch again. The page URL updates but the page is rendered with stale data.

StepActionURLHistory stack (oldest → newest)Data fetch?
1router.push('/page1')/page1/page1 {shallow:false}Yes
2router.replace('/page1?count=1', {shallow:true})/page1?count=1/page1?count=1 {shallow:true}No (shallow)
3router.push('/page2')/page2/page1?count=1 {shallow:true} <br>/page2 {shallow:false}Yes
4router.replace('/page2?count=1', {shallow:true})/page2?count=1/page1?count=1 {shallow:true} <br>/page2?count=1 {shallow:true}No (shallow)
5Browser back/page1?count=1/page1?count=1 {shallow:true} ⚠️ <br>/page2?count=1 {shallow:true}No ❌ Bug

Current vs. Expected behavior

Expected behaviour

Navigating back to /page1?count=1 via the browser should trigger a getServerSideProps call and render fresh data.

Actual behaviour

getServerSideProps is not called. The URL updates to /page1?count=1 but the page retains stale data from the previous render.

Provide environment information

n/a

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

Linking and Navigating, Pages Router

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

next dev (local), next build (local), next start (local), Vercel (Deployed), Other (Deployed)

Additional context

Related issues

This appears to be a variant of #34365 and #16028.

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 - 💡(How to fix) Fix router.replace with shallow: true persists shallow flag in history state, causing getServerSideProps to be skipped on browser back navigation