Skip to content

Encode the cache item name built by unstable_cache - #96937

Open
unstubbable wants to merge 2 commits into
hl/encode-header-safefrom
hl/encode-fetch-url
Open

Encode the cache item name built by unstable_cache#96937
unstubbable wants to merge 2 commits into
hl/encode-header-safefrom
hl/encode-fetch-url

Conversation

@unstubbable

@unstubbable unstubbable commented Aug 7, 2026

Copy link
Copy Markdown
Contributor

A cache implementation may serialize cache metadata into HTTP request headers, whose values are limited to Latin-1. unstable_cache assembles a cache item name from the request URL and the name of the cached callback, and neither part was encoded. When that name holds a character above U+00FF the conversion throws before the request is dispatched, so the read never reaches the cache and the write that follows it fails the same way. Nothing is stored, nothing is found, and the entry falls back to the origin on every render.

The name is built by getFetchUrlPrefix, which reads the pathname and the search parameters out of the request URL. The pathname stays percent-encoded, but URLSearchParams returns decoded keys and values, so a non-ASCII query parameter is the reachable case: it applies to any dynamic route that calls unstable_cache, whether or not the route reads searchParams, and therefore also to a parameter a caller appends. A callback whose name holds such a character is affected too, though a production build usually renames the binding.

This change encodes the assembled name with encodeHeaderSafe. That helper only replaces characters outside the class Node accepts in a header value, so the separating spaces and the URL punctuation are preserved and the name keeps its documented shape. Every name that is representable today is returned unchanged, so this is inert for existing entries. The item name is a label: it is not the cache key, which is derived separately from the callback's key parts and arguments, and the Suspense Cache API neither parses nor matches on it.

The test covers the two parts of the name on separate routes so a failure names the part it comes from, and asserts the constraint rather than either input, since which inputs are live depends on the bundler and on minification. A deployment checks it through the real cache handler implementation, where the failure shows up as an entry that is recomputed on every request.

fixes #76286

Cache metadata reaches a cache implementation through HTTP request
headers, whose values are limited to Latin-1. Every tag, soft tag, and
cache item name has to be representable there. A value that is not gets
lost before it reaches the cache, so the entry is never found and never
stored, and the route calls the origin on every render without reporting
an error.

`unstable_cache` assembles its item name from the request URL and the
name of the cached callback, and the fixture covers those two parts on
separate routes so that a failure names the part it comes from.
`/[slug]` holds an anonymous callback and is requested with a non-ASCII
segment and a non-ASCII query parameter. The pathname arrives
percent-encoded, but `URLSearchParams` returns decoded keys and values,
so the query is the part that cannot be carried. `/named-callback` is
requested under a pure ASCII URL and holds a callback whose name is
non-ASCII, which leaves the name as the only candidate there.

The `/[slug]` route also renders a tagged `fetch` as a control. It
derives its item name from an already percent-encoded URL, so it has to
keep caching for the very request that breaks the entry beside it, which
separates a broken item name from a cache that is unavailable.

A deployment exercises the constraint through the real cache
implementation, where an item name it cannot carry surfaces as a cache
that never returns anything, so the assertion there is that the rendered
values stay stable across requests. Everywhere else the fixture
registers a cache handler that applies the same conversion and reports
the values that fail it.

Each branch guards against passing for the wrong reason. The local one
requires the handler to have been consulted, and every read asserts that
both values are numbers, so a render that produced neither cannot read
as two equal results. The callback name is only asserted in development:
a production build renames the binding, and the constraint would hold
there for a reason unrelated to the encoding.
A cache implementation may serialize cache metadata into HTTP request
headers, whose values are limited to Latin-1. `unstable_cache` assembles
a cache item name from the request URL and the name of the cached
callback, and neither part was encoded. When that name holds a character
above U+00FF the conversion throws before the request is dispatched, so
the read never reaches the cache and the write that follows it fails the
same way. Nothing is stored, nothing is found, and the entry falls back
to the origin on every render.

The name is built by `getFetchUrlPrefix`, which reads the pathname and
the search parameters out of the request URL. The pathname stays
percent-encoded, but `URLSearchParams` returns decoded keys and values,
so a non-ASCII query parameter is the reachable case: it applies to any
dynamic route that calls `unstable_cache`, whether or not the route
reads `searchParams`, and therefore also to a parameter a caller
appends. A callback whose name holds such a character is affected too,
though a production build usually renames the binding.

This change encodes the assembled name with `encodeHeaderSafe`. That
helper only replaces characters outside the class Node accepts in a
header value, so the separating spaces and the URL punctuation are
preserved and the name keeps its documented shape. Every name that is
representable today is returned unchanged, so this is inert for existing
entries. The item name is a label: it is not the cache key, which is
derived separately from the callback's key parts and arguments, and the
Suspense Cache API neither parses nor matches on it.

The test covers the two parts of the name on separate routes so a
failure names the part it comes from, and asserts the constraint rather
than either input, since which inputs are live depends on the bundler
and on minification. A deployment checks it through the real cache
handler implementation, where the failure shows up as an entry that is
recomputed on every request.
@github-actions

github-actions Bot commented Aug 7, 2026

Copy link
Copy Markdown
Contributor

Tests Passed

Commit: 40c7eca

@github-actions

github-actions Bot commented Aug 7, 2026

Copy link
Copy Markdown
Contributor

Stats from current PR

🟢 1 improvement

Metric Canary PR Change Trend
Turbo Build Time 6.218s 5.905s 🟢 313ms (-5%) ▆▆▃▄▅
📊 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 813ms ▄▆▆▅▇
Cold (Ready in log) 795ms 794ms ▁▄▄▃▇
Cold (First Request) 1.320s 1.326s ▁▆▄▃▇
Warm (Listen) 813ms 813ms ▁▁▁▁█
Warm (Ready in log) 795ms 800ms ▂▆▃▁█
Warm (First Request) 1.336s 1.345s ▁▇▃▁█
📦 Dev Server (Webpack) (Legacy)

📦 Dev Server (Webpack)

Metric Canary PR Change Trend
Cold (Listen) 812ms 811ms ▅▄▅▅▅
Cold (Ready in log) 779ms 779ms ▄▃▄▄▂
Cold (First Request) 3.310s 3.393s ▄▄▄▅▂
Warm (Listen) 811ms 811ms ▅▅▅▅▁
Warm (Ready in log) 778ms 774ms ▅▄▅▅▁
Warm (First Request) 3.321s 3.345s ▆▃▅▅▂

⚡ Production Builds

Metric Canary PR Change Trend
Fresh Build 6.218s 5.905s 🟢 313ms (-5%) ▆▆▃▄▅
Cached Build 3.001s 2.975s ▁█▃▄▆
📦 Production Builds (Webpack) (Legacy)

📦 Production Builds (Webpack)

Metric Canary PR Change Trend
Fresh Build 24.028s 23.764s ▇▂█▄▂
Cached Build 24.275s 24.080s ▃▂▆▃▁
node_modules Size 553 MB 553 MB ▁▁▁▁▁
📦 Bundle Sizes

Bundle Sizes

⚡ Turbopack

Client

