Skip to content

Warn for non-deterministic "use cache" args during final prerender#92820

Merged
unstubbable merged 3 commits into
canaryfrom
hl/use-cache-handle-non-deterministic-cache-keys
Apr 15, 2026
Merged

Warn for non-deterministic "use cache" args during final prerender#92820
unstubbable merged 3 commits into
canaryfrom
hl/use-cache-handle-non-deterministic-cache-keys

Conversation

@unstubbable
Copy link
Copy Markdown
Contributor

@unstubbable unstubbable commented Apr 15, 2026

When a "use cache" function receives arguments that differ between the cache warming phase and the final prerender, the cache key changes and the Resume Data Cache (RDC) entry from the prospective prerender is missed.

This can happen for various reasons, for example when concurrent async operations push results into a shared array in non-deterministic order, and that array is then passed as an argument to a cached function.

Without a cacheSignal to keep the render alive, the final prerender aborts the cache entry generation, producing an incomplete RSC stream that causes "Connection closed" errors.

This change detects that scenario (an RDC miss during the final prerender where cacheSignal is null) and returns a hanging promise instead of generating a broken cache entry. A warning is logged to help developers identify the non-deterministic arguments. By making the cached function a dynamic hole rather than erroring, the prerender can still complete and produce at least a partial shell if there is a Suspense boundary above. This affects both on-demand prerendering and runtime prefetching.

To avoid false positives, cache keys that were intentionally skipped during the prospective prerender (e.g. because the cached function accessed fallback params) are tracked in a dynamicCacheKeys set on the RDC. During the final prerender, a known dynamic key is returned as a hanging promise early without logging a warning. This also serves as a performance optimization, since it avoids trying to regenerate the entry. This set is intentionally not serialized, as cache misses for dynamic keys should generate fresh entries during the resume at request time.

@unstubbable unstubbable changed the title Warn for non-deterministic "use cache" arguments during final prerender Warn for non-deterministic "use cache" args during final prerender Apr 15, 2026
@nextjs-bot
Copy link
Copy Markdown
Contributor

nextjs-bot commented Apr 15, 2026

Tests Passed

@nextjs-bot
Copy link
Copy Markdown
Contributor

nextjs-bot commented Apr 15, 2026

Stats from current PR

🔴 1 regression

Metric Canary PR Change Trend
node_modules Size 493 MB 494 MB 🔴 +81.9 kB (+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) 455ms 455ms ▁▁██▁
Cold (Ready in log) 444ms 442ms ▇▃▃▂▅
Cold (First Request) 1.139s 1.119s ▁███▁
Warm (Listen) 456ms 457ms █▅▅█▅
Warm (Ready in log) 442ms 443ms ▃▅▇▅▁
Warm (First Request) 347ms 351ms ▃▅▆▁▁
📦 Dev Server (Webpack) (Legacy)

📦 Dev Server (Webpack)

Metric Canary PR Change Trend
Cold (Listen) 456ms 456ms █▁█▁█
Cold (Ready in log) 439ms 439ms ▂▃▃▇█
Cold (First Request) 1.965s 1.950s ▆▆▅▇▇
Warm (Listen) 455ms 457ms ▅▅██▅
Warm (Ready in log) 438ms 439ms ▁▂▂▇▇
Warm (First Request) 1.953s 1.974s ▇█▇██

⚡ Production Builds

Metric Canary PR Change Trend
Fresh Build 3.860s 3.896s █▃▁▂▇
Cached Build 3.911s 3.952s █▁▃▂▆
📦 Production Builds (Webpack) (Legacy)

📦 Production Builds (Webpack)

Metric Canary PR Change Trend
Fresh Build 14.569s 14.586s ▂▁▄▇▇
Cached Build 14.656s 14.731s ▃▂▄▇█
node_modules Size 493 MB 494 MB 🔴 +81.9 kB (+0%) █████
📦 Bundle Sizes

Bundle Sizes

⚡ Turbopack

Client

Main Bundles
Canary PR Change
053b9v5j96uu_.js gzip 8.56 kB N/A -
07rxhp_1_g4mu.js gzip 13.1 kB N/A -
096aa2uod0_wa.js gzip 9.82 kB N/A -
0cz1d0mv5g_q7.js gzip 39.4 kB 39.4 kB
0ea8vo2o6kp_s.js gzip 13.3 kB N/A -
0egovw6-5i1e3.js gzip 8.57 kB N/A -
0fli3_wppnim5.js gzip 12.9 kB N/A -
0gkovryjv05n3.js gzip 154 B N/A -
0ho78l23ixdy7.js gzip 155 B N/A -
0jrg8yoqn4b8e.js gzip 155 B N/A -
0k09jwjeb-tki.js gzip 13.8 kB N/A -
0kb7_ep3r1z0_.js gzip 10.1 kB N/A -
0kmgih16pb2zy.js gzip 9.24 kB N/A -
0lbv96yr1uj-x.js gzip 168 B N/A -
0wxpyd8r-vipl.js gzip 1.47 kB N/A -
1-z_xm7pzpbeh.js gzip 49.2 kB N/A -
11qa6mv6j4f0n.js gzip 160 B N/A -
16lhqjoqbznyg.js gzip 220 B 220 B
16vepdkipri3r.js gzip 8.51 kB N/A -
17n96uu6y1pxq.js gzip 8.6 kB N/A -
1d9ms1y2nit5w.js gzip 65.5 kB N/A -
1elt1qium-r2m.css gzip 115 B 115 B
1odlz7_bqi4gv.js gzip 160 B N/A -
1s-lssnhdqk31.js gzip 156 B N/A -
1xz7fhgdco-x_.js gzip 8.59 kB N/A -
2__-e_ym8n788.js gzip 450 B N/A -
22o6xd9_ywdu6.js gzip 233 B N/A -
2avcdi72oetam.js gzip 156 B N/A -
2h9_xc-4i5_oy.js gzip 152 B N/A -
2k1q_gre6hp0-.js gzip 8.56 kB N/A -
2kvj8yrfznmwx.js gzip 5.69 kB N/A -
2sgqisp4604si.js gzip 8.51 kB N/A -
2u7io78pjtekk.js gzip 156 B N/A -
3_lh2q04233ur.js gzip 70.8 kB N/A -
342ijzvrpe53h.js gzip 2.29 kB N/A -
3p575fh88tejl.js gzip 155 B N/A -
3v9s-fz24f8oo.js gzip 10.4 kB N/A -
3zdh_rd16s0i8.js gzip 157 B N/A -
420pn_b9x-3ir.js gzip 8.62 kB N/A -
43l229j-6khv5.js gzip 156 B N/A -
44un3--wmqiyh.js gzip 7.61 kB N/A -
turbopack-0x..2c1o.js gzip 4.19 kB N/A -
turbopack-0y..rjc9.js gzip 4.2 kB N/A -
turbopack-1k..rbj5.js gzip 4.19 kB N/A -
turbopack-1q.._m7g.js gzip 4.19 kB N/A -
turbopack-2-..4mlc.js gzip 4.17 kB N/A -
turbopack-22..h7oj.js gzip 4.19 kB N/A -
turbopack-25..lf2b.js gzip 4.19 kB N/A -
turbopack-2b..nby5.js gzip 4.19 kB N/A -
turbopack-2m..wm_6.js gzip 4.19 kB N/A -
turbopack-2n.._pfr.js gzip 4.19 kB N/A -
turbopack-3b..ydgx.js gzip 4.19 kB N/A -
turbopack-3e..0ajm.js gzip 4.19 kB N/A -
turbopack-3l..dvr5.js gzip 4.19 kB N/A -
turbopack-3m..qy5_.js gzip 4.19 kB N/A -
03_szppy5nakp.js gzip N/A 49.2 kB -
0arkbdqpxc37i.js gzip N/A 8.6 kB -
0iuamvde1mkye.js gzip N/A 166 B -
0mz-rbiau93_g.js gzip N/A 161 B -
0tvekitj587fh.js gzip N/A 8.51 kB -
0vcsfpshdbpoi.js gzip N/A 157 B -
0z83a1om5rvtt.js gzip N/A 7.61 kB -
1-jqyfc89tixo.js gzip N/A 1.46 kB -
14t1kneseb8th.js gzip N/A 2.3 kB -
16bh0aqv641xv.js gzip N/A 157 B -
192q2suy7rwb_.js gzip N/A 8.56 kB -
1ab2xruymo-oj.js gzip N/A 449 B -
1im7xphq6dxob.js gzip N/A 159 B -
1ng_99sotnhzq.js gzip N/A 159 B -
1qui_nsqw5190.js gzip N/A 157 B -
1wzrm0xjjbzn5.js gzip N/A 10.1 kB -
25a1yz7zua29z.js gzip N/A 13.8 kB -
2f2bp_kexa37v.js gzip N/A 13.3 kB -
2fpb7l1pbce6e.js gzip N/A 65.5 kB -
2i5v1taq97gql.js gzip N/A 8.59 kB -
2jrvadyrjp0yr.js gzip N/A 155 B -
2n3vh0w0tnb_2.js gzip N/A 8.52 kB -
2u_rpxq3tzytl.js gzip N/A 233 B -
2wfbykz1avdns.js gzip N/A 8.56 kB -
32sifemopndov.js gzip N/A 10.4 kB -
368lim5wq0o0r.js gzip N/A 12.9 kB -
37ba9t8u6k_9f.js gzip N/A 156 B -
3aouhp_o4j3ta.js gzip N/A 151 B -
3de283pazbu99.js gzip N/A 9.81 kB -
3drqjohogojbw.js gzip N/A 5.69 kB -
3g8l1m2-o-ewi.js gzip N/A 13.1 kB -
3gp2o_v7jcw6s.js gzip N/A 8.57 kB -
3kab57ds4zwc9.js gzip N/A 157 B -
3qu7g1q2l_x2n.js gzip N/A 9.24 kB -
3vucw_kkxf-6j.js gzip N/A 155 B -
3zycucbo0ee-y.js gzip N/A 70.8 kB -
41p1znhcs5rk8.js gzip N/A 155 B -
43j6fjudq0a3_.js gzip N/A 8.62 kB -
turbopack-02..ejcf.js gzip N/A 4.19 kB -
turbopack-04..36zk.js gzip N/A 4.19 kB -
turbopack-05..ed1i.js gzip N/A 4.19 kB -
turbopack-08..ymqg.js gzip N/A 4.19 kB -
turbopack-1e..75yv.js gzip N/A 4.17 kB -
turbopack-1e..lwrg.js gzip N/A 4.19 kB -
turbopack-1s..5095.js gzip N/A 4.19 kB -
turbopack-1w..leed.js gzip N/A 4.19 kB -
turbopack-1x..h7hw.js gzip N/A 4.19 kB -
turbopack-29..nv35.js gzip N/A 4.19 kB -
turbopack-2d..42om.js gzip N/A 4.2 kB -
turbopack-3f..ewzo.js gzip N/A 4.19 kB -
turbopack-3m..3yye.js gzip N/A 4.19 kB -
turbopack-3z..lv_i.js gzip N/A 4.19 kB -
Total 465 kB 465 kB ⚠️ +39 B

Server

Middleware
Canary PR Change
middleware-b..fest.js gzip 715 B 718 B
Total 715 B 718 B ⚠️ +3 B
Build Details
Build Manifests
Canary PR Change
_buildManifest.js gzip 434 B 429 B 🟢 5 B (-1%)
Total 434 B 429 B ✅ -5 B

📦 Webpack

Client

Main Bundles
Canary PR Change
2637-HASH.js gzip 4.63 kB N/A -
7724.HASH.js gzip 169 B N/A -
8274-HASH.js gzip 61.3 kB N/A -
8817-HASH.js gzip 5.59 kB N/A -
c3500254-HASH.js gzip 62.8 kB N/A -
framework-HASH.js gzip 59.7 kB 59.7 kB
main-app-HASH.js gzip 254 B 255 B
main-HASH.js gzip 39.4 kB 39.4 kB
webpack-HASH.js gzip 1.68 kB 1.68 kB
5887-HASH.js gzip N/A 5.61 kB -
6522-HASH.js gzip N/A 60.7 kB -
6779-HASH.js gzip N/A 4.63 kB -
8854.HASH.js gzip N/A 169 B -
eab920f9-HASH.js gzip N/A 62.8 kB -
Total 235 kB 235 kB ✅ -619 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 193 B 193 B
_error-HASH.js gzip 182 B 182 B
css-HASH.js gzip 333 B 334 B
dynamic-HASH.js gzip 1.81 kB 1.8 kB
edge-ssr-HASH.js gzip 255 B 255 B
head-HASH.js gzip 353 B 349 B 🟢 4 B (-1%)
hooks-HASH.js gzip 384 B 382 B
image-HASH.js gzip 581 B 581 B
index-HASH.js gzip 260 B 259 B
link-HASH.js gzip 2.51 kB 2.51 kB
routerDirect..HASH.js gzip 316 B 318 B
script-HASH.js gzip 386 B 386 B
withRouter-HASH.js gzip 313 B 314 B
1afbb74e6ecf..834.css gzip 106 B 106 B
Total 7.98 kB 7.97 kB ✅ -10 B

Server

Edge SSR
Canary PR Change
edge-ssr.js gzip 126 kB 126 kB
page.js gzip 273 kB 273 kB
Total 399 kB 398 kB ✅ -403 B
Middleware
Canary PR Change
middleware-b..fest.js gzip 616 B 619 B
middleware-r..fest.js gzip 156 B 156 B
middleware.js gzip 44 kB 44.2 kB
edge-runtime..pack.js gzip 842 B 842 B
Total 45.7 kB 45.8 kB ⚠️ +186 B
Build Details
Build Manifests
Canary PR Change
_buildManifest.js gzip 721 B 720 B
Total 721 B 720 B ✅ -1 B
Build Cache
Canary PR Change
0.pack gzip 4.38 MB 4.38 MB
index.pack gzip 113 kB 115 kB 🔴 +2.1 kB (+2%)
index.pack.old gzip 113 kB 114 kB
Total 4.61 MB 4.61 MB ⚠️ +2.56 kB

🔄 Shared (bundler-independent)

Runtimes
Canary PR Change
app-page-exp...dev.js gzip 346 kB 346 kB
app-page-exp..prod.js gzip 192 kB 192 kB
app-page-tur...dev.js gzip 346 kB 346 kB
app-page-tur..prod.js gzip 192 kB 192 kB
app-page-tur...dev.js gzip 342 kB 342 kB
app-page-tur..prod.js gzip 190 kB 190 kB
app-page.run...dev.js gzip 343 kB 343 kB
app-page.run..prod.js gzip 190 kB 190 kB
app-route-ex...dev.js gzip 77 kB 77 kB
app-route-ex..prod.js gzip 52.5 kB 52.5 kB
app-route-tu...dev.js gzip 77.1 kB 77.1 kB
app-route-tu..prod.js gzip 52.6 kB 52.6 kB
app-route-tu...dev.js gzip 76.7 kB 76.7 kB
app-route-tu..prod.js gzip 52.3 kB 52.3 kB
app-route.ru...dev.js gzip 76.6 kB 76.6 kB
app-route.ru..prod.js gzip 52.3 kB 52.3 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 43.9 kB 43.9 kB
pages-api-tu..prod.js gzip 33.5 kB 33.5 kB
pages-api.ru...dev.js gzip 43.9 kB 43.9 kB
pages-api.ru..prod.js gzip 33.5 kB 33.5 kB
pages-turbo....dev.js gzip 53.3 kB 53.3 kB
pages-turbo...prod.js gzip 39.1 kB 39.1 kB
pages.runtim...dev.js gzip 53.3 kB 53.3 kB
pages.runtim..prod.js gzip 39.1 kB 39.1 kB
server.runti..prod.js gzip 62.9 kB 62.9 kB
Total 3.06 MB 3.06 MB ⚠️ +271 B
📝 Changed Files (16 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
View diffs
app-page-exp..ntime.dev.js

Diff too large to display

app-page-exp..time.prod.js

Diff too large to display

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

Diff too large to display

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

Diff too large to display

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

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

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

When a `"use cache"` function receives arguments that differ between the
cache warming phase and the final prerender, the cache key changes and
the Resume Data Cache (RDC) entry from the prospective prerender is
missed.

This can happen for various reasons, for example when concurrent async
operations push results into a shared array in non-deterministic order,
and that array is then passed as an argument to a cached function.

Without a `cacheSignal` to keep the render alive, the final prerender
aborts the cache entry generation, producing an incomplete RSC stream
that causes "Connection closed" errors.

This change detects that scenario (an RDC miss during the final
prerender where `cacheSignal` is `null`) and returns a hanging promise
instead of generating a broken cache entry. A warning is logged to help
developers identify the non-deterministic arguments. By making the
cached function a dynamic hole rather than erroring, the prerender can
still complete and produce at least a partial shell if there is a
Suspense boundary above. This affects both on-demand prerendering and
runtime prefetching.
@unstubbable unstubbable force-pushed the hl/use-cache-handle-non-deterministic-cache-keys branch from 3fc6183 to 9356114 Compare April 15, 2026 18:34
@unstubbable unstubbable force-pushed the hl/use-cache-handle-non-deterministic-cache-keys branch from 9356114 to c5f13e8 Compare April 15, 2026 19:18
@unstubbable unstubbable marked this pull request as ready for review April 15, 2026 19:42
@unstubbable unstubbable requested a review from gnoff April 15, 2026 19:42
break
// fallthrough
case 'prerender-runtime':
if (!cacheSignal) {
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.

I wonder if we're at the point of complexity where a separate implementation for the prospective path is easier. nothing to do about it in this PR just leaving as a drive by thought

Comment on lines +3453 to +3460
at cache (webpack:///<next-src>)
1559 | case 'prerender':
1560 | case 'prerender-runtime':
> 1561 | return makeHangingPromise(
| ^
1562 | workUnitStore.renderSignal,
1563 | workStore.route,
1564 | 'dynamic "use cache"'
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.

Is this really what a user would see? Is this an artifact of testing from within the Next.js repo?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Webpack has a source mapping bug here. We also had bad source locations like this before in this file. I need to change this assertion the same way I did in #90105 for the previous ones. Will do before merging.

Comment on lines +3424 to +3428
at Page (app/use-cache-params/[slug]/page.tsx:1:16)
> 1 | export default async function Page({
| ^
2 | params,
3 | }: {
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.

why did this change?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I think it's because properly returning the hanging promise in the first task of the final prerender now allows React to enhance the owner stack using the hanging promise rejection. Whereas before, we tried to generate a cache entry again due to the RDC cache miss and didn't return the hanging promise in time.

@unstubbable unstubbable enabled auto-merge (squash) April 15, 2026 21:45
@unstubbable unstubbable merged commit 662c6d5 into canary Apr 15, 2026
183 checks passed
@unstubbable unstubbable deleted the hl/use-cache-handle-non-deterministic-cache-keys branch April 15, 2026 21:59
@github-actions github-actions Bot locked as resolved and limited conversation to collaborators Apr 30, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants