Skip to content

Fix race when navigating Back before hydration - #96252

Merged
gaearon merged 3 commits into
canaryfrom
gaearon-replay-missed-traversal
Aug 5, 2026
Merged

gaearon merged 3 commits into
canaryfrom
gaearon-replay-missed-traversal

Conversation

@gaearon

@gaearon gaearon commented Jul 27, 2026 •

Copy link
Copy Markdown
Contributor

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.

@github-actions

github-actions Bot commented Jul 27, 2026 •

Copy link
Copy Markdown
Contributor

Tests Passed

Commit: e4a0892

@github-actions

github-actions Bot commented Jul 27, 2026 •

Copy link
Copy Markdown
Contributor

Stats from current PR

🔴 3 regressions

Metric Canary PR Change Trend
node_modules Size 552 MB 552 MB 🔴 +60.2 kB (+0%) ▁▁▁▁▁
Webpack Build Time 23.803s 24.393s 🔴 +590ms (+2%) ▄▁▁▄▂
Webpack Warm (First Request) 3.350s 3.535s 🔴 +185ms (+6%) ▅▁▃▃▂
📊 All Metrics
📖 Metrics Glossary

Dev Server Metrics:

  • Listen = TCP port starts accepting connections
  • First Request = HTTP server returns successful response
  • Cold = Fresh build (no cache)
  • Warm = With cached build artifacts

Build Metrics:

  • Fresh = Clean build (no .next directory)
  • Cached = With existing .next directory

Change Thresholds:

  • Time: Changes < 50ms AND < 10%, OR < 2% are insignificant
  • Size: Changes < 1KB AND < 1% are insignificant
  • All other changes are flagged to catch regressions

⚡ Dev Server

Metric Canary PR Change Trend
Cold (Listen) 812ms 814ms ✓ █▁▁▁▁
Cold (Ready in log) 793ms 794ms ✓ █▁▁▁▁
Cold (First Request) 1.307s 1.316s ✓ █▂▁▁▂
Warm (Listen) 814ms 813ms ✓ █▁▁▁▁
Warm (Ready in log) 789ms 804ms ✓ █▁▁▁▁
Warm (First Request) 1.301s 1.352s ✓ █▂▁▁▁
📦 Dev Server (Webpack) (Legacy)

📦 Dev Server (Webpack)

Metric Canary PR Change Trend
Cold (Listen) 813ms 812ms ✓ █▁███
Cold (Ready in log) 775ms 779ms ✓ ▆▁▄▅▃
Cold (First Request) 3.360s 3.401s ✓ ▅▁▅▂▁
Warm (Listen) 810ms 813ms ✓ ▅▁▅▅▄
Warm (Ready in log) 775ms 804ms ✓ ▄▁▃▄▃
Warm (First Request) 3.350s 3.535s 🔴 +185ms (+6%) ▅▁▃▃▂

⚡ Production Builds

Metric Canary PR Change Trend
Fresh Build 5.808s 5.759s ✓ █▄▂▂▃
Cached Build 2.953s 3.035s ✓ █▂▂▂▂
📦 Production Builds (Webpack) (Legacy)

📦 Production Builds (Webpack)

Metric Canary PR Change Trend
Fresh Build 23.803s 24.393s 🔴 +590ms (+2%) ▄▁▁▄▂
Cached Build 24.167s 24.209s ✓ ▅▁▁▅▂
node_modules Size 552 MB 552 MB 🔴 +60.2 kB (+0%) ▁▁▁▁▁
📦 Bundle Sizes

Bundle Sizes

⚡ Turbopack

Client

Main Bundles
Canary PR Change
00mwvyrg6ixvj.js gzip 160 B N/A -
0a-p0meqxynjz.js gzip 10.3 kB N/A -
0avtke4yefj4-.js gzip 13.1 kB N/A -
0cz1d0mv5g_q7.js gzip 39.4 kB 39.4 kB ✓
0du-x7rwsiheh.js gzip 71.4 kB N/A -
0e67499aes59m.js gzip 153 B N/A -
0ecxt13uk5yi5.js gzip 160 B N/A -
0eh7mct33fy2t.js gzip 65.6 kB N/A -
0g_a4a3kvjhcb.js gzip 170 B N/A -
0hpff96vwf5wg.js gzip 157 B N/A -
0kq_31m4cy1yc.js gzip 156 B N/A -
0pt5of3ch6d2u.js gzip 13.6 kB N/A -
0tth87xhgvqg0.js gzip 154 B N/A -
0xnsdo-6y9r3k.js gzip 5.72 kB N/A -
0yi7is8u4covm.js gzip 8.77 kB N/A -
1biacj8n3mamp.js gzip 155 B N/A -
1elt1qium-r2m.css gzip 115 B 115 B ✓
1fvcy6mevca8_.js gzip 155 B N/A -
1prv-wg6jgtt2.js gzip 8.77 kB N/A -
1t47vod02e_4a.js gzip 8.75 kB N/A -
1tf1phijqlx9j.js gzip 220 B 220 B ✓
1tw002sq5bo4l.js gzip 10 kB N/A -
1wlerik1n7zbr.js gzip 8.7 kB N/A -
2jpx0_6n0ykra.js gzip 3.66 kB N/A -
2pevdqjpbf3xk.js gzip 9.45 kB N/A -
2qd2anu9eizlz.js gzip 8.75 kB N/A -
2sv39ae5_8vfj.js gzip 8.81 kB N/A -
2utx6n7w65xqa.js gzip 8.7 kB N/A -
3-ukivglqkfeq.js gzip 8.78 kB N/A -
3044_8tfjfw1i.js gzip 158 B N/A -
325xisqrylugu.js gzip 450 B N/A -
34f111jded263.js gzip 7.53 kB N/A -
3f2c3tys-p419.js gzip 46.3 kB N/A -
3fc7bkh7rw3rf.js gzip 155 B N/A -
3icg_udga0idh.js gzip 156 B N/A -
3jc9ghvds1spf.js gzip 2.29 kB N/A -
3juwkvkgvywfe.js gzip 10.6 kB N/A -
3kbove7q58uqh.js gzip 13.2 kB N/A -
3mgnie1ovhj25.js gzip 155 B N/A -
3qn7vntp30gaj.js gzip 1.47 kB N/A -
turbopack-07..gjbk.js gzip 3.86 kB N/A -
turbopack-0e..8-70.js gzip 3.86 kB N/A -
turbopack-0m..l2wg.js gzip 3.86 kB N/A -
turbopack-0p..d0xe.js gzip 3.88 kB N/A -
turbopack-0t..uhga.js gzip 3.86 kB N/A -
turbopack-1_..ll45.js gzip 3.86 kB N/A -
turbopack-13..1gyq.js gzip 3.86 kB N/A -
turbopack-1e..w26b.js gzip 3.86 kB N/A -
turbopack-1m..hraf.js gzip 3.86 kB N/A -
turbopack-2h..2ozq.js gzip 3.86 kB N/A -
turbopack-2z..t6me.js gzip 3.86 kB N/A -
turbopack-36..5e0r.js gzip 3.84 kB N/A -
turbopack-3r..lj85.js gzip 3.86 kB N/A -
turbopack-41..5277.js gzip 3.86 kB N/A -
0_sfs0nf236wo.js gzip N/A 3.65 kB -
030a1njeh5b77.js gzip N/A 10 kB -
06s8o-lue6t9e.js gzip N/A 155 B -
0fg7be2iavl6n.js gzip N/A 168 B -
0inldqi6r-n5r.js gzip N/A 156 B -
0ot0qt5np20h7.js gzip N/A 8.78 kB -
0qitnimre00_k.js gzip N/A 156 B -
0uz1o-lwrim3w.js gzip N/A 157 B -
12m9rr7_7cexd.js gzip N/A 8.7 kB -
12ofo5lnnnsfs.js gzip N/A 156 B -
17hod4realhn2.js gzip N/A 161 B -
186r3y_rfvr0b.js gzip N/A 8.78 kB -
19v97848yrjp7.js gzip N/A 8.81 kB -
1nd_0rwgz2ozk.js gzip N/A 9.45 kB -
1oi9pltr_e10m.js gzip N/A 10.6 kB -
1rd9tzqgzsz2w.js gzip N/A 8.77 kB -
1rq1g_cwv642d.js gzip N/A 10.3 kB -
1uojw_uabeg93.js gzip N/A 450 B -
1w-7lusgyl81y.js gzip N/A 8.7 kB -
21h56wdn9zddo.js gzip N/A 8.75 kB -
2auhrzpzovkv5.js gzip N/A 46.3 kB -
2g6iw6c8ad64g.js gzip N/A 65.6 kB -
2ovff1533zvno.js gzip N/A 13.2 kB -
2s4z8jy0z7v93.js gzip N/A 1.46 kB -
2tm97fjdke0kt.js gzip N/A 7.54 kB -
2y6gb5bs4mnam.js gzip N/A 8.75 kB -
341h71ln1jpl3.js gzip N/A 2.29 kB -
34ttpt_n7605h.js gzip N/A 5.72 kB -
35qkezu10liph.js gzip N/A 162 B -
36ckfq9o-ys-a.js gzip N/A 13.1 kB -
3em63fx-8w_gc.js gzip N/A 156 B -
3n6jn-wxtjx-h.js gzip N/A 71.6 kB -
3nih04zcyby0u.js gzip N/A 156 B -
3ocgf72p_k0py.js gzip N/A 153 B -
3rk7h_t2arfg9.js gzip N/A 155 B -
3vdh6_u50pu5t.js gzip N/A 13.6 kB -
3zbdf4igr_egf.js gzip N/A 157 B -
turbopack-07..ddhi.js gzip N/A 3.86 kB -
turbopack-09..yxdx.js gzip N/A 3.86 kB -
turbopack-0n..ctrg.js gzip N/A 3.86 kB -
turbopack-0q..89jt.js gzip N/A 3.86 kB -
turbopack-10..mgpj.js gzip N/A 3.86 kB -
turbopack-2-..8tn0.js gzip N/A 3.86 kB -
turbopack-28..ieeq.js gzip N/A 3.84 kB -
turbopack-2f..1-rn.js gzip N/A 3.86 kB -
turbopack-2o..s9tc.js gzip N/A 3.86 kB -
turbopack-3_..3ik3.js gzip N/A 3.86 kB -
turbopack-30..6s0l.js gzip N/A 3.86 kB -
turbopack-3c..d5b2.js gzip N/A 3.86 kB -
turbopack-3v..o7-g.js gzip N/A 3.88 kB -
turbopack-3z..4q95.js gzip N/A 3.86 kB -
Total 451 kB 451 kB ⚠️ +186 B

Server

Middleware
Canary PR Change
middleware-b..fest.js gzip 777 B 772 B ✓
Total 777 B 772 B ✅ -5 B
Build Details
Build Manifests
Canary PR Change
_buildManifest.js gzip 431 B 429 B ✓
Total 431 B 429 B ✅ -2 B
Build Cache
Canary PR Change
00000001.sst gzip 15.3 MB 14.1 MB 🟢 1.17 MB (-8%)
00000002.sst gzip 13.1 MB 15 MB 🔴 +1.96 MB (+15%)
00000003.sst gzip 13.3 MB 14.9 MB 🔴 +1.53 MB (+11%)
00000004.sst gzip 13.6 MB 11.3 MB 🟢 2.29 MB (-17%)
00000005.sst gzip 10.3 MB 10.3 MB ✓
00000006.sst gzip 2.83 MB 2.83 MB ✓
00000007.sst gzip 59 B 59 B ✓
00000008.meta gzip 89 B 89 B ✓
00000009.meta gzip 298 kB 298 kB ✓
00000010.meta gzip 299 kB 299 kB ✓
00000011.meta gzip 298 kB 298 kB ✓
00000012.sst gzip 50.7 kB 52.1 kB 🔴 +1.39 kB (+3%)
00000013.sst gzip 1.85 MB 1.9 MB 🔴 +44.2 kB (+2%)
00000014.sst gzip 59 B 59 B ✓
00000015.meta gzip 116 B 116 B ✓
00000016.meta gzip 324 kB 324 kB ✓
00000017.meta gzip 404 kB 405 kB ✓
00000018.sst gzip 51.4 kB 53.1 kB 🔴 +1.76 kB (+3%)
00000019.sst gzip 1.39 MB 1.39 MB ✓
00000020.sst gzip 59 B 59 B ✓
00000021.meta gzip 116 B 116 B ✓
00000022.meta gzip 324 kB 324 kB ✓
00000023.meta gzip 370 kB 370 kB ✓
00000024.sst gzip 51.5 kB 53.3 kB 🔴 +1.81 kB (+4%)
00000025.sst gzip 1.39 MB 1.39 MB ✓
00000026.sst gzip 59 B 59 B ✓
00000027.meta gzip 116 B 116 B ✓
00000028.meta gzip 324 kB 324 kB ✓
00000029.meta gzip 370 kB 370 kB ✓
00000030.sst gzip 50.8 kB 52.2 kB 🔴 +1.39 kB (+3%)
00000031.sst gzip 1.39 MB 1.39 MB ✓
00000032.sst gzip 59 B 59 B ✓
00000033.meta gzip 116 B 116 B ✓
00000034.meta gzip 324 kB 324 kB ✓
00000035.meta gzip 370 kB 370 kB ✓
00000036.sst gzip 51.4 kB 53.1 kB 🔴 +1.71 kB (+3%)
00000037.sst gzip 1.39 MB 1.39 MB ✓
00000038.sst gzip 59 B 59 B ✓
00000039.meta gzip 116 B 116 B ✓
00000040.meta gzip 324 kB 324 kB ✓
00000041.meta gzip 370 kB 370 kB ✓
CURRENT gzip 24 B 24 B ✓
LOG gzip 676 B 671 B ✓
Total 80.5 MB 80.6 MB ⚠️ +95.8 kB

📦 Webpack

Client

Main Bundles
Canary PR Change
3322-HASH.js gzip 65.1 kB N/A -
4191.HASH.js gzip 169 B N/A -
7920-HASH.js gzip 4.68 kB N/A -
9784-HASH.js gzip 5.63 kB N/A -
b1ad9f4c-HASH.js gzip 63.2 kB N/A -
framework-HASH.js gzip 59.7 kB 59.7 kB ✓
main-app-HASH.js gzip 254 B 253 B ✓
main-HASH.js gzip 40 kB 40 kB ✓
webpack-HASH.js gzip 1.68 kB 1.68 kB ✓
3577.HASH.js gzip N/A 168 B -
578-HASH.js gzip N/A 66 kB -
8590-HASH.js gzip N/A 5.61 kB -
9750-HASH.js gzip N/A 4.69 kB -
a8984546-HASH.js gzip N/A 63.2 kB -
Total 240 kB 241 kB ⚠️ +839 B
Polyfills
Canary PR Change
polyfills-HASH.js gzip 39.4 kB 39.4 kB ✓
Total 39.4 kB 39.4 kB ✓
Pages
Canary PR Change
_app-HASH.js gzip 194 B 193 B ✓
_error-HASH.js gzip 181 B 182 B ✓
css-HASH.js gzip 334 B 331 B ✓
dynamic-HASH.js gzip 1.81 kB 1.81 kB ✓
edge-ssr-HASH.js gzip 255 B 253 B ✓
head-HASH.js gzip 349 B 351 B ✓
hooks-HASH.js gzip 382 B 384 B ✓
image-HASH.js gzip 581 B 582 B ✓
index-HASH.js gzip 260 B 259 B ✓
link-HASH.js gzip 2.48 kB 2.48 kB ✓
routerDirect..HASH.js gzip 317 B 318 B ✓
script-HASH.js gzip 384 B 386 B ✓
withRouter-HASH.js gzip 316 B 315 B ✓
1afbb74e6ecf..834.css gzip 106 B 106 B ✓
Total 7.95 kB 7.95 kB ⚠️ +1 B

Server

Edge SSR
Canary PR Change
edge-ssr.js gzip 129 kB 128 kB ✓
page.js gzip 294 kB 294 kB ✓
Total 422 kB 423 kB ⚠️ +249 B
Middleware
Canary PR Change
middleware-b..fest.js gzip 617 B 619 B ✓
middleware-r..fest.js gzip 156 B 156 B ✓
middleware.js gzip 45.5 kB 45.5 kB ✓
edge-runtime..pack.js gzip 842 B 842 B ✓
Total 47.1 kB 47.1 kB ✅ -45 B
Build Details
Build Manifests
Canary PR Change
_buildManifest.js gzip 718 B 718 B ✓
Total 718 B 718 B ✓
Build Cache
Canary PR Change
0.pack gzip 4.87 MB 4.86 MB 🟢 9.17 kB (0%)
index.pack gzip 124 kB 122 kB 🟢 1.77 kB (-1%)
index.pack.old gzip 124 kB 121 kB 🟢 2.08 kB (-2%)
Total 5.12 MB 5.11 MB ✅ -13 kB

🔄 Shared (bundler-independent)

Runtimes
Canary PR Change
app-page-exp...dev.js gzip 374 kB 374 kB ✓
app-page-exp..prod.js gzip 206 kB 207 kB ✓
app-page-tur...dev.js gzip 373 kB 374 kB ✓
app-page-tur..prod.js gzip 206 kB 206 kB ✓
app-page-tur...dev.js gzip 370 kB 370 kB ✓
app-page-tur..prod.js gzip 204 kB 204 kB ✓
app-page.run...dev.js gzip 370 kB 370 kB ✓
app-page.run..prod.js gzip 204 kB 204 kB ✓
app-route-ex...dev.js gzip 83.2 kB 83.2 kB ✓
app-route-ex..prod.js gzip 56.4 kB 56.4 kB ✓
app-route-tu...dev.js gzip 83.2 kB 83.2 kB ✓
app-route-tu..prod.js gzip 56.4 kB 56.4 kB ✓
app-route-tu...dev.js gzip 82.8 kB 82.8 kB ✓
app-route-tu..prod.js gzip 56.2 kB 56.2 kB ✓
app-route.ru...dev.js gzip 82.8 kB 82.8 kB ✓
app-route.ru..prod.js gzip 56.2 kB 56.2 kB ✓
dev-validati...dev.js gzip 133 kB 133 kB ✓
dev-validati...dev.js gzip 133 kB 133 kB ✓
dev-validati...dev.js gzip 131 kB 131 kB ✓
dev-validati...dev.js gzip 131 kB 131 kB ✓
dist_client_...dev.js gzip 324 B 324 B ✓
dist_client_...dev.js gzip 326 B 326 B ✓
dist_client_...dev.js gzip 318 B 318 B ✓
dist_client_...dev.js gzip 317 B 317 B ✓
pages-api-tu...dev.js gzip 46 kB 46 kB ✓
pages-api-tu..prod.js gzip 34.3 kB 34.3 kB ✓
pages-api.ru...dev.js gzip 45.9 kB 45.9 kB ✓
pages-api.ru..prod.js gzip 34.3 kB 34.3 kB ✓
pages-turbo....dev.js gzip 54.7 kB 54.7 kB ✓
pages-turbo...prod.js gzip 39.8 kB 39.8 kB ✓
pages.runtim...dev.js gzip 54.7 kB 54.7 kB ✓
pages.runtim..prod.js gzip 39.8 kB 39.8 kB ✓
server.runti..prod.js gzip 66.9 kB 66.9 kB ✓
use-cache-pr...dev.js gzip 72.3 kB 72.3 kB ✓
use-cache-pr...dev.js gzip 72.3 kB 72.3 kB ✓
use-cache-pr...dev.js gzip 70.6 kB 70.6 kB ✓
use-cache-pr...dev.js gzip 70.6 kB 70.6 kB ✓
Total 4.1 MB 4.1 MB ⚠️ +1.21 kB
📝 Changed Files (8 files)

Files with changes:

  • app-page-exp..ntime.dev.js
  • app-page-exp..time.prod.js
  • app-page-tur..ntime.dev.js
  • app-page-tur..time.prod.js
  • app-page-tur..ntime.dev.js
  • app-page-tur..time.prod.js
  • app-page.runtime.dev.js
  • app-page.runtime.prod.js
View diffs
app-page-exp..ntime.dev.js

Diff too large to display

app-page-exp..time.prod.js
failed to diff
app-page-tur..ntime.dev.js

Diff too large to display

app-page-tur..time.prod.js
failed to diff
app-page-tur..ntime.dev.js

Diff too large to display

app-page-tur..time.prod.js

Diff too large to display

app-page.runtime.dev.js

Diff too large to display

app-page.runtime.prod.js

Diff too large to display

📎 Tarball URL
https://vercel-packages.vercel.app/next/commits/e4a0892dd0cef6cf83d818d20664319f357d9245/next

Commit: e4a0892

@gaearon
gaearon force-pushed the gaearon-replay-missed-traversal branch 2 times, most recently from 0ae0d28 to 56baf14 Compare July 27, 2026 02:04
@gaearon
gaearon changed the base branch from fix/isolated-install-overrides to canary July 27, 2026 02:04
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
gaearon force-pushed the gaearon-replay-missed-traversal branch from 56baf14 to 441526f Compare August 5, 2026 16:26
@gaearon
gaearon marked this pull request as ready for review August 5, 2026 16:35
@gaearon gaearon changed the title Replay same-document traversals that happen before hydration Fix race when navigating Back Aug 5, 2026
@gaearon gaearon changed the title Fix race when navigating Back Fix race when navigating Back before hydration Aug 5, 2026
@gaearon
gaearon force-pushed the gaearon-replay-missed-traversal branch from 441526f to d970c67 Compare August 5, 2026 16:46
@gaearon
gaearon requested review from timneutkens and ztanner August 5, 2026 18:21
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
gaearon force-pushed the gaearon-replay-missed-traversal branch from 3800b6a to e4a0892 Compare August 5, 2026 20:27
@gaearon
gaearon merged commit 0fd2e51 into canary Aug 5, 2026
131 checks passed
@gaearon
gaearon deleted the gaearon-replay-missed-traversal branch August 5, 2026 21:39
@franky47

franky47 commented Aug 7, 2026

Copy link
Copy Markdown
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.
@github-actions github-actions Bot added the locked label Sep 5, 2026
@github-actions github-actions Bot locked as resolved and limited conversation to collaborators Sep 5, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants