Skip to content

Fix stale dev 'use cache' for cookieless requests and route handlers#96022

Merged
unstubbable merged 1 commit into
hl/hmr-use-cache-failing-testsfrom
hl/avoid-hmr-refresh-hash-cookie
Jul 22, 2026
Merged

Fix stale dev 'use cache' for cookieless requests and route handlers#96022
unstubbable merged 1 commit into
hl/hmr-use-cache-failing-testsfrom
hl/avoid-hmr-refresh-hash-cookie

Conversation

@unstubbable

@unstubbable unstubbable commented Jul 21, 2026

Copy link
Copy Markdown
Contributor

In development, editing a file does not evict existing 'use cache' entries; an entry is invalidated only by re-keying, and the key includes an HMR refresh hash. That hash was delivered through the __next_hmr_refresh_hash__ cookie, which the browser HMR client set from each server-components change and sent back on later requests. Two cases were therefore broken. First, any request that does not carry the cookie (a curl, a plain fetch, a fresh browser profile, a second device) served stale cached content after an edit, for both pages and route handlers. Second, on Turbopack a route handler served stale content even when fetched from a page whose HMR client had the cookie, because a route-handler edit never advanced the hash there and so never updated the cookie; on webpack that case happens to work, leaving only cookieless route handlers broken.

The hash is server-authored to begin with (webpack's stats.hash, Turbopack's hmrHash counter), so this change sources it directly on the server and removes the cookie entirely. The hot-reloader exposes it, and base-server attaches it to each request with addRequestMeta, next to serverComponentsHmrCache, so it reaches every render, including the internal Cache Components validation and warmup renders. It is threaded onto the request store the same way serverComponentsHmrCache is, so 'use cache' is invalidated for every client regardless of whether it ran the HMR client. Route handlers, whose NextRequest does not carry request meta, receive it through the app-route template's renderOpts.

Editing a route handler also has to advance the hash in the first place. On webpack it already did, but on Turbopack the app-route entry wired no change subscription, so a route edit recompiled the module without advancing hmrHash; it now subscribes to the route endpoint so an edit advances the hash without broadcasting a page refresh. That subscription is also made the only thing that advances hmrHash: the config-invalidation and server Fast Refresh reset paths previously bumped it even without a content change, for example on a route's first load, and with the hash now read synchronously instead of bounced back through a cookie, such a bump would land between a route's cold write and warm read and evict a still-valid entry. The getter returns the counter unconditionally so the key is present and stable for every request, matching webpack's always-present stats.hash.

With the cookie gone, the hash field on the SERVER_COMPONENT_CHANGES HMR message has no remaining consumer, so it is dropped from the message type and both bundlers.

This is a short-term stopgap until the granular implementation hash (codeHash plus runtimeEnvVars plus the Next.js version) is used in next dev, which invalidates server-authoritatively by construction and will let this mechanism be removed.

Tip

Best reviewed with hidden whitespace changes.

closes NAR-894


Stack created with GitHub Stacks CLIGive Feedback 💬

@github-actions

github-actions Bot commented Jul 21, 2026

Copy link
Copy Markdown
Contributor

Tests Passed

Commit: 72eadbc

@github-actions

github-actions Bot commented Jul 21, 2026

Copy link
Copy Markdown
Contributor

Stats from current PR

🔴 2 regressions

Metric Canary PR Change Trend
node_modules Size 527 MB 527 MB 🔴 +121 kB (+0%) █████
Webpack Warm (First Request) 3.344s 3.509s 🔴 +165ms (+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) 763ms 761ms ▂▇▂▇▂
Cold (Ready in log) 758ms 758ms ▁█▁█▁
Cold (First Request) 1.278s 1.286s ▂█▁▇▁
Warm (Listen) 762ms 762ms ▁▇▁▇▁
Warm (Ready in log) 760ms 764ms ▁▇▁▇▁
Warm (First Request) 1.283s 1.289s ▁▇▁▇▂
📦 Dev Server (Webpack) (Legacy)

📦 Dev Server (Webpack)

Metric Canary PR Change Trend
Cold (Listen) 812ms 812ms ▁▂█▂▂
Cold (Ready in log) 788ms 782ms ▁▁█▁▁
Cold (First Request) 3.371s 3.390s ▁▁█▁▁
Warm (Listen) 814ms 813ms ▁▂█▂▂
Warm (Ready in log) 789ms 806ms ▁▁█▁▁
Warm (First Request) 3.344s 3.509s 🔴 +165ms (+5%) ▁▁█▁▁

⚡ Production Builds

Metric Canary PR Change Trend
Fresh Build 4.894s 4.986s ▁█▁▇▃
Cached Build 4.996s 5.048s ▃▇▁▇▃
📦 Production Builds (Webpack) (Legacy)

📦 Production Builds (Webpack)

Metric Canary PR Change Trend
Fresh Build 26.402s 26.025s ▁▁█▁▁
Cached Build 26.008s 26.249s ▁▁█▁▁
node_modules Size 527 MB 527 MB 🔴 +121 kB (+0%) █████
📦 Bundle Sizes

Bundle Sizes

⚡ Turbopack

Client

Main Bundles
Canary PR Change
03frh1tm1pkvs.js gzip 65.6 kB N/A -
0a-p0meqxynjz.js gzip 10.3 kB N/A -
0avtke4yefj4-.js gzip 13.1 kB N/A -
0ban--bx2qns6.js gzip 155 B N/A -
0cz1d0mv5g_q7.js gzip 39.4 kB 39.4 kB
0ke29q58efn1a.js gzip 156 B N/A -
0m3evdsbel0p0.js gzip 153 B N/A -
0p6ivy9lb7-ut.js gzip 157 B N/A -
0pt5of3ch6d2u.js gzip 13.6 kB N/A -
0sug3ihj23mvn.js gzip 154 B N/A -
0xnsdo-6y9r3k.js gzip 5.72 kB N/A -
0y25v1ej71uft.js gzip 155 B N/A -
0yi7is8u4covm.js gzip 8.77 kB N/A -
12zd-d7xicsb4.js gzip 158 B N/A -
1elt1qium-r2m.css gzip 115 B 115 B
1g2ea05dkmmwi.js gzip 45.2 kB N/A -
1mqxdmso4160h.js gzip 160 B N/A -
1prv-wg6jgtt2.js gzip 8.77 kB N/A -
1t47vod02e_4a.js gzip 8.75 kB N/A -
1tf1phijqlx9j.js gzip 220 B 220 B
1tw002sq5bo4l.js gzip 10 kB N/A -
1wlerik1n7zbr.js gzip 8.7 kB N/A -
2d91wcgx6lf2k.js gzip 153 B N/A -
2koflviradwt3.js gzip 7.4 kB N/A -
2lviwqiqxm904.js gzip 3.52 kB N/A -
2ocpv0tylryvn.js gzip 160 B N/A -
2pevdqjpbf3xk.js gzip 9.45 kB N/A -
2qd2anu9eizlz.js gzip 8.75 kB N/A -
2sv39ae5_8vfj.js gzip 8.81 kB N/A -
2utx6n7w65xqa.js gzip 8.7 kB N/A -
2xsvwiy3-s105.js gzip 156 B N/A -
2zhsfsomo6g1v.js gzip 169 B N/A -
3-ukivglqkfeq.js gzip 8.78 kB N/A -
325xisqrylugu.js gzip 450 B N/A -
3jc9ghvds1spf.js gzip 2.29 kB N/A -
3juwkvkgvywfe.js gzip 10.6 kB N/A -
3kbove7q58uqh.js gzip 13.2 kB N/A -
3qn7vntp30gaj.js gzip 1.47 kB N/A -
3s5fbzg2qit8e.js gzip 156 B N/A -
3xtfvhlgn22av.js gzip 71.1 kB N/A -
turbopack-0z.._-wk.js gzip 3.78 kB N/A -
turbopack-0z..g5ya.js gzip 3.8 kB N/A -
turbopack-10..hlo9.js gzip 3.8 kB N/A -
turbopack-11..yksz.js gzip 3.81 kB N/A -
turbopack-1k..5le3.js gzip 3.8 kB N/A -
turbopack-2p..8e3n.js gzip 3.8 kB N/A -
turbopack-2s..l7od.js gzip 3.8 kB N/A -
turbopack-2u..c9m0.js gzip 3.8 kB N/A -
turbopack-39..3ysu.js gzip 3.8 kB N/A -
turbopack-3i..u_md.js gzip 3.8 kB N/A -
turbopack-3l..z_a8.js gzip 3.81 kB N/A -
turbopack-3w..t926.js gzip 3.8 kB N/A -
turbopack-3y..v370.js gzip 3.82 kB N/A -
turbopack-40..h7ed.js gzip 3.8 kB N/A -
030a1njeh5b77.js gzip N/A 10 kB -
0ceaj3rmii5dl.js gzip N/A 157 B -
0ot0qt5np20h7.js gzip N/A 8.78 kB -
0po7u01wwhl8s.js gzip N/A 157 B -
0xf_8wxc6n71z.js gzip N/A 45.1 kB -
12m9rr7_7cexd.js gzip N/A 8.7 kB -
15-b7iytymgp5.js gzip N/A 65.6 kB -
186r3y_rfvr0b.js gzip N/A 8.78 kB -
19v97848yrjp7.js gzip N/A 8.81 kB -
1b9oo_n-opj3l.js gzip N/A 156 B -
1mvdo2etbyclo.js gzip N/A 156 B -
1nd_0rwgz2ozk.js gzip N/A 9.45 kB -
1oi9pltr_e10m.js gzip N/A 10.6 kB -
1qow5vb4es1bl.js gzip N/A 152 B -
1rd9tzqgzsz2w.js gzip N/A 8.77 kB -
1rewgj8du8r7b.js gzip N/A 157 B -
1rq1g_cwv642d.js gzip N/A 10.3 kB -
1uojw_uabeg93.js gzip N/A 450 B -
1w-7lusgyl81y.js gzip N/A 8.7 kB -
2-4bvun4utbsg.js gzip N/A 157 B -
20pls2cco-oyl.js gzip N/A 71.1 kB -
21h56wdn9zddo.js gzip N/A 8.75 kB -
21kc-4x8zf_oh.js gzip N/A 155 B -
22nt-vwpfkpq5.js gzip N/A 155 B -
2j9dbydew6g_b.js gzip N/A 162 B -
2lg10omv7xu14.js gzip N/A 3.52 kB -
2ovff1533zvno.js gzip N/A 13.2 kB -
2s4z8jy0z7v93.js gzip N/A 1.46 kB -
2y6gb5bs4mnam.js gzip N/A 8.75 kB -
2zvoj8k1z3cei.js gzip N/A 7.41 kB -
341h71ln1jpl3.js gzip N/A 2.29 kB -
34ttpt_n7605h.js gzip N/A 5.72 kB -
36ckfq9o-ys-a.js gzip N/A 13.1 kB -
37pm_isc9xj9i.js gzip N/A 163 B -
3vdh6_u50pu5t.js gzip N/A 13.6 kB -
3zbnmofunn_bw.js gzip N/A 158 B -
45148p5lps61x.js gzip N/A 170 B -
turbopack-00..m1m7.js gzip N/A 3.81 kB -
turbopack-0d..4kzx.js gzip N/A 3.81 kB -
turbopack-0z..p79s.js gzip N/A 3.81 kB -
turbopack-1c..77uv.js gzip N/A 3.81 kB -
turbopack-1i..3kn6.js gzip N/A 3.81 kB -
turbopack-2d..v5q3.js gzip N/A 3.82 kB -
turbopack-2k..d454.js gzip N/A 3.81 kB -
turbopack-2n..bd6n.js gzip N/A 3.79 kB -
turbopack-2t..j9f5.js gzip N/A 3.81 kB -
turbopack-3_..5w5s.js gzip N/A 3.81 kB -
turbopack-39..bm8d.js gzip N/A 3.81 kB -
turbopack-3e..0dhh.js gzip N/A 3.8 kB -
turbopack-3t..czon.js gzip N/A 3.81 kB -
turbopack-3v..bfyi.js gzip N/A 3.81 kB -
Total 448 kB 448 kB ⚠️ +71 B

Server

Middleware
Canary PR Change
middleware-b..fest.js gzip 778 B 777 B
Total 778 B 777 B ✅ -1 B
Build Details
Build Manifests
Canary PR Change
_buildManifest.js gzip 436 B 435 B
Total 436 B 435 B ✅ -1 B

📦 Webpack

Client

Main Bundles
Canary PR Change
3322-HASH.js gzip 63.7 kB N/A -
4191.HASH.js gzip 169 B N/A -
7920-HASH.js gzip 4.68 kB N/A -
9784-HASH.js gzip 5.63 kB N/A -
b1ad9f4c-HASH.js gzip 62.9 kB N/A -
framework-HASH.js gzip 59.7 kB 59.7 kB
main-app-HASH.js gzip 254 B 253 B
main-HASH.js gzip 40 kB 40 kB
webpack-HASH.js gzip 1.68 kB 1.68 kB
3577.HASH.js gzip N/A 168 B -
578-HASH.js gzip N/A 64.4 kB -
8590-HASH.js gzip N/A 5.61 kB -
9750-HASH.js gzip N/A 4.69 kB -
a8984546-HASH.js gzip N/A 62.9 kB -
Total 239 kB 239 kB ⚠️ +609 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 128 kB 128 kB
page.js gzip 287 kB 287 kB
Total 415 kB 414 kB ✅ -655 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.5 kB 45 kB 🟢 544 B (-1%)
edge-runtime..pack.js gzip 842 B 842 B
Total 47.2 kB 46.6 kB ✅ -543 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.82 MB 4.82 MB
index.pack gzip 119 kB 119 kB
index.pack.old gzip 120 kB 118 kB 🟢 2.64 kB (-2%)
Total 5.06 MB 5.05 MB ✅ -6.21 kB

🔄 Shared (bundler-independent)

Runtimes
Canary PR Change
app-page-exp...dev.js gzip 366 kB 366 kB
app-page-exp..prod.js gzip 202 kB 202 kB
app-page-tur...dev.js gzip 365 kB 365 kB
app-page-tur..prod.js gzip 202 kB 202 kB
app-page-tur...dev.js gzip 362 kB 362 kB
app-page-tur..prod.js gzip 200 kB 200 kB
app-page.run...dev.js gzip 362 kB 362 kB
app-page.run..prod.js gzip 200 kB 200 kB
app-route-ex...dev.js gzip 81.7 kB 81.8 kB
app-route-ex..prod.js gzip 55.6 kB 55.6 kB
app-route-tu...dev.js gzip 81.8 kB 81.8 kB
app-route-tu..prod.js gzip 55.6 kB 55.6 kB
app-route-tu...dev.js gzip 81.4 kB 81.4 kB
app-route-tu..prod.js gzip 55.4 kB 55.4 kB
app-route.ru...dev.js gzip 81.3 kB 81.4 kB
app-route.ru..prod.js gzip 55.3 kB 55.4 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 45.4 kB 45.4 kB
pages-api-tu..prod.js gzip 34.1 kB 34.1 kB
pages-api.ru...dev.js gzip 45.4 kB 45.4 kB
pages-api.ru..prod.js gzip 34.1 kB 34.1 kB
pages-turbo....dev.js gzip 54.9 kB 54.9 kB
pages-turbo...prod.js gzip 39.7 kB 39.7 kB
pages.runtim...dev.js gzip 54.8 kB 54.8 kB
pages.runtim..prod.js gzip 39.7 kB 39.7 kB
server.runti..prod.js gzip 67.8 kB 67.8 kB
use-cache-pr...dev.js gzip 71.6 kB 71.6 kB
use-cache-pr...dev.js gzip 71.6 kB 71.6 kB
use-cache-pr...dev.js gzip 69.9 kB 69.9 kB
use-cache-pr...dev.js gzip 69.9 kB 69.9 kB
Total 3.51 MB 3.51 MB ⚠️ +150 B
📝 Changed Files (21 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
  • server.runtime.prod.js
  • use-cache-pr..ntime.dev.js
  • use-cache-pr..ntime.dev.js
  • use-cache-pr..ntime.dev.js
  • ... and 1 more
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

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

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/72eadbcce5b7e4cc88cdb539980e2051cdd6d850/next

Commit: 72eadbc

In development, editing a file does not evict existing `'use cache'`
entries; an entry is invalidated only by re-keying, and the key includes
an HMR refresh hash. That hash was delivered through the
`__next_hmr_refresh_hash__` cookie, which the browser HMR client set
from each server-components change and sent back on later requests. Two
cases were therefore broken. First, any request that does not carry the
cookie (a `curl`, a plain `fetch`, a fresh browser profile, a second
device) served stale cached content after an edit, for both pages and
route handlers. Second, on Turbopack a route handler served stale
content even when fetched from a page whose HMR client had the cookie,
because a route-handler edit never advanced the hash there and so never
updated the cookie; on webpack that case happens to work, leaving only
cookieless route handlers broken.

The hash is server-authored to begin with (webpack's `stats.hash`,
Turbopack's `hmrHash` counter), so this change sources it directly on
the server and removes the cookie entirely. The hot-reloader exposes it,
and `base-server` attaches it to each request with `addRequestMeta`,
next to `serverComponentsHmrCache`, so it reaches every render,
including the internal Cache Components validation and warmup renders.
It is threaded onto the request store the same way
`serverComponentsHmrCache` is, so `'use cache'` is invalidated for every
client regardless of whether it ran the HMR client. Route handlers,
whose `NextRequest` does not carry request meta, receive it through the
app-route template's `renderOpts`.

Editing a route handler also has to advance the hash in the first place.
On webpack it already did, but on Turbopack the `app-route` entry wired
no change subscription, so a route edit recompiled the module without
advancing `hmrHash`; it now subscribes to the route endpoint so an edit
advances the hash without broadcasting a page refresh. That subscription
is also made the only thing that advances `hmrHash`: the
config-invalidation and server Fast Refresh reset paths previously
bumped it even without a content change, for example on a route's first
load, and with the hash now read synchronously instead of bounced back
through a cookie, such a bump would land between a route's cold write
and warm read and evict a still-valid entry. The getter returns the
counter unconditionally so the key is present and stable for every
request, matching webpack's always-present `stats.hash`.

With the cookie gone, the `hash` field on the `SERVER_COMPONENT_CHANGES`
HMR message has no remaining consumer, so it is dropped from the message
type and both bundlers.

This is a short-term stopgap until the granular implementation hash
(`codeHash` plus `runtimeEnvVars` plus the Next.js version) is used in
`next dev`, which invalidates server-authoritatively by construction and
will let this mechanism be removed.
@unstubbable
unstubbable force-pushed the hl/avoid-hmr-refresh-hash-cookie branch from d3e6dca to 72eadbc Compare July 21, 2026 22:14
Comment thread packages/next/src/server/dev/hot-reloader-webpack.ts
@unstubbable
unstubbable marked this pull request as ready for review July 22, 2026 07:16
@unstubbable
unstubbable requested a review from gaojude July 22, 2026 07:17
@unstubbable
unstubbable merged commit 286862e into canary Jul 22, 2026
129 checks passed
@unstubbable
unstubbable deleted the hl/avoid-hmr-refresh-hash-cookie branch July 22, 2026 13:50
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.

2 participants