Skip to content

Improve "Back before hydration" test suite - #97651

Closed
gaearon wants to merge 5 commits into
canaryfrom
gaearon-history-prep
Closed

gaearon wants to merge 5 commits into
canaryfrom
gaearon-history-prep

Conversation

@gaearon

@gaearon gaearon commented Aug 20, 2026 •

Copy link
Copy Markdown
Contributor

Test-only changes.

  • Drop the Suspense version of the back-before-hydration tests. I added it to cover what turned out to be the Activity/Suspense retry bug (Blank page when pressing Back between a reload and hydration with cacheComponents (regression in 16.3.0-canary.87) #95848), which React has since fixed.
  • Wait for the document to finish parsing before changing history in those tests so the tested window is exactly "inline scripts have run, external scripts have not".
  • Render usePathname()/useSearchParams() from the fixture layout and assert the router's URL in every scenario. One of these is wrong so there's a TODO for the future.
  • Add a case where "nothing interesting happens" to ensure Back/Forward still works there.

Every scenario ran twice, once below a Suspense-wrapped layout. That
variant existed to cover the Activity/Suspense retry bug (#95848), which
React has since fixed, and the search fixture still exercises a Suspense
boundary directly. Run each scenario once against the plain routes and
delete the /suspense fixtures.
@github-actions

github-actions Bot commented Aug 20, 2026 •

Copy link
Copy Markdown
Contributor

Stats from current PR

🔴 1 regression

Metric Canary PR Change Trend
node_modules Size 552 MB 554 MB 🔴 +1.64 MB (+0%) ▁▁▁▁▁
📊 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) 814ms 814ms ✓ ▅▄▅▄▄
Cold (Ready in log) 792ms 801ms ✓ ▆▂▄▂▂
Cold (First Request) 1.264s 1.332s ✓ ▇▁▅▃▂
Warm (Listen) 812ms 813ms ✓ █▁███
Warm (Ready in log) 792ms 797ms ✓ █▂▄▄▄
Warm (First Request) 1.253s 1.319s ✓ █▁▃▂▂
📦 Dev Server (Webpack) (Legacy)

📦 Dev Server (Webpack)

Metric Canary PR Change Trend
Cold (Listen) 814ms 813ms ✓ ▁▁█▁▁
Cold (Ready in log) 802ms 809ms ✓ ▂▃█▃▅
Cold (First Request) 3.409s 3.429s ✓ ▁▄█▂▅
Warm (Listen) 814ms 813ms ✓ ▅▅▇▃▇
Warm (Ready in log) 820ms 810ms ✓ ▃▅█▄▇
Warm (First Request) 3.404s 3.486s ✓ ▃▄█▄▇

⚡ Production Builds

Metric Canary PR Change Trend
Fresh Build 5.743s 5.741s ✓ █▂▆▂▃
Cached Build 2.949s 2.923s ✓ ▅▁▃▁▂
📦 Production Builds (Webpack) (Legacy)

📦 Production Builds (Webpack)

Metric Canary PR Change Trend
Fresh Build 25.012s 24.891s ✓ ▃▄█▃▆
Cached Build 24.812s 25.179s ✓ ▃▄█▃▆
node_modules Size 552 MB 554 MB 🔴 +1.64 MB (+0%) ▁▁▁▁▁
📦 Bundle Sizes

Bundle Sizes

⚡ Turbopack

Client

Main Bundles
Canary PR Change
0_f4fr4vbr6q1.js gzip 156 B N/A -
017_4moyv5m34.js gzip 117 B N/A -
05jocc85-b73_.js gzip 341 B N/A -
07aosl_cs3-cz.js gzip 71.5 kB N/A -
0cz1d0mv5g_q7.js gzip 39.4 kB 39.4 kB ✓
0e5lgz-xcf6ln.js gzip 273 B N/A -
0enhddgqs2dl-.js gzip 265 B N/A -
0h34mejwa_7pd.js gzip 354 B N/A -
0w853zxni3ro0.js gzip 1.36 kB N/A -
13lq2aw3_0bgw.js gzip 3.52 kB N/A -
1elt1qium-r2m.css gzip 115 B 115 B ✓
1m5vdnlf0deiv.js gzip 411 B N/A -
1nigpy_mr0xvj.js gzip 339 B N/A -
1p0l8mn2wz5f2.js gzip 1.97 kB N/A -
1s-3t9k2_uhom.js gzip 117 B N/A -
1sqoyy39jpgkq.js gzip 121 B N/A -
1tf1phijqlx9j.js gzip 220 B 220 B ✓
2b05_424u1zq-.js gzip 120 B N/A -
2ca40xittx95r.js gzip 8.75 kB N/A -
2hgz42b8sldyc.js gzip 5.85 kB N/A -
2kci0vnt-49o-.js gzip 113 B N/A -
2n-8z9sbeiiem.js gzip 65.5 kB N/A -
2sxgl_rcrrn_p.js gzip 399 B N/A -
2u_p__ndkooh-.js gzip 5.66 kB N/A -
2vst0p6zpfrr_.js gzip 7.53 kB N/A -
2vt62i17bhbdu.js gzip 2.35 kB N/A -
342di-crrg9kq.js gzip 119 B N/A -
36bhyc0i1hpg3.js gzip 46.7 kB N/A -
36hfc2_979aw2.js gzip 119 B N/A -
38wfp6jjrrdfm.js gzip 131 B N/A -
3dl1ilk37-30g.js gzip 117 B N/A -
3h0us7yy7bd_o.js gzip 13.1 kB N/A -
3kvxagoobts7h.js gzip 117 B N/A -
3p99hd9vwe6ae.js gzip 13 kB N/A -
3r0475panhk6m.js gzip 123 B N/A -
3s0i-weykgzrt.js gzip 125 B N/A -
3wijk2ywqgsv9.js gzip 2.52 kB N/A -
41u5s3oe2-erp.js gzip 2.29 kB N/A -
turbopack-0d..9ejy.js gzip 3.74 kB 3.74 kB ✓
0-6bcj16ji2wf.js gzip N/A 10.6 kB -
07ryk0jced-sc.js gzip N/A 8.78 kB -
0f2pzjwlzp570.js gzip N/A 153 B -
0jwho9fkrb_t3.js gzip N/A 2.29 kB -
0plt0zo6-4_v5.js gzip N/A 166 B -
0qzh85u899nb_.js gzip N/A 153 B -
0roh390ijzxa5.js gzip N/A 8.75 kB -
0vex9w55ursqj.js gzip N/A 8.79 kB -
17oe55cu76cd7.js gzip N/A 450 B -
1drww5xikb-c-.js gzip N/A 9.46 kB -
1g60xde_dv17t.js gzip N/A 8.79 kB -
1tk2c3ly9trqz.js gzip N/A 158 B -
1uzv47btzam64.js gzip N/A 5.73 kB -
1vy7n7wxv_rh5.js gzip N/A 8.81 kB -
1xnmvqyl9im03.js gzip N/A 152 B -
2-0i7pl900-ou.js gzip N/A 8.71 kB -
2-kcbngm7ik7y.js gzip N/A 8.75 kB -
201m4ukegyvno.js gzip N/A 149 B -
29p5-xa4jmdhg.js gzip N/A 13.7 kB -
2fvvh1zteirv4.js gzip N/A 154 B -
2mr-6hunyw7uo.js gzip N/A 7.53 kB -
2nsrcvgnlgkby.js gzip N/A 3.56 kB -
2rvtn0r6ofqke.js gzip N/A 158 B -
2ugc69z0t0ypz.js gzip N/A 1.46 kB -
31-5ri_8uiq45.js gzip N/A 71.7 kB -
337k106f267_f.js gzip N/A 153 B -
36dn8i-_3dnq5.js gzip N/A 10 kB -
37kl4d29o4lzm.js gzip N/A 151 B -
3af363oh3ex9y.js gzip N/A 46.9 kB -
3c9h4nw32b1g_.js gzip N/A 154 B -
3fljpmwcjxqhx.js gzip N/A 10.3 kB -
3gz44skqdlsrb.js gzip N/A 8.71 kB -
3otcauqycd0hf.js gzip N/A 65.6 kB -
3qts07grq6eyi.js gzip N/A 152 B -
3rta4z_5ewc-z.js gzip N/A 152 B -
3skgwg3_u6i_e.js gzip N/A 13.2 kB -
3y9tus7kb5su0.js gzip N/A 13.3 kB -
Total 299 kB 401 kB ⚠️ +102 kB

Server

Middleware
Canary PR Change
middleware-b..fest.js gzip 1.02 kB 1.06 kB 🔴 +32 B (+3%)
Total 1.02 kB 1.06 kB ⚠️ +32 B
Build Details
Build Manifests
Canary PR Change
_buildManifest.js gzip 846 B 877 B 🔴 +31 B (+4%)
Total 846 B 877 B ⚠️ +31 B
Build Cache
Canary PR Change
00000001.sst gzip 13.7 MB 12.3 MB 🟢 1.35 MB (-10%)
00000002.sst gzip 12.2 MB 13.2 MB 🔴 +1.05 MB (+9%)
00000003.sst gzip 15.2 MB 16.5 MB 🔴 +1.27 MB (+8%)
00000004.sst gzip 13.9 MB 13.4 MB 🟢 443 kB (-3%)
00000005.sst gzip 10.2 MB 10.2 MB 🔴 +42.7 kB (+0%)
00000006.sst gzip 2.79 MB 2.8 MB 🔴 +8.97 kB (+0%)
00000007.sst gzip 59 B 59 B ✓
00000008.meta gzip 89 B 89 B ✓
00000009.meta gzip 298 kB 298 kB ✓
00000010.meta gzip 298 kB 298 kB ✓
00000011.meta gzip 298 kB 298 kB ✓
00000012.sst gzip 53.1 kB 52.8 kB ✓
00000013.sst gzip 1.8 MB 1.84 MB 🔴 +39.7 kB (+2%)
00000014.sst gzip 59 B 59 B ✓
00000015.meta gzip 116 B 116 B ✓
00000016.meta gzip 328 kB 328 kB ✓
00000017.meta gzip 406 kB 408 kB ✓
00000018.sst gzip 53.6 kB 53.6 kB ✓
00000019.sst gzip 1.37 MB 1.42 MB 🔴 +48.4 kB (+4%)
00000020.sst gzip 59 B 59 B ✓
00000021.meta gzip 116 B 116 B ✓
00000022.meta gzip 328 kB 328 kB ✓
00000023.meta gzip 372 kB 374 kB ✓
00000024.sst gzip 53.7 kB 53.7 kB ✓
00000025.sst gzip 1.37 MB 1.42 MB 🔴 +48.6 kB (+4%)
00000026.sst gzip 59 B 59 B ✓
00000027.meta gzip 116 B 116 B ✓
00000028.meta gzip 328 kB 328 kB ✓
00000029.meta gzip 372 kB 374 kB ✓
00000030.sst gzip 53.1 kB 52.9 kB ✓
00000031.sst gzip 1.37 MB 1.42 MB 🔴 +48.7 kB (+4%)
00000032.sst gzip 59 B 59 B ✓
00000033.meta gzip 116 B 116 B ✓
00000034.meta gzip 328 kB 328 kB ✓
00000035.meta gzip 372 kB 374 kB ✓
00000036.sst gzip 53.6 kB 53.5 kB ✓
00000037.sst gzip 1.37 MB 1.42 MB 🔴 +48.6 kB (+4%)
00000038.sst gzip 59 B 59 B ✓
00000039.meta gzip 116 B 116 B ✓
00000040.meta gzip 328 kB 328 kB ✓
00000041.meta gzip 372 kB 374 kB ✓
CURRENT gzip 93 B 94 B 🔴 +1 B (+1%)
LOG gzip 677 B 687 B 🔴 +10 B (+1%)
Total 80 MB 80.8 MB ⚠️ +821 kB

📦 Webpack

Client

Main Bundles
Canary PR Change
3322-HASH.js gzip 65.6 kB N/A -
4191.HASH.js gzip 169 B N/A -
9784-HASH.js gzip 5.57 kB N/A -
b1ad9f4c-HASH.js gzip 63.5 kB N/A -
framework-HASH.js gzip 59.7 kB 59.7 kB ✓
main-app-HASH.js gzip 253 B 252 B ✓
main-HASH.js gzip 39.8 kB 40.2 kB 🔴 +437 B (+1%)
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.2 kB -
8590-HASH.js gzip N/A 5.61 kB -
9750-HASH.js gzip N/A 4.68 kB -
a8984546-HASH.js gzip N/A 63.5 kB -
Total 236 kB 242 kB ⚠️ +5.74 kB
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 5.02 kB 582 B 🟢 4.43 kB (-88%)
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 12.4 kB 7.96 kB ✅ -4.43 kB

Server

Edge SSR
Canary PR Change
edge-ssr.js gzip 128 kB 129 kB ✓
page.js gzip 292 kB 294 kB ✓
Total 420 kB 423 kB ⚠️ +2.4 kB
Middleware
Canary PR Change
middleware-b..fest.js gzip 599 B 616 B 🔴 +17 B (+3%)
middleware-r..fest.js gzip 156 B 156 B ✓
middleware.js gzip 45.6 kB 45.6 kB ✓
edge-runtime..pack.js gzip 842 B 842 B ✓
Total 47.2 kB 47.2 kB ⚠️ +37 B
Build Details
Build Manifests
Canary PR Change
_buildManifest.js gzip 698 B 718 B 🔴 +20 B (+3%)
Total 698 B 718 B ⚠️ +20 B
Build Cache
Canary PR Change
0.pack gzip 4.79 MB 4.81 MB 🔴 +23.4 kB (+0%)
index.pack gzip 124 kB 124 kB ✓
index.pack.old gzip 124 kB 123 kB ✓
Total 5.04 MB 5.06 MB ⚠️ +23.3 kB

🔄 Shared (bundler-independent)

Runtimes
Canary PR Change
app-page-exp...dev.js gzip 373 kB 375 kB ✓
app-page-exp..prod.js gzip 206 kB 207 kB ✓
app-page-tur...dev.js gzip 373 kB 375 kB ✓
app-page-tur..prod.js gzip 206 kB 206 kB ✓
app-page-tur...dev.js gzip 369 kB 371 kB ✓
app-page-tur..prod.js gzip 203 kB 204 kB ✓
app-page.run...dev.js gzip 370 kB 371 kB ✓
app-page.run..prod.js gzip 203 kB 204 kB ✓
app-route-ex...dev.js gzip 82.5 kB 83.3 kB 🔴 +855 B (+1%)
app-route-ex..prod.js gzip 55.6 kB 56.3 kB 🔴 +717 B (+1%)
app-route-tu...dev.js gzip 82.5 kB 83.4 kB 🔴 +849 B (+1%)
app-route-tu..prod.js gzip 55.6 kB 56.3 kB 🔴 +717 B (+1%)
app-route-tu...dev.js gzip 82.1 kB 82.9 kB 🔴 +825 B (+1%)
app-route-tu..prod.js gzip 55.4 kB 56.1 kB 🔴 +723 B (+1%)
app-route.ru...dev.js gzip 82.1 kB 82.9 kB 🔴 +823 B (+1%)
app-route.ru..prod.js gzip 55.4 kB 56.1 kB 🔴 +722 B (+1%)
dev-validati...dev.js gzip 134 kB 134 kB ✓
dev-validati...dev.js gzip 134 kB 134 kB ✓
dev-validati...dev.js gzip 132 kB 132 kB ✓
dev-validati...dev.js gzip 132 kB 132 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.2 kB 46.5 kB ✓
pages-api-tu..prod.js gzip 34.3 kB 34.7 kB 🔴 +405 B (+1%)
pages-api.ru...dev.js gzip 46.2 kB 46.5 kB ✓
pages-api.ru..prod.js gzip 34.2 kB 34.6 kB 🔴 +402 B (+1%)
pages-turbo....dev.js gzip 54.6 kB 55.3 kB 🔴 +703 B (+1%)
pages-turbo...prod.js gzip 39.6 kB 40.2 kB 🔴 +588 B (+1%)
pages.runtim...dev.js gzip 54.6 kB 55.3 kB 🔴 +680 B (+1%)
pages.runtim..prod.js gzip 39.6 kB 40.2 kB 🔴 +588 B (+1%)
server.runti..prod.js gzip 65.8 kB 66.7 kB 🔴 +848 B (+1%)
use-cache-pr...dev.js gzip 72.5 kB 72.8 kB ✓
use-cache-pr...dev.js gzip 72.5 kB 72.8 kB ✓
use-cache-pr...dev.js gzip 70.7 kB 71 kB ✓
use-cache-pr...dev.js gzip 70.7 kB 71 kB ✓
Total 4.09 MB 4.11 MB ⚠️ +22.9 kB
📝 Changed Files (33 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
  • app-route-ex..ntime.dev.js
  • app-route-ex..time.prod.js
  • app-route-tu..ntime.dev.js
  • app-route-tu..time.prod.js
  • app-route-tu..ntime.dev.js
  • app-route-tu..time.prod.js
  • app-route.runtime.dev.js
  • app-route.ru..time.prod.js
  • dev-validati..ntime.dev.js
  • dev-validati..ntime.dev.js
  • dev-validati..ntime.dev.js
  • dev-validati..ntime.dev.js
  • ... and 13 more
View diffs
app-page-exp..ntime.dev.js
failed to diff
app-page-exp..time.prod.js
failed to diff
app-page-tur..ntime.dev.js
failed to diff
app-page-tur..time.prod.js
failed to diff
app-page-tur..ntime.dev.js
failed to diff
app-page-tur..time.prod.js
failed to diff
app-page.runtime.dev.js
failed to diff
app-page.runtime.prod.js
failed to diff
app-route-ex..ntime.dev.js

Diff too large to display

app-route-ex..time.prod.js

Diff too large to display

app-route-tu..ntime.dev.js

Diff too large to display

app-route-tu..time.prod.js

Diff too large to display

app-route-tu..ntime.dev.js

Diff too large to display

app-route-tu..time.prod.js

Diff too large to display

app-route.runtime.dev.js

Diff too large to display

app-route.ru..time.prod.js

Diff too large to display

dev-validati..ntime.dev.js
failed to diff
dev-validati..ntime.dev.js
failed to diff
dev-validati..ntime.dev.js
failed to diff
dev-validati..ntime.dev.js
failed to diff
pages-api-tu..ntime.dev.js

Diff too large to display

pages-api-tu..time.prod.js

Diff too large to display

pages-api.runtime.dev.js

Diff too large to display

pages-api.ru..time.prod.js

Diff too large to display

pages-turbo...ntime.dev.js

Diff too large to display

pages-turbo...time.prod.js

Diff too large to display

pages.runtime.dev.js

Diff too large to display

pages.runtime.prod.js

Diff too large to display

server.runtime.prod.js

Diff too large to display

use-cache-pr..ntime.dev.js

Diff too large to display

use-cache-pr..ntime.dev.js

Diff too large to display

use-cache-pr..ntime.dev.js

Diff too large to display

use-cache-pr..ntime.dev.js

Diff too large to display

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

Commit: 690aaad

@github-actions

github-actions Bot commented Aug 20, 2026 •

Copy link
Copy Markdown
Contributor

Tests Passed

Commit: 690aaad

@gaearon gaearon changed the title gaearon history prep Prepare for history-before-hydration changes Aug 20, 2026
@gaearon
gaearon marked this pull request as ready for review August 21, 2026 03:03
The helpers returned as soon as the new document committed, so a
scenario could act on history while the parser was still partway
through the HTML. Wait for `document.readyState` to leave "loading"
first. The stalled scripts are async, so this does not wait on them.
This pins the tested window to "inline scripts have run, external
scripts have not", which is the window the rest of the suite is about.

No framework changes.
The scenarios checked the URL bar and the rendered heading. Neither one
comes from the router: the browser owns the URL bar, and the heading is
whatever React last rendered. `usePathname()` and `useSearchParams()`
read the router's own `canonicalUrl`, which can disagree with both.
Render it from the layout and assert it too.

In the scenario where a third-party write lands during the recovery,
the router never learns about the written entry. That is recorded with
a TODO.

No framework changes.
The suite had no control case. Add a reload with no history change and
check that the server HTML is hydrated in place rather than re-rendered
or reloaded, and that traversing away and back afterwards works.

No framework changes.
@gaearon
gaearon force-pushed the gaearon-history-prep branch 2 times, most recently from 9690fc9 to c9e1114 Compare August 21, 2026 17:04
The router initializes from window.location. After a Back press before
hydration that is the traversed-to URL, while the server rendered the
page for the entry the document started on. usePathname() rendered
during hydration does not match the HTML, React reports a hydration
error and regenerates the tree on the client.

Render the pathname during hydration from the layout. Every scenario
now checks that the page reported no errors, except the two that press
Back before hydration, which record the mismatch with a TODO.
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
gaearon force-pushed the gaearon-history-prep branch from c9e1114 to 690aaad Compare August 21, 2026 20:48
@gaearon gaearon changed the title Prepare for history-before-hydration changes Improve "Back before hydration" test suite Aug 21, 2026
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.

@dianatofficial dianatofficial left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Solid PR. Tests cover the important paths.

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.
@gaearon gaearon closed this Nov 9, 2026
@github-actions github-actions Bot locked as resolved and limited conversation to collaborators Nov 23, 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.

2 participants