Repository navigation
Fix race when navigating Back before hydration - #96252
Merged
Merged
Conversation
Contributor
Tests PassedCommit: e4a0892 |
Contributor
Stats from current PR🔴 3 regressions
📊 All Metrics📖 Metrics GlossaryDev Server Metrics:
Build Metrics:
Change Thresholds:
⚡ Dev Server
📦 Dev Server (Webpack) (Legacy)📦 Dev Server (Webpack)
⚡ Production Builds
📦 Production Builds (Webpack) (Legacy)📦 Production Builds (Webpack)
📦 Bundle SizesBundle Sizes⚡ TurbopackClient Main Bundles
Server Middleware
Build DetailsBuild Manifests
Build Cache
📦 WebpackClient Main Bundles
Polyfills
Pages
Server Edge SSR
Middleware
Build DetailsBuild Manifests
Build Cache
🔄 Shared (bundler-independent)Runtimes
📝 Changed Files (8 files)Files with changes:
View diffsapp-page-exp..ntime.dev.jsDiff too large to display app-page-exp..time.prod.jsfailed to diffapp-page-tur..ntime.dev.jsDiff too large to display app-page-tur..time.prod.jsfailed to diffapp-page-tur..ntime.dev.jsDiff too large to display app-page-tur..time.prod.jsDiff too large to display app-page.runtime.dev.jsDiff too large to display app-page.runtime.prod.jsDiff too large to display 📎 Tarball URLCommit: e4a0892 |
gaearon
force-pushed
the
gaearon-replay-missed-traversal
branch
2 times, most recently
from
July 27, 2026 02:04
0ae0d28 to
56baf14
Compare
Chrome keeps same-document history association across a reload: after navigating Home -> Post and reloading, pressing Back before hydration is an instant same-document traversal that no popstate listener observes. The URL changes but the router never learns about it, so URL and content stay permanently out of sync. The suite covers the reconciliation scenarios (path change and search-param-only change) and the constraints any fix must preserve (third-party pushState adoption, in-page anchor jumps, pushState followed by back, third-party writes landing before the replay, traversals onto third-party entries) — each with and without a Suspense boundary above the page. The four reconciliation tests fail in every configuration (dev and start, with and without cacheComponents); the twelve constraint tests pass without a fix.
gaearon
force-pushed
the
gaearon-replay-missed-traversal
branch
from
August 5, 2026 16:26
56baf14 to
441526f
Compare
gaearon
marked this pull request as ready for review
August 5, 2026 16:35
gaearon
force-pushed
the
gaearon-replay-missed-traversal
branch
from
August 5, 2026 16:46
441526f to
d970c67
Compare
ztanner
reviewed
Aug 5, 2026
A traversal can land between the first commit (where the initial history write is skipped) and the effect that installs the popstate listener: detection had already concluded at commit time, the listener does not exist yet, and the traversal would never be replayed. The detection is not tied to a point in time: the activation entry is fixed for the document's lifetime and entry keys are stable across replaceState, so a key mismatch means an unobserved traversal at any point until the listener is installed. Extract it into hasMissedTraversal() and evaluate it independently at both sites instead of carrying the first commit's answer across the gap. Each site keeps its own once-per-document latch, since the predicate stays true for the rest of the session after any observed navigation. This also handles a traversal back to the activation entry during the gap: re-evaluation sees matching keys and correctly skips the replay, where the carried answer would have replayed an entry that is already rendered. There is no deterministic test for the closed window — it requires a traversal whose queued popstate dispatches between two React effect flushes. The existing suite covers both sites' behavior in all reachable-on-demand scenarios.
gaearon
force-pushed
the
gaearon-replay-missed-traversal
branch
from
August 5, 2026 20:27
3800b6a to
e4a0892
Compare
ztanner
approved these changes
Aug 5, 2026
Contributor
|
Confirmed working fine with the initial nuqs regression test in #95848, thanks! |
m-kawafuji
pushed a commit
to m-kawafuji/next.js
that referenced
this pull request
Aug 8, 2026
Relands vercel#95682 (reverted in vercel#95853 due to vercel#95848). This contains: - A bunch of tests (some failing and some passing before this PR; all passing after this PR). - The fix from vercel#95682. In short, we use Navigation API to detect during hydration that we're on a different history entry than we started, and in that case, replay the missed traversal with similar logic to what we do for `onPopState`. The issue with the original PR was that under Activity, React would sometimes hang applying the state update. This was fixed in react/react#37135 and synced, so we can do this fix again now. ## Test Plan Old and new tests.
gaearon
added a commit
that referenced
this pull request
Aug 20, 2026
The router learns about history changes from its popstate listener and its pushState/replaceState wrappers, both installed in an effect after hydration. Until then a Back press, an anchor click, or a third-party write goes unobserved, and the router's first history write stamps the payload's state onto whatever entry is current. #96252 recovered the Back case by comparing Navigation API entry keys, which only works in browsers that implement the Navigation API and only for traversals. Replace the inference with observation. A small inline script, emitted by React with the shell ahead of the bootstrap scripts, marks the entry the document was activated on, wraps pushState/replaceState so entries written before hydration inherit that entry's router state, and records whether any history change happened. The marker carries a per-document token so an entry marked by an earlier document is not mistaken for one this document can render. On the client, the router initializes from the activation URL rather than window.location, so hydration matches the server HTML even if the URL has changed. If history changed, HistoryUpdater skips its write and the Router effect hands the current entry off the same way a popstate onto it would be handled: an entry with a router tree is restored, an entry derived from the activation entry (or any entry on the activation route) renders the initial payload, and an entry the router knows nothing about is adopted, as the first write used to do. The bootstrap wrappers are removed at handoff unless application code wrapped them in turn. Traversals that happen while the shell is still streaming, before the inline script runs, are not observed. They fall back to the pre-#96252 behavior.
gaearon
added a commit
that referenced
this pull request
Aug 20, 2026
The router learns about history changes from its popstate listener and its pushState/replaceState wrappers, both installed in an effect after hydration. Until then a Back press, an anchor click, or a third-party write goes unobserved, and the router's first history write stamps the payload's state onto whatever entry is current. #96252 recovered the Back case by comparing Navigation API entry keys, which only works in browsers that implement the Navigation API and only for traversals. Replace the inference with observation. A small inline script, emitted by React with the shell ahead of the bootstrap scripts, marks the entry the document was activated on, wraps pushState/replaceState so entries written before hydration inherit that entry's router state, and records whether any history change happened. The marker carries a per-document token so an entry marked by an earlier document is not mistaken for one this document can render. On the client, the router initializes from the activation URL rather than window.location, so hydration matches the server HTML even if the URL has changed. If history changed, HistoryUpdater skips its write and the Router effect hands the current entry off the way a popstate onto it would be handled: an entry on the activation route renders the initial payload whatever its state holds, an entry with a router tree restores that tree, an entry derived from the activation entry renders the initial payload, and an entry the router knows nothing about is adopted, as the first write used to do. The bootstrap wrappers are removed at handoff unless application code wrapped them in turn. The inline script adds about 1KB gzipped to every App Router document. Traversals that happen while the shell is still streaming, before the script runs, are not observed and fall back to the pre-#96252 behavior.
gaearon
added a commit
that referenced
this pull request
Aug 20, 2026
The router learns about history changes from its popstate listener and its pushState/replaceState wrappers, both installed in an effect after hydration. Until then a Back press, an anchor click, or a third-party write goes unobserved, and the router's first history write stamps the payload's state onto whatever entry is current. #96252 recovered the Back case by comparing Navigation API entry keys, which only works in browsers that implement the Navigation API and only for traversals. Replace the inference with observation. A small inline script, emitted by React with the shell ahead of the bootstrap scripts, marks the entry the document was activated on, wraps pushState/replaceState so entries written before hydration inherit that entry's router state, and records whether any history change happened. The marker carries a per-document token so an entry marked by an earlier document is not mistaken for one this document can render. On the client, the router initializes from the activation URL rather than window.location, so hydration matches the server HTML even if the URL has changed. If history changed, HistoryUpdater skips its write and the Router effect hands the current entry off the way a popstate onto it would be handled: an entry on the activation route renders the initial payload whatever its state holds, an entry with a router tree restores that tree, an entry derived from the activation entry renders the initial payload, and an entry the router knows nothing about is adopted, as the first write used to do. The bootstrap wrappers are removed at handoff unless application code wrapped them in turn. Seven scenarios in the back-before-hydration suite change outcome: the Back case now passes without the Navigation API; early entries created by third-party writes stay traversable instead of reloading; a write that lands during hydration, or after an early traversal, renders the page for the entry it was written from; and an entry marked by an earlier document is no longer rendered with this document's payload. The inline script adds about 1KB gzipped to every App Router document. Traversals that happen while the shell is still streaming, before the script runs, are not observed and fall back to the pre-#96252 behavior.
gaearon
added a commit
that referenced
this pull request
Aug 20, 2026
The router learns about history changes from its popstate listener and its pushState/replaceState wrappers, both installed in an effect after hydration. Until then a Back press, an anchor click, or a third-party write goes unobserved, and the router's first history write stamps the payload's state onto whatever entry is current. #96252 recovered the Back case by comparing Navigation API entry keys, which only works in browsers that implement the Navigation API and only for traversals. Replace the inference with observation. A small inline script, emitted by React with the shell ahead of the bootstrap scripts, marks the entry the document was activated on, wraps pushState/replaceState so entries written before hydration inherit that entry's router state, and records whether any history change happened. The marker carries a per-document token so an entry marked by an earlier document is not mistaken for one this document can render. On the client, the router initializes from the activation URL rather than window.location, so hydration matches the server HTML even if the URL has changed. If history changed, HistoryUpdater skips its write and the Router effect hands the current entry off the way a popstate onto it would be handled: an entry on the activation route renders the initial payload whatever its state holds, an entry with a router tree restores that tree, an entry derived from the activation entry renders the initial payload, and an entry the router knows nothing about is adopted, as the first write used to do. The bootstrap wrappers are removed at handoff unless application code wrapped them in turn. Seven scenarios in the back-before-hydration suite change outcome: the Back case now passes without the Navigation API; early entries created by third-party writes stay traversable instead of reloading; a write that lands during hydration, or after an early traversal, renders the page for the entry it was written from; and an entry marked by an earlier document is no longer rendered with this document's payload. The inline script adds about 1KB gzipped to every App Router document. Traversals that happen while the shell is still streaming, before the script runs, are not observed and fall back to the pre-#96252 behavior.
gaearon
added a commit
that referenced
this pull request
Aug 20, 2026
The router learns about history changes from its popstate listener and its pushState/replaceState wrappers, both installed in an effect after hydration. Until then a Back press, an anchor click, or a third-party write goes unobserved, and the router's first history write stamps the payload's state onto whatever entry is current. #96252 recovered the Back case by comparing Navigation API entry keys, which only works in browsers that implement the Navigation API and only for traversals. Replace the inference with observation. A small inline script, emitted by React with the shell ahead of the bootstrap scripts, marks the entry the document was activated on, wraps pushState/replaceState so entries written before hydration inherit that entry's router state, and records whether any history change happened. The marker carries a per-document token so an entry marked by an earlier document is not mistaken for one this document can render. On the client, the router initializes from the activation URL rather than window.location, so hydration matches the server HTML even if the URL has changed. If history changed, HistoryUpdater skips its write and the Router effect hands the current entry off the way a popstate onto it would be handled: an entry on the activation route renders the initial payload whatever its state holds, an entry with a router tree restores that tree, an entry derived from the activation entry renders the initial payload, and an entry the router knows nothing about is adopted, as the first write used to do. The bootstrap wrappers are removed at handoff unless application code wrapped them in turn. Seven scenarios in the back-before-hydration suite change outcome: the Back case now passes without the Navigation API; early entries created by third-party writes stay traversable instead of reloading; a write that lands during hydration, or after an early traversal, renders the page for the entry it was written from; and an entry marked by an earlier document is no longer rendered with this document's payload. The inline script adds about 1KB gzipped to every App Router document. Traversals that happen while the shell is still streaming, before the script runs, are not observed and fall back to the pre-#96252 behavior.
gaearon
added a commit
that referenced
this pull request
Aug 21, 2026
The router only starts listening to history in an effect after hydration. #96252 handles a Back press before that by comparing Navigation API entry keys at hydration time, which only works in Chromium. It also initializes the router from window.location, which after such a Back press is the wrong URL: usePathname() disagrees with the server HTML and React regenerates the tree on the client. Record the URL the document was created for in an inline script that React emits with the shell, before anything else runs. Use the Navigation API's activation entry where available, since that is right even if history moved before the script ran, and location.href otherwise. Initialize the router from that URL. Then "history moved before the router was listening" is just "the current URL differs from the one the router started on", which works in every browser and is checked at the first history write and again when the listener is installed, so a traversal during hydration is seen too. The first write is skipped in that case and the Router effect catches up: a router-written entry is restored like a popstate, any other entry is adopted at its URL, as the first write used to do. The three TODOs from the prep PR are resolved: hydration matches the server HTML after a Back press, and a third-party write that lands during hydration is picked up. A Back press before the inline script runs is handled only where the Navigation API exists, as before. The script is 150 bytes.
gaearon
added a commit
that referenced
this pull request
Aug 21, 2026
The router only starts listening to history in an effect after hydration. #96252 handles a Back press before that by comparing Navigation API entry keys at hydration time, which only works in Chromium. It also initializes the router from window.location, which after such a Back press is the wrong URL: usePathname() disagrees with the server HTML and React regenerates the tree on the client. Record the URL the document was created for in an inline script that React emits with the shell, before anything else runs. Use the Navigation API's activation entry where available, since that is right even if history moved before the script ran, and location.href otherwise. Initialize the router from that URL. Then "history moved before the router was listening" is just "the current URL differs from the one the router started on", which works in every browser and is checked at the first history write and again when the listener is installed, so a traversal during hydration is seen too. The first write is skipped in that case and the Router effect catches up: a router-written entry is restored like a popstate, any other entry is adopted at its URL, as the first write used to do. The three TODOs from the prep PR are resolved: hydration matches the server HTML after a Back press, and a third-party write that lands during hydration is picked up. A Back press before the inline script runs is handled only where the Navigation API exists, as before. The script is 150 bytes.
gaearon
added a commit
that referenced
this pull request
Aug 21, 2026
The router only starts listening to history in an effect after hydration. #96252 handles a Back press before that by comparing Navigation API entry keys at hydration time, which only works in Chromium. It also initializes the router from window.location, which after such a Back press is the wrong URL: usePathname() disagrees with the server HTML and React regenerates the tree on the client. Record the URL the document was created for in an inline script that React emits with the shell, before anything else runs. Use the Navigation API's activation entry where available, since that is right even if history moved before the script ran, and location.href otherwise. Initialize the router from that URL. Then "history moved before the router was listening" is just "the current URL differs from the one the router started on", which works in every browser and is checked at the first history write and again when the listener is installed, so a traversal during hydration is seen too. The first write is skipped in that case and the Router effect catches up: a router-written entry is restored like a popstate, any other entry is adopted at its URL, as the first write used to do. The TODOs from #97651 are resolved: hydration matches the server HTML after a Back press, and a third-party write that lands during hydration is picked up. A Back press before the inline script runs is handled only where the Navigation API exists, as before. The script is 150 bytes.
gaearon
added a commit
that referenced
this pull request
Aug 21, 2026
The router only starts listening to history in an effect after hydration. #96252 handles a Back press before that by comparing Navigation API entry keys at hydration time, which only works in Chromium. It also initializes the router from window.location, which after such a Back press is the wrong URL: usePathname() disagrees with the server HTML and React regenerates the tree on the client. Record the URL the document was created for in an inline script that React emits with the shell, before anything else runs. Use the Navigation API's activation entry where available, since that is right even if history moved before the script ran, and location.href otherwise. Initialize the router from that URL. Then "history moved before the router was listening" is just "the current URL differs from the one the router started on", which works in every browser and is checked at the first history write and again when the listener is installed, so a traversal during hydration is seen too. The first write is skipped in that case and the Router effect catches up: a router-written entry is restored like a popstate, any other entry is adopted at its URL, as the first write used to do. The TODOs from #97651 are resolved: hydration matches the server HTML after a Back press, and a third-party write that lands during hydration is picked up. A Back press before the inline script runs is handled only where the Navigation API exists, as before. The script is 170 bytes. It is wrapped in try/catch and placed after the chunk bootstrap, since without the Navigation API `self.navigation` can resolve to a frame with that name and throw.
gaearon
added a commit
that referenced
this pull request
Aug 21, 2026
The router only starts listening to history in an effect after hydration. #96252 handles a Back press before that by comparing Navigation API entry keys at hydration time, which only works in Chromium. It also initializes the router from window.location, which after such a Back press is the wrong URL: usePathname() disagrees with the server HTML and React regenerates the tree on the client. Record the URL the document was created for in an inline script that React emits with the shell, before anything else runs. Use the Navigation API's activation entry where available, since that is right even if history moved before the script ran, and location.href otherwise. Initialize the router from that URL. Then "history moved before the router was listening" is just "the current URL differs from the one the router started on", which works in every browser and is checked at the first history write and again when the listener is installed, so a traversal during hydration is seen too. The first write is skipped in that case and the Router effect catches up: a router-written entry is restored like a popstate, any other entry is adopted at its URL, as the first write used to do. The TODOs from #97651 are resolved: hydration matches the server HTML after a Back press, and a third-party write that lands during hydration is picked up. A Back press before the inline script runs is handled only where the Navigation API exists, as before. The script is 200 bytes. It only reads from a real Navigation instance, since `self.navigation` may be a frame or an app object by that name, and nothing in it or in reading the value may throw, since hydration is bootstrapped in the same script and must not depend on this.
gaearon
added a commit
that referenced
this pull request
Aug 21, 2026
The router only starts listening to history in an effect after hydration. #96252 handles a Back press before that by comparing Navigation API entry keys at hydration time, which only works in Chromium. It also initializes the router from window.location, which after such a Back press is the wrong URL: usePathname() disagrees with the server HTML and React regenerates the tree on the client. Record the URL the document was created for in an inline script that React emits with the shell, before anything else runs. Use the Navigation API's activation entry where available, since that is right even if history moved before the script ran, and location.href otherwise. Initialize the router from that URL. Then "history moved before the router was listening" is just "the current URL differs from the one the router started on", which works in every browser and is checked at the first history write and again when the listener is installed, so a traversal during hydration is seen too. The first write is skipped in that case and the Router effect catches up: a router-written entry is restored like a popstate, any other entry is adopted at its URL, as the first write used to do. The TODOs from #97651 are resolved: hydration matches the server HTML after a Back press, and a third-party write that lands during hydration is picked up. A Back press before the inline script runs is handled only where the Navigation API exists, as before. The script is 200 bytes. It only reads from a real Navigation instance, since `self.navigation` may be a frame or an app object by that name, and nothing in it or in reading the value may throw, since hydration is bootstrapped in the same script and must not depend on this.
gaearon
added a commit
that referenced
this pull request
Aug 22, 2026
The router only starts listening to history in an effect after hydration. #96252 handles a Back press before that by comparing Navigation API entry keys at hydration time, which only works in Chromium. It also initializes the router from window.location, which after such a Back press is the wrong URL: usePathname() disagrees with the server HTML and React regenerates the tree on the client. Record the URL the document was created for in an inline script that React emits with the shell, before anything else runs. Use the Navigation API's activation entry where available, since that is right even if history moved before the script ran, and location.href otherwise. Initialize the router from that URL. Then "history moved before the router was listening" is just "the current URL differs from the one the router started on", which works in every browser and is checked at the first history write and again when the listener is installed, so a traversal during hydration is seen too. The first write is skipped in that case and the Router effect catches up: a router-written entry is restored like a popstate, any other entry is adopted at its URL, as the first write used to do. The TODOs from #97651 are resolved: hydration matches the server HTML after a Back press, and a third-party write that lands during hydration is picked up. A Back press before the inline script runs is handled only where the Navigation API exists, as before. The script is 200 bytes. It only reads from a real Navigation instance, since `self.navigation` may be a frame or an app object by that name, and nothing in it or in reading the value may throw, since hydration is bootstrapped in the same script and must not depend on this.
gaearon
added a commit
that referenced
this pull request
Aug 22, 2026
The router only starts listening to history in an effect after hydration. #96252 handles a Back press before that by comparing Navigation API entry keys at hydration time, and only for entries the router wrote. It also initializes the router from window.location, which after such a Back press is the wrong URL: usePathname() disagrees with the server HTML and React regenerates the tree on the client. Initialize the router from the URL the document was created for instead: the Navigation API's activation entry where available, and window.location otherwise. Then "history moved before the router was listening" is just "the current URL differs from the one the router started on". This is checked at the first history write and again when the listener is installed, so a traversal during hydration is seen too. The first write is skipped in that case and the Router effect catches up: a router-written entry is restored like a popstate, any other entry is adopted at its URL, as the first write used to do. The TODOs from #97651 are resolved: hydration matches the server HTML after a Back press, and a third-party write that lands during hydration is picked up. `window.navigation` is only read when it is a real Navigation instance, since a frame or a global by that name can take its place, and reading it must not break hydration.
gaearon
added a commit
that referenced
this pull request
Aug 22, 2026
The router only starts listening to history in an effect after hydration. #96252 handles a Back press before that by comparing Navigation API entry keys at hydration time, which only works in Chromium. It also initializes the router from window.location, which after such a Back press is the wrong URL: usePathname() disagrees with the server HTML and React regenerates the tree on the client. Record the URL the document was created for in an inline script that React emits with the shell, before anything else runs. Use the Navigation API's activation entry where available, since that is right even if history moved before the script ran, and location.href otherwise. Initialize the router from that URL. Then "history moved before the router was listening" is just "the current URL differs from the one the router started on", which works in every browser and is checked at the first history write and again when the listener is installed, so a traversal during hydration is seen too. The first write is skipped in that case and the Router effect catches up: a router-written entry is restored like a popstate, any other entry is adopted at its URL, as the first write used to do. The TODOs from #97651 are resolved: hydration matches the server HTML after a Back press, and a third-party write that lands during hydration is picked up. A Back press before the inline script runs is handled only where the Navigation API exists, as before. The script is 214 bytes. It only reads from a real Navigation instance, since `self.navigation` may be a frame or an app object by that name, and nothing in it or in reading the value may throw, since hydration is bootstrapped in the same script and must not depend on this.
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 subscribe to this conversation on GitHub.
Already have an account?
Sign in.
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.
Relands #95682 (reverted in #95853 due to #95848).
This contains:
In short, we use Navigation API to detect during hydration that we're on a different history entry than we started, and in that case, replay the missed traversal with similar logic to what we do for
onPopState.The issue with the original PR was that under Activity, React would sometimes hang applying the state update. This was fixed in react/react#37135 and synced, so we can do this fix again now.
Test Plan
Old and new tests.