fix(router): do not inherit shallow flag from history state on browser back/forward (popstate)#93870
Open
sleitor wants to merge 1 commit into
Open
fix(router): do not inherit shallow flag from history state on browser back/forward (popstate)#93870sleitor wants to merge 1 commit into
sleitor wants to merge 1 commit into
Conversation
When router.replace() is called with shallow:true, Next.js stores the shallow flag inside the window.history state for that entry. This is correct for the immediate navigation, but when the user later navigates back via the browser (popstate), the onPopState handler was reading options.shallow from the history state and re-applying it, causing getServerSideProps to be skipped again on browser back/forward. Fix: always force shallow:false in the onPopState handler. Browser back/forward navigation should never be shallow — the stored flag was only meant to suppress the data fetch at the time of the original replace/push call. Adds a regression test to the client-shallow-routing test suite. Fixes vercel#93844
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What
Fixes #93844
When
router.replace()is called withshallow: true, Next.js stores the shallow flag insidewindow.history.statefor that history entry. This is correct for the immediate navigation — it suppresses thegetServerSidePropscall as intended.However, the flag is never cleared, so when the user later navigates back (or forward) via the browser, the
onPopStatehandler readsoptions.shallowfrom the stored history state and re-applies it, incorrectly skipping the data fetch again.How
In the
onPopStatehandler, forceshallow: falseregardless of what is stored in the history state:Browser back/forward navigations (popstate events) should always trigger a fresh
getServerSidePropscall. The_shallowflag in the history state was only meant to suppress the fetch at the time of the originalreplace/pushcall.Testing
Added a regression test to the existing
client-shallow-routingE2E suite that:router.replace()withshallow: true(no data fetch expected)getServerSidePropsis called (newrandomvalue)Related