fix(app-router): guard popstate scroll restore on stale navigation#7
Closed
NathanDrake2406 wants to merge 4 commits into
Closed
fix(app-router): guard popstate scroll restore on stale navigation#7NathanDrake2406 wants to merge 4 commits into
NathanDrake2406 wants to merge 4 commits into
Conversation
The popstate scroll race tests imported app-browser-entry directly, which pulls virtual modules that the unit runner cannot load outside the browser entry build graph. That made the PR fail before the new regression tests could run. Extract the popstate restoration coordinator into a normal helper module, wire the browser entry through it, and keep the tests' fake RSC navigation promises identical to the pending marker they assert against.
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.
Summary
Prevents stale App Router popstate completions from restoring an old history scroll position after rapid back/forward races.
Problem
window.__VINEXT_RSC_NAVIGATE__is invoked for everypopstate, and we currently runrestorePopstateScrollPosition(event.state)in.finallyunconditionally. If multiple back/forward traversals run concurrently, an older navigation can resolve after a newer one and incorrectly restore scroll for the stale history entry.This manifests as jumpy/incorrect scroll when users navigate quickly or under network jitter.
Why this change is meaningful
This is not speculative UI behavior tuning; it is a concrete race-condition fix affecting user-visible navigation state:
activeNavigationId/isCurrentNavigation) and with Next.js intent to drop superseded traversalsImplementation
In
packages/vinext/src/server/app-browser-entry.ts:navigatesession id (navId) for each popstate-triggered RSC navigation viabrowserNavigationController.getActiveNavigationId().finallyhandler unless thatnavIdis still current__VINEXT_RSC_PENDING__identity guard for clearing only the current pending markerNet effect: only the winner traversal may restore scroll, and superseded traversals settle quietly.
Next.js parity / reference
ACTION_NAVIGATE/ACTION_RESTOREsupersede pending work by marking itdiscarded:https://github.com/vercel/next.js/blob/canary/packages/next/src/client/components/app-router-instance.ts#L173-L206
https://github.com/vercel/next.js/blob/canary/packages/next/src/client/components/app-router.tsx#L375-L399
https://github.com/vercel/next.js/blob/canary/packages/next/src/shared/lib/router/router.ts#L670-L696
Validation
0abbf083