Skip to content

fix: hash canonical invariant - #96737

Open
icyJoseph wants to merge 7 commits into
canaryfrom
fix/hash-canonical-invariant
Open

icyJoseph wants to merge 7 commits into
canaryfrom
fix/hash-canonical-invariant

Conversation

@icyJoseph

@icyJoseph icyJoseph commented Aug 5, 2026 •

Copy link
Copy Markdown
Member

Route cache entries are keyed by pathname and search only, so one entry is shared across every hash of a route. A same-route navigation rebuilds the URL by appending the incoming hash to the entry's stored canonical URL:

Which means that "the stored canonical URL is hashless". Every time we write to RouteCacheEntry.canonicalUrl it has to be without hash. These places don't do that:

  • create-initial-router-state.ts passes createHrefFromUrl(location), so a direct entry to /p/123#modal seeds the entry with the hash
  • server-action-reducer.ts derives its URL from state.canonicalUrl when the action didn't redirect, so any revalidating action fired on a hash-ful URL poisons the entry
  • render-tree.ts (formerly ppr-navigations.ts, moved in Reorganize client router modules #97402) passes a retry URL that fetchServerResponse returns as originalUrl when the response wasn't redirected

AppRouterState.canonicalUrl is the opposite: it drives history.pushState and must keep the hash. Both are plain string, and #93132 already fixed one writer of the first kind, which is why this recurred.

Any client router navigation then conflicts with the stored hash. In #96714, router.replace(pathname) restores a hash the app just removed, and router.push to a new hash produces #foo#bar, once again.

Both happen on routes entered directly with a hash, and also after a server action revalidates.

Instead of going around patching createHrefFromUrl, we should normalize inside discoverKnownRoute, the single point all route cache entries are written through, rather than at each call site.

I got an agent to look at createHrefFromUrl usage further, and also noticed that redirect('/foo#bar') from a server action called outside a form, such as from an onClick, loses its fragment.

That dropped a fragment the action explicitly asked for, while the external-redirect branch four lines above kept it. Now both keep it.

The reducer itself navigates to the hashful URL, but when the action's rejection goes unhandled, handleUnhandledRedirect in app-router.tsx navigates again with the error's URL and ignores its handled marker. A <form action> goes through RedirectBoundary, which respects the marker, so that case already kept the hash.

Also adding tests for the other places a fragment could plausibly end up on a canonicalUrl.

  • test/e2e/app-dir/hash-canonical-url has six of them: the hash not being cleared, and the double fragment, each on initial load and after a server action, plus the action redirect and a guard on the prefetch path.

  • test/e2e/app-dir/hash-canonical-url-retry has three more pinning the redirect-retry path.

On turning prefetch off for the hash-cross-path-push test, a prefetched entry is hashless, so as it stood it wouldn't have caught a regression.

Fixes #96714
Fixes #99245
Fixes #95671
Fixes #96643

Related: #77101 (the <form action> case was fixed by #86367 in 16.1.0-canary.17; this fixes the onClick case)

Verification

pnpm test-start-turbo on hash-canonical-url, hash-canonical-url-retry and the navigation cross-pathname hash test, all green on this branch. Red/green by reverting parts of the change:

  • Without the discoverKnownRoute strip (canary's code): 4 of 6 hash-canonical-url tests fail (direct entry with a hash, and after a revalidating action, for both replace and push)
  • Without the strip and without the false in navigateToUnknownRoute: the hash-cross-path-push test and the retry suite's replace test also fail
  • Without the server-action-reducer.ts change: the action redirect test fails (url.hash is ""); a <form action> variant of it passes either way
  • Not run: next dev and webpack (covered by CI)

@github-actions

github-actions Bot commented Aug 5, 2026 •

Copy link
Copy Markdown
Contributor

Stats from current PR

🔴 2 regressions, 1 improvement

Metric Canary PR Change Trend
Webpack Build Time 24.438s 25.500s 🔴 +1.062s (+4%) ▃▅█▆█
Webpack Build Time (cached) 24.524s 25.255s 🔴 +731ms (+3%) ▃▄█▆▇
Turbo Build Time (cached) 3.300s 3.014s 🟢 286ms (-9%) ▂▁▁▂▂
📊 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) 813ms 812ms ✓ ▁▁▁▁▁
Cold (Ready in log) 802ms 795ms ✓ ▁▁▁▁▁
Cold (First Request) 1.351s 1.315s ✓ ▂▁▁▁▂
Warm (Listen) 813ms 813ms ✓ ▂▂▂▂▂
Warm (Ready in log) 789ms 786ms ✓ ▁▁▁▁▁
Warm (First Request) 1.303s 1.295s ✓ ▁▁▁▁▁
📦 Dev Server (Webpack) (Legacy)

📦 Dev Server (Webpack)

Metric Canary PR Change Trend
Cold (Listen) 813ms 812ms ✓ ▁████
Cold (Ready in log) 785ms 793ms ✓ ▂▆█▇▅
Cold (First Request) 3.375s 3.351s ✓ ▃▄█▆▄
Warm (Listen) 812ms 811ms ✓ █████
Warm (Ready in log) 793ms 783ms ✓ ▂▆█▆▆
Warm (First Request) 3.423s 3.373s ✓ ▄▅█▆▅

⚡ Production Builds

Metric Canary PR Change Trend
Fresh Build 6.255s 6.095s ✓ ▂▁▁▂▂
Cached Build 3.300s 3.014s 🟢 286ms (-9%) ▂▁▁▂▂
📦 Production Builds (Webpack) (Legacy)

📦 Production Builds (Webpack)

Metric Canary PR Change Trend
Fresh Build 24.438s 25.500s 🔴 +1.062s (+4%) ▃▅█▆█
Cached Build 24.524s 25.255s 🔴 +731ms (+3%) ▃▄█▆▇
node_modules Size 553 MB 553 MB ✓ ▁▁▁▁▁
📦 Bundle Sizes

Bundle Sizes

⚡ Turbopack

Client

Main Bundles
Canary PR Change
00gjhzer5qnd4.js gzip 154 B N/A -
058w61dpm3cqw.js gzip 65.6 kB N/A -
07jdby0ue616s.js gzip 450 B N/A -
0afsxfu2qvoii.js gzip 155 B N/A -
0awb0vu674hnk.js gzip 158 B N/A -
0b7f6-wm5_fgx.js gzip 10.3 kB N/A -
0cz1d0mv5g_q7.js gzip 39.4 kB 39.4 kB ✓
0hnzlx68v8wq8.js gzip 8.7 kB N/A -
0ky-xprxap4kv.js gzip 158 B N/A -
0r0fd4d11wyn4.js gzip 71.5 kB N/A -
0r7ql6wdiv73c.js gzip 10 kB N/A -
0zzo4lng-xek6.js gzip 9.44 kB N/A -
10k2k258ymu43.js gzip 8.78 kB N/A -
1a3hvn719yvjk.js gzip 8.75 kB N/A -
1b0_t57fceog6.js gzip 170 B N/A -
1cgyoj65s6d9x.js gzip 156 B N/A -
1elt1qium-r2m.css gzip 115 B 115 B ✓
1enuk65o0iyxc.js gzip 8.7 kB N/A -
1j8mwpx902qpn.js gzip 164 B N/A -
1myuaxlmtuan4.js gzip 8.75 kB N/A -
1tf1phijqlx9j.js gzip 220 B 220 B ✓
1tgz0p9msc4tz.js gzip 155 B N/A -
29cpgg4v96x1a.js gzip 46.3 kB N/A -
2emlnosu2nbyp.js gzip 10.6 kB N/A -
2m3bh1binl60-.js gzip 13.2 kB N/A -
2n72evcce9v1r.js gzip 8.77 kB N/A -
2y5oyfvi8x17-.js gzip 157 B N/A -
31m0nox7lipas.js gzip 7.53 kB N/A -
34bfz28htmaat.js gzip 156 B N/A -
38-q43pzktqhs.js gzip 1.46 kB N/A -
38uv8-7_7l5av.js gzip 8.78 kB N/A -
3ac6edgvuj2vd.js gzip 3.66 kB N/A -
3ceky-0yt3r4e.js gzip 8.8 kB N/A -
3jb_acjto1dn1.js gzip 13.6 kB N/A -
3kfiy2dr7a5m3.js gzip 5.72 kB N/A -
3mrwxzed0ylgv.js gzip 13.1 kB N/A -
3vucd_i-9s02n.js gzip 154 B N/A -
407ltsw2jd-3v.js gzip 158 B N/A -
41u5s3oe2-erp.js gzip 2.29 kB N/A -
428qlfq3v26qp.js gzip 161 B N/A -
turbopack-08..64w9.js gzip 3.84 kB N/A -
turbopack-08..byc8.js gzip 3.86 kB N/A -
turbopack-0b..-c8g.js gzip 3.86 kB N/A -
turbopack-0m..dzni.js gzip 3.86 kB N/A -
turbopack-0r..y6wd.js gzip 3.86 kB N/A -
turbopack-0v..5wac.js gzip 3.86 kB N/A -
turbopack-1_..mi45.js gzip 3.86 kB N/A -
turbopack-1e..9fou.js gzip 3.86 kB N/A -
turbopack-1n..darg.js gzip 3.86 kB N/A -
turbopack-1t..zf9u.js gzip 3.86 kB N/A -
turbopack-1y..rqab.js gzip 3.88 kB N/A -
turbopack-21..vkoq.js gzip 3.86 kB N/A -
turbopack-3t..zm7h.js gzip 3.86 kB N/A -
turbopack-3t..xu-0.js gzip 3.86 kB N/A -
00kbbwl1whzq5.js gzip N/A 155 B -
070i4ykf3d62z.js gzip N/A 8.7 kB -
0agv3-x0j9qy3.js gzip N/A 10.3 kB -
0fp4xgd37m5-d.js gzip N/A 3.65 kB -
0j7ubrcd_njd7.js gzip N/A 162 B -
0jwho9fkrb_t3.js gzip N/A 2.29 kB -
0uqaj5-kagorf.js gzip N/A 160 B -
0wg1cc2wbyjd6.js gzip N/A 8.77 kB -
0z2rm17uxhhx5.js gzip N/A 158 B -
1_syfo9rv8a2q.js gzip N/A 7.54 kB -
10n4t641ac4-m.js gzip N/A 158 B -
12-pii8wla4es.js gzip N/A 71.6 kB -
14j95hw2e7a3z.js gzip N/A 8.74 kB -
166ci2e3b45t7.js gzip N/A 154 B -
17oe55cu76cd7.js gzip N/A 450 B -
1frm88k70_rxf.js gzip N/A 8.78 kB -
1rvsu5wwb2kn-.js gzip N/A 158 B -
1ynlcmklb8_j3.js gzip N/A 9.45 kB -
2_0jk384fqmik.js gzip N/A 46.3 kB -
24yke4g7c6tl3.js gzip N/A 157 B -
2a_p4b_g75xdo.js gzip N/A 8.8 kB -
2ce6apx02oexo.js gzip N/A 156 B -
2dn0ae5-nmkio.js gzip N/A 13.6 kB -
2g2f64lmg3m7w.js gzip N/A 5.73 kB -
2kolt5lwxt8o0.js gzip N/A 10 kB -
2lpk5_hknut8q.js gzip N/A 13.1 kB -
2p0a0fr4ydefu.js gzip N/A 8.7 kB -
2p9-5diwx5gt6.js gzip N/A 10.6 kB -
2tlidw2e-lxb8.js gzip N/A 65.6 kB -
2ugc69z0t0ypz.js gzip N/A 1.46 kB -
3552-t9hisyhx.js gzip N/A 155 B -
39rn5wd6qslc0.js gzip N/A 156 B -
3crtg4i2per05.js gzip N/A 8.74 kB -
3ghtbhrse9p63.js gzip N/A 8.78 kB -
3hk4sk0tzj4o2.js gzip N/A 169 B -
3q0h3yzsw1g9v.js gzip N/A 13.2 kB -
3wp6cj5wrp0d8.js gzip N/A 156 B -
turbopack-07..60mu.js gzip N/A 3.86 kB -
turbopack-09..viho.js gzip N/A 3.86 kB -
turbopack-14..fm0u.js gzip N/A 3.86 kB -
turbopack-16..-t6e.js gzip N/A 3.86 kB -
turbopack-19..0j_1.js gzip N/A 3.86 kB -
turbopack-1e..6nfx.js gzip N/A 3.86 kB -
turbopack-1n..goqe.js gzip N/A 3.86 kB -
turbopack-1p..m_om.js gzip N/A 3.86 kB -
turbopack-1r..a-g8.js gzip N/A 3.86 kB -
turbopack-1z..hpil.js gzip N/A 3.86 kB -
turbopack-2_..gdyp.js gzip N/A 3.84 kB -
turbopack-3p..c8lx.js gzip N/A 3.86 kB -
turbopack-3p..ut2y.js gzip N/A 3.88 kB -
turbopack-42..4cr1.js gzip N/A 3.86 kB -
Total 451 kB 451 kB ⚠️ +24 B

Server

Middleware
Canary PR Change
middleware-b..fest.js gzip 775 B 773 B ✓
Total 775 B 773 B ✅ -2 B
Build Details
Build Manifests
Canary PR Change
_buildManifest.js gzip 437 B 434 B ✓
Total 437 B 434 B ✅ -3 B
Build Cache
Canary PR Change
00000001.sst gzip 13 MB 14.4 MB 🔴 +1.34 MB (+10%)
00000002.sst gzip 14 MB 13.5 MB 🟢 452 kB (-3%)
00000003.sst gzip 14.2 MB 13.7 MB 🟢 512 kB (-4%)
00000004.sst gzip 14 MB 13.5 MB 🟢 408 kB (-3%)
00000005.sst gzip 10.2 MB 10.2 MB 🟢 46.8 kB (0%)
00000006.sst gzip 2.81 MB 2.81 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 298 kB 298 kB ✓
00000011.meta gzip 298 kB 298 kB ✓
00000012.sst gzip 50.2 kB 51 kB 🔴 +844 B (+2%)
00000013.sst gzip 1.88 MB 1.89 MB 🔴 +11.2 kB (+1%)
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 50.9 kB 51.8 kB 🔴 +921 B (+2%)
00000019.sst gzip 1.39 MB 1.39 MB 🟢 5.43 kB (0%)
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 50.9 kB 51.8 kB 🔴 +857 B (+2%)
00000025.sst gzip 1.39 MB 1.39 MB 🟢 4.5 kB (0%)
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.3 kB 51.2 kB 🔴 +869 B (+2%)
00000031.sst gzip 1.39 MB 1.39 MB 🟢 4.52 kB (0%)
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 50.8 kB 51.7 kB 🔴 +952 B (+2%)
00000037.sst gzip 1.39 MB 1.39 MB 🟢 4.49 kB (0%)
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 671 B 656 B 🟢 15 B (-2%)
Total 80.3 MB 80.2 MB ✅ -81.4 kB

📦 Webpack

Client

Main Bundles
Canary PR Change
3322-HASH.js gzip 65.2 kB N/A -
4191.HASH.js gzip 169 B N/A -
7920-HASH.js gzip 4.67 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 253 B 252 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 65.9 kB -
8590-HASH.js gzip N/A 5.6 kB -
9750-HASH.js gzip N/A 4.68 kB -
a8984546-HASH.js gzip N/A 63.2 kB -
Total 241 kB 241 kB ⚠️ +692 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 ⚠️ +348 B
Middleware
Canary PR Change
middleware-b..fest.js gzip 616 B 617 B ✓
middleware-r..fest.js gzip 156 B 156 B ✓
middleware.js gzip 45.7 kB 45.5 kB ✓
edge-runtime..pack.js gzip 842 B 842 B ✓
Total 47.3 kB 47.1 kB ✅ -160 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.87 MB ✓
index.pack gzip 122 kB 122 kB ✓
index.pack.old gzip 123 kB 124 kB ✓
Total 5.12 MB 5.11 MB ✅ -2.22 kB

🔄 Shared (bundler-independent)

Runtimes
Canary PR Change
app-page-exp...dev.js gzip 374 kB 374 kB ✓
app-page-exp..prod.js gzip 207 kB 207 kB ✓
app-page-tur...dev.js gzip 374 kB 374 kB ✓
app-page-tur..prod.js gzip 206 kB 207 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.3 kB 83.3 kB ✓
app-route-ex..prod.js gzip 56.4 kB 56.4 kB ✓
app-route-tu...dev.js gzip 83.3 kB 83.3 kB ✓
app-route-tu..prod.js gzip 56.4 kB 56.4 kB ✓
app-route-tu...dev.js gzip 82.9 kB 82.9 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 46 kB 46 kB ✓
pages-api.ru..prod.js gzip 34.3 kB 34.3 kB ✓
pages-turbo....dev.js gzip 54.8 kB 54.8 kB ✓
pages-turbo...prod.js gzip 39.9 kB 39.9 kB ✓
pages.runtim...dev.js gzip 54.8 kB 54.8 kB ✓
pages.runtim..prod.js gzip 39.9 kB 39.9 kB ✓
server.runti..prod.js gzip 67 kB 67 kB ✓
use-cache-pr...dev.js gzip 72.3 kB 72.3 kB ✓
use-cache-pr...dev.js gzip 72.4 kB 72.4 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 ⚠️ +260 B
📝 Changed Files (10 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
  • pages-api.ru..time.prod.js
  • pages.runtime.prod.js
View diffs
app-page-exp..ntime.dev.js
failed to diff
app-page-exp..time.prod.js

Diff too large to display

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

Diff too large to display

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

Diff too large to display

app-page.runtime.dev.js
failed to diff
app-page.runtime.prod.js

Diff too large to display

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

Diff too large to display

pages.runtime.prod.js

Diff too large to display

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

Commit: 9292673

@github-actions

github-actions Bot commented Aug 5, 2026 •

Copy link
Copy Markdown
Contributor

Tests Passed

Commit: a3325d0
View workflow run

@icyJoseph
icyJoseph marked this pull request as ready for review August 6, 2026 08:29
@icyJoseph
icyJoseph requested a review from acdlite August 6, 2026 13:57
@ewah

This comment was marked as spam.

@eljose47

Copy link
Copy Markdown

Another symptom of the same bug, reported in #99245: after a full load at a fragment URL, same-route hash Links concatenate instead of replacing. Load /#a, click <Link href="#b">, and the router pushes /#a#b. Every later hash Link keeps the #a.

This case has been reported twice before, as #95671 and #98644. Both were closed automatically for having no repro link. It might be worth adding to the hash-canonical-url tests here, since the /#a#b URL is the most visible form of this bug.

@OfficialCodeVoyage

Copy link
Copy Markdown

Another data point, from a production app hitting this.

Still reproduces on 16.4.5, the November security release (also on 16.4.4 and 16.5.0-canary.36). It needs a production build, a page rendered on demand, and a hash on the full page load:

full page load /pricing#packs          /pricing#packs
click <Link href="/other">             /other
click <Link href="/pricing#packs">     /pricing#packs#packs   <- expected /pricing#packs

A <Link href="/pricing#faq"> instead lands on /pricing#packs#faq.

Same app and same clicks, measured on 16.4.4, none of these double: a full load with no hash, reaching the page by clicking from /, a prerendered page, next dev, and a first click after staleTimes.static has expired (tested with 30 s).

Fix check: changing only the write in create-initial-router-state to createHrefFromUrl(location, false) removes the doubling on 16.4.4 and 16.4.5 (patched dist/ and dist/esm/, rebuilt, same clicks). That is the first writer this PR lists. We have not tested this PR's own build or its other two writers.

Why it matters in production: the doubled URL is what location.href returns. We passed the current address to a payment provider as a cancel URL, the provider rejected it as invalid, and a customer's purchase attempts failed. We now clean that URL on the server and ship the one-argument change as a postinstall patch until a release carries this fix.

A review here would help. Thanks for working on it.

@icyJoseph
icyJoseph force-pushed the fix/hash-canonical-invariant branch from 17594f8 to 6a8fd81 Compare November 5, 2026 00:08
@ThulinaWickramasinghe

Copy link
Copy Markdown

Hi do we know a stable release without this issue or a version which fixes this? We have production issue where chunks fail to load. This is affecting thousands of users. We are suspecting this as a root cause.

Copy link
Copy Markdown

We independently reproduced this issue in AMCore using Next.js 16.4.2 in a production build.

Opening /en/login?tag=a&tag=b#locale directly, switching to Russian, then back to English produced /en/login?tag=a&tag=b#locale#locale. We reproduced this twice; the equivalent navigation without an initial fragment worked correctly.

We applied a narrow local patch that strips the fragment from the canonical URL stored by discoverKnownRoute, in both published module variants. Our production checks then passed for fragment preservation, replacement and clearing, repeated query parameters, and back/forward navigation.

This validates that normalization for our scenario, not this PR’s complete implementation or its separate server-action redirect change.

An official fix would let us remove the temporary dependency patch. Thank you for working on this.

felzab added a commit to felzab/frankfurtleague that referenced this pull request Nov 13, 2026
Frontend deps: Next 16.4.6's App Router counts a navigation as hash-only
only when the hash changed, so a link to the fragment the address
already holds is treated as a refresh of the page and never scrolls
(vercel/next.js#88986, open). A page loaded with a fragment also keeps
that fragment in its route cache entry, to which the next fragment link
appends its own, so the address reads `#a#b` (vercel/next.js#101168,
open). 16.4.6 is the newest release, and the newest canary,
16.5.0-canary.49, carries neither fix: both of its lines read unchanged.

The patch is pnpm's own mechanism, built with `pnpm patch` and
`pnpm patch-commit` (pnpm.io/11.x/cli/patch), keyed to the exact
version with both issues named beside the entry. It holds two fixes.

- The navigate reducer, which every Link click and every router.push or
  replace enters, marks a navigation to a fragment with a new
  ScrollBehavior member, and completeSoftNavigation counts such a
  navigation as a fragment change even when the hash is the same.
  HTML's navigate algorithm scrolls to the fragment on every such
  navigation. Widening the hash comparison alone, as the research
  proposed, was rejected: a server action with no redirect and a
  server-patch retry navigate to the current URL with the default
  scroll behaviour, so every save on an address holding a fragment
  would have jumped the page to it.
- discoverKnownRoute, the one writer of a route cache entry, stores the
  canonical URL without its fragment, the invariant Next's own comment
  at the unknown-route call states. That is where the open upstream fix
  vercel/next.js#96737 puts it. Trimming the hash where
  navigateUsingPrefetchedRouteTree reads it would leave the optimistic
  route entries, built from the same stored URL, carrying it.

Only `dist/client/` is patched. In a production build the browser
chunks and the server's SSR chunks both carry that copy: each patched
hunk appears in them with the CommonJS export getters and footer that
only `dist/client/` has, and no chunk holds the unpatched comparison,
which `dist/esm/` would have brought. The server never runs a
navigation.

The resolving install `pnpm patch-commit` runs also wrote ajv-formats
3.0.1's optional peer on ajv into the lockfile, which the frozen
install had not asked for. It is pnpm's own write, and the package
declares that peer.

Verified: `pnpm install --frozen-lockfile` in a clean copy of the
manifest, lockfile, workspace file and patches exits 0 and installs
the patched Next. `scripts/tests/test_patched_dependencies.py` exits 0.
`pnpm build` with the gate's environment, `pnpm run typecheck`, `lint`,
`knip` and `knip:prod` each exit 0. Whether the second click scrolls is
established only in a browser on a rebuilt stack.
@vercel

vercel Bot commented Nov 24, 2026

Copy link
Copy Markdown
Contributor

Vercel Agent Verified Review finished reviewing your changes and confirmed no issues. 1 finding needs your judgment.
a3325d0 • Tue, 24 Nov 2026 09:02:38 GMT • 1m 46s • Report • Details

Summary

Strips any hash from the canonical URL inside discoverKnownRoute, the single function every route cache entry is written through. Entries are shared across hashes and readers append the target hash, so a stored hash produced #a#b or brought back a hash the app had cleared. The change also keeps the hash on the redirect error that an internal server-action redirect rejects with, and adds e2e coverage for direct entry with a hash, revalidating actions and the redirect-retry path.

Questions for the author (1)

  • Did the new 'should keep the hash of a redirect issued by an action' test fail for you on the base? Reading the code, the base already navigates to the hashful redirect URL.

Issues (1)

Severity Issue Location
Low Unverified: Action-redirect hash test passes without the server-action-reducer change hash-canonical-url.test.ts:94
Checked (3)

     Check Location
Holds Every route cache entry write now goes through the hash strip in discoverKnownRoute.
Grepped every caller of writeRouteIntoCache/fulfillRouteCacheEntry: only optimistic-routes.ts calls them, after the strip. All external writers call discoverKnownRoute.
optimistic-routes.ts:249
Holds The router state's canonical URL still carries the hash, so history keeps it.
The prefetched path appends url.hash to the now-hashless route.canonicalUrl. The unknown-route path still passes the hashful createHrefFromUrl(canonicalUrl) to navigateToKnownRoute (line 744).
app-router-state.ts:430
Holds Removing the explicit false in navigateToUnknownRoute changes nothing, because discoverKnownRoute strips the hash itself.
The canonicalUrl argument flows into discoverKnownRoute, and the strip there is the first thing that runs.
app-router-state.ts:670
Review steps

     Step Time
Sandbox set up 18.9s
Repository rules read: REVIEW.md and AGENTS.md files (4)
Agent review: 7 of 24 changed files opened, 2 commands run 1m 25s
Repros run on the head and the base (0), findings graded (1) 1.8s

Read the full report ↗ (Download report.json • report.md)

Confirmed: its repro failed on this commit and not the same way on the base. Unverified: a person should judge it.

@vercel vercel Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Vercel Agent Verified Review reviewed a3325d0 and isn’t approving it: its findings need your judgment first. 1 open finding is listed in its review summary and inline. View the full report


it('should keep the hash of a redirect issued by an action', async () => {
const browser = await next.browser('/')
await browser.elementById('to-action').click()

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Note

Unverified · Minor — Action-redirect hash test passes without the server-action-reducer change

The PR says redirect('/foo#bar') from a server action loses its fragment, and adds 'should keep the hash of a redirect issued by an action' to cover it. The base already keeps the hash in the address bar: the reducer navigates to createHrefFromUrl(redirectUrl), which includes the hash by default. The edited redirectHref only goes into a RedirectError marked handled = true, which RedirectBoundary never navigates with. So the new test can't fail on the base, and the described bug isn't what this hunk changes. Its only visible effect is the URL in the rejected error's digest.

Needs your judgment: the review traced this through the code but couldn’t run anything to prove it.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

🤖 This reply was written with an AI agent, in a session where a human and the agent tested whether this hunk is needed with red/green e2e runs (pnpm test-start-turbo). The results below come from those runs.

Yes, it fails on the base. The reducer does navigate to the hashful URL, but the fixture calls the action from an onClick without awaiting it, so the rejected RedirectError reaches handleUnhandledRedirect in app-router.tsx. That listener doesn't check handled, and it pushes the error's URL again. On canary that URL is built with createHrefFromUrl(redirectLocation, false), so the second navigation drops the hash.

Call style without this hunk with it
onClick={() => action()} lands on /redirect-target with no hash #section
<form action={action}> #section #section

A <form action> goes through RedirectBoundary, which respects handled, so it already kept the hash. That case was fixed in #86367. The listener ignoring handled, and the double navigation that causes, will be a separate follow-up.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

7 participants