Main Bundles
Canary PR Change
06bwjz_-4znb-.js gzip 154 B N/A -
07jdby0ue616s.js gzip 450 B N/A -
0cz1d0mv5g_q7.js gzip 39.4 kB 39.4 kB
0im0vz0lo815s.js gzip 8.77 kB N/A -
0l54qj3-79a7-.js gzip 162 B N/A -
0qa98wh5-vacg.js gzip 160 B N/A -
1-jrl0bfay5va.js gzip 157 B N/A -
133405p6ewydx.js gzip 10 kB N/A -
17p50_vq5-xc5.js gzip 8.71 kB N/A -
19xzn89g-pvj8.js gzip 9.45 kB N/A -
1elt1qium-r2m.css gzip 115 B 115 B
1ey-k8tr3q-bs.js gzip 10.6 kB N/A -
1p6ukk737nprf.js gzip 156 B N/A -
1q6ufgdgm0nh1.js gzip 157 B N/A -
1tf1phijqlx9j.js gzip 220 B 220 B
1wd1e9uuc4euy.js gzip 8.76 kB N/A -
1x8o4qqktz5ba.js gzip 157 B N/A -
2-w5wamautz2c.js gzip 65.6 kB N/A -
219oe54sobbj5.js gzip 8.81 kB N/A -
22alyybqz5_09.js gzip 3.57 kB N/A -
23_81vni0opf0.js gzip 157 B N/A -
2816ugqjp0cw7.js gzip 156 B N/A -
28p1njha-2ovo.js gzip 13.6 kB N/A -
2b-nvb0ybzif_.js gzip 8.7 kB N/A -
2bdp2_cq5bxqf.js gzip 169 B N/A -
2j5mn0ng9x-8y.js gzip 71.5 kB N/A -
2jrojsoli4din.js gzip 155 B N/A -
2m3bh1binl60-.js gzip 13.2 kB N/A -
2oks0a7s7ajem.js gzip 10.3 kB N/A -
31r35oy32u7ag.js gzip 46.7 kB N/A -
38-q43pzktqhs.js gzip 1.46 kB N/A -
39vr3swgz7fry.js gzip 155 B N/A -
3fw39x1tg924h.js gzip 8.79 kB N/A -
3kfiy2dr7a5m3.js gzip 5.72 kB N/A -
3l2c-qzlafpi-.js gzip 8.78 kB N/A -
3mrwxzed0ylgv.js gzip 13.1 kB N/A -
3rdf0zyd2dgxs.js gzip 8.76 kB N/A -
41jfc8g2x7su6.js gzip 7.54 kB N/A -
41u5s3oe2-erp.js gzip 2.29 kB N/A -
445z7nxr_34_5.js gzip 155 B N/A -
turbopack-07..iac9.js gzip 3.86 kB N/A -
turbopack-0d..3g83.js gzip 3.88 kB N/A -
turbopack-0h..fyuy.js gzip 3.86 kB N/A -
turbopack-0u..33e_.js gzip 3.86 kB N/A -
turbopack-14..19iy.js gzip 3.86 kB N/A -
turbopack-15..1srt.js gzip 3.86 kB N/A -
turbopack-1k..6a7i.js gzip 3.86 kB N/A -
turbopack-22..g_rn.js gzip 3.86 kB N/A -
turbopack-2d..r8zj.js gzip 3.84 kB N/A -
turbopack-2f..kjh-.js gzip 3.86 kB N/A -
turbopack-36..nj8y.js gzip 3.86 kB N/A -
turbopack-3b..xviv.js gzip 3.86 kB N/A -
turbopack-3q..regv.js gzip 3.86 kB N/A -
turbopack-3t..-aad.js gzip 3.86 kB N/A -
05era0sgyuzos.js gzip N/A 169 B -
0b87lr4cig17b.js gzip N/A 8.75 kB -
0e1qkafcsotw-.js gzip N/A 8.7 kB -
0j64ls9v56hnd.js gzip N/A 3.56 kB -
0jwho9fkrb_t3.js gzip N/A 2.29 kB -
0masaz6qt9sws.js gzip N/A 10 kB -
0n2f4y98eckf4.js gzip N/A 8.78 kB -
0nfyq2uqpzxo0.js gzip N/A 161 B -
0oxxrqdztv01y.js gzip N/A 157 B -
0srrwa49njwix.js gzip N/A 156 B -
1-z24kqxc7682.js gzip N/A 155 B -
116z3939gven3.js gzip N/A 158 B -
1234ksg9gdne_.js gzip N/A 8.77 kB -
13zwxq46cobu6.js gzip N/A 155 B -
17oe55cu76cd7.js gzip N/A 450 B -
18ehgajg6-37l.js gzip N/A 46.7 kB -
1iaemojbqwqrc.js gzip N/A 153 B -
1ko45a632uro6.js gzip N/A 8.79 kB -
1o2ufy-giyg2d.js gzip N/A 156 B -
1t2-5vzqrs9e7.js gzip N/A 156 B -
1u5hjpgv9qkjy.js gzip N/A 9.45 kB -
1w-rxw29xo-6-.js gzip N/A 158 B -
1wp4lf96ur1qn.js gzip N/A 154 B -
2d_wvcq8ytz-8.js gzip N/A 71.5 kB -
2dt81h56uyprs.js gzip N/A 8.75 kB -
2e30e3g3_0zjv.js gzip N/A 156 B -
2g2f64lmg3m7w.js gzip N/A 5.73 kB -
2lpk5_hknut8q.js gzip N/A 13.1 kB -
2ugc69z0t0ypz.js gzip N/A 1.46 kB -
2uile-vrx5c0g.js gzip N/A 8.81 kB -
2xhyauod8lfuk.js gzip N/A 8.71 kB -
2z-2x1ptpwnb3.js gzip N/A 10.3 kB -
36lwn7padaock.js gzip N/A 65.6 kB -
3q0h3yzsw1g9v.js gzip N/A 13.2 kB -
3rodp-hyqw7n0.js gzip N/A 13.6 kB -
3t7v1ffjrnh9r.js gzip N/A 7.55 kB -
40fek9ngcsppd.js gzip N/A 10.6 kB -
turbopack-04..9fpj.js gzip N/A 3.86 kB -
turbopack-0c..kifv.js gzip N/A 3.84 kB -
turbopack-0j..d_b8.js gzip N/A 3.86 kB -
turbopack-13..961i.js gzip N/A 3.86 kB -
turbopack-1n..6up2.js gzip N/A 3.88 kB -
turbopack-2_..n059.js gzip N/A 3.86 kB -
turbopack-28..uooe.js gzip N/A 3.86 kB -
turbopack-2k..vi_q.js gzip N/A 3.86 kB -
turbopack-2z..ez8z.js gzip N/A 3.86 kB -
turbopack-34..f-ja.js gzip N/A 3.86 kB -
turbopack-38..a-pl.js gzip N/A 3.86 kB -
turbopack-3b..xk9s.js gzip N/A 3.86 kB -
turbopack-3n..f-m5.js gzip N/A 3.86 kB -
turbopack-3x..bpg7.js gzip N/A 3.86 kB -
Total 451 kB 451 kB ⚠️ +11 B

Server

Middleware
Canary PR Change
middleware-b..fest.js gzip 782 B 776 B
Total 782 B 776 B ✅ -6 B
Build Details
Build Manifests
Canary PR Change
_buildManifest.js gzip 434 B 433 B
Total 434 B 433 B ✅ -1 B
Build Cache
Canary PR Change
00000001.sst gzip 13.8 MB 14.9 MB 🔴 +1.14 MB (+8%)
00000002.sst gzip 12.6 MB 13.1 MB 🔴 +518 kB (+4%)
00000003.sst gzip 14 MB 11.4 MB 🟢 2.65 MB (-19%)
00000004.sst gzip 14.8 MB 15.8 MB 🔴 +956 kB (+6%)
00000005.sst gzip 10.2 MB 10.2 MB 🟢 29.4 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 51.7 kB 51.3 kB
00000013.sst gzip 1.84 MB 1.87 MB 🔴 +34 kB (+2%)
00000014.sst gzip 59 B 59 B
00000015.meta gzip 116 B 116 B
00000016.meta gzip 325 kB 325 kB
00000017.meta gzip 404 kB 405 kB
00000018.sst gzip 52.7 kB 52.1 kB 🟢 573 B (-1%)
00000019.sst gzip 1.38 MB 1.38 MB 🟢 3.2 kB (0%)
00000020.sst gzip 59 B 59 B
00000021.meta gzip 116 B 116 B
00000022.meta gzip 325 kB 325 kB
00000023.meta gzip 372 kB 371 kB
00000024.sst gzip 52.7 kB 52.1 kB 🟢 617 B (-1%)
00000025.sst gzip 1.38 MB 1.38 MB 🟢 3.62 kB (0%)
00000026.sst gzip 59 B 59 B
00000027.meta gzip 116 B 116 B
00000028.meta gzip 325 kB 325 kB
00000029.meta gzip 372 kB 371 kB
00000030.sst gzip 51.9 kB 51.4 kB 🟢 540 B (-1%)
00000031.sst gzip 1.38 MB 1.38 MB 🟢 3.63 kB (0%)
00000032.sst gzip 59 B 59 B
00000033.meta gzip 116 B 116 B
00000034.meta gzip 325 kB 325 kB
00000035.meta gzip 372 kB 371 kB
00000036.sst gzip 52.6 kB 51.9 kB 🟢 621 B (-1%)
00000037.sst gzip 1.38 MB 1.38 MB 🟢 3.62 kB (0%)
00000038.sst gzip 59 B 59 B
00000039.meta gzip 116 B 116 B
00000040.meta gzip 325 kB 325 kB
00000041.meta gzip 372 kB 371 kB
CURRENT gzip 24 B 24 B
LOG gzip 680 B 663 B 🟢 17 B (-2%)
Total 80.3 MB 80.2 MB ✅ -52.4 kB

📦 Webpack

Client

Main Bundles
Canary PR Change
3322-HASH.js gzip 65.7 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 255 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.4 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 242 kB ⚠️ +657 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 295 kB
Total 423 kB 423 kB ⚠️ +426 B
Middleware
Canary PR Change
middleware-b..fest.js gzip 618 B 617 B
middleware-r..fest.js gzip 156 B 156 B
middleware.js gzip 45.7 kB 45.2 kB 🟢 536 B (-1%)
edge-runtime..pack.js gzip 842 B 842 B
Total 47.3 kB 46.8 kB ✅ -537 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.8 MB 4.8 MB
index.pack gzip 124 kB 122 kB 🟢 1.89 kB (-2%)
index.pack.old gzip 123 kB 123 kB
Total 5.05 MB 5.04 MB ✅ -4.15 kB

🔄 Shared (bundler-independent)

Runtimes
Canary PR Change
app-page-exp...dev.js gzip 375 kB 375 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 207 kB 207 kB
app-page-tur...dev.js gzip 370 kB 370 kB
app-page-tur..prod.js gzip 205 kB 205 kB
app-page.run...dev.js gzip 371 kB 371 kB
app-page.run..prod.js gzip 205 kB 205 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 ⚠️ +8 B
📝 Changed Files (12 files)

Files with changes:

  • app-page-exp..ntime.dev.js
  • app-page-tur..ntime.dev.js
  • app-page-tur..ntime.dev.js
  • app-page.runtime.dev.js
  • app-route-ex..ntime.dev.js
  • app-route-tu..ntime.dev.js
  • app-route-tu..ntime.dev.js
  • app-route.runtime.dev.js
  • pages-api-tu..ntime.dev.js
  • pages-api.runtime.dev.js
  • pages-turbo...ntime.dev.js
  • pages.runtime.dev.js
View diffs
app-page-exp..ntime.dev.js

Diff too large to display

app-page-tur..ntime.dev.js

Diff too large to display

app-page-tur..ntime.dev.js

Diff too large to display

app-page.runtime.dev.js

Diff too large to display

app-route-ex..ntime.dev.js

Diff too large to display

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

Diff too large to display

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

Diff too large to display

app-route.runtime.dev.js

Diff too large to display

pages-api-tu..ntime.dev.js

Diff too large to display

pages-api.runtime.dev.js

Diff too large to display

pages-turbo...ntime.dev.js

Diff too large to display

pages.runtime.dev.js

Diff too large to display

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

Commit: 40c7eca

@unstubbable
unstubbable marked this pull request as ready for review August 7, 2026 18:27
@unstubbable
unstubbable requested a review from gnoff August 7, 2026 18:28
anujbolewar

This comment was marked as spam.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

unstable_cache broken on Vercel when non english searchParams present in URL

2 participants