Skip to content

Turbopack: enable server HMR for app route handlers#91466

Merged
wbinnssmith merged 2 commits into
canaryfrom
wbinnssmith/server-hmr-routes
Mar 19, 2026
Merged

Turbopack: enable server HMR for app route handlers#91466
wbinnssmith merged 2 commits into
canaryfrom
wbinnssmith/server-hmr-routes

Conversation

@wbinnssmith

@wbinnssmith wbinnssmith commented Mar 16, 2026

Copy link
Copy Markdown
Member

App router handlers are already built with the Turbopack runtime, so they can use server HMR. We also already get subscription events for their chunks. However, unlike app pages which use __next_app__.require() for dynamic devModuleCache lookup, route handlers capture userland exports statically in AppRouteRouteModule at construction time. This change makes routes behave a lot like pages, dynamically loading the user's code when requests are made. This way, we can freely invalidate and reload it when changes are made without evicting the entire require cache.

Only next dev uses this lazy evaluation, as next start continues to eagerly import route handlers.

This change removes the isAppPage restriction from usesServerHmr, extending server HMR coverage to all App Router entries (pages and route handlers) built with the Node.js runtime.

For route handlers, we also clear the entry chunk from Node.js require.cache on each rebuild so the next requirePage() call re-executes the entry and obtains fresh module exports from devModuleCache (which HMR updates in-place).

Test Plan: Added an additional e2e test, with-dep

@nextjs-bot nextjs-bot added created-by: Turbopack team PRs by the Turbopack team. tests Turbopack Related to Turbopack with Next.js. type: next labels Mar 16, 2026
@nextjs-bot

nextjs-bot commented Mar 16, 2026

Copy link
Copy Markdown
Contributor

Tests Passed

@codspeed-hq

codspeed-hq Bot commented Mar 16, 2026

Copy link
Copy Markdown

Merging this PR will improve performance by 3.38%

⚡ 1 improved benchmark
✅ 16 untouched benchmarks
⏩ 3 skipped benchmarks1

Performance Changes

Mode Benchmark BASE HEAD Efficiency
Simulation packages-bundle.js[full] 997.9 ms 965.3 ms +3.38%

Comparing wbinnssmith/server-hmr-routes (3e8bcd8) with canary (98e1133)

Open in CodSpeed

Footnotes

  1. 3 benchmarks were skipped, so the baseline results were used instead. If they were deleted from the codebase, click here and archive them to remove them from the performance reports.

@wbinnssmith wbinnssmith force-pushed the wbinnssmith/server-hmr-routes branch from aeb6fd2 to abbe1d9 Compare March 16, 2026 20:55
@nextjs-bot

nextjs-bot commented Mar 16, 2026

Copy link
Copy Markdown
Contributor

Stats from current PR

🔴 1 regression

Metric Canary PR Change Trend
node_modules Size 484 MB 484 MB 🔴 +48.4 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) 506ms 506ms ▁▁█▁▁
Cold (Ready in log) 462ms 461ms ▁▁▇▁▁
Cold (First Request) 867ms 842ms ▂▁█▂▁
Warm (Listen) 457ms 507ms ▁▁█▁▁
Warm (Ready in log) 461ms 462ms ▁▁█▁▁
Warm (First Request) 374ms 335ms ▁▁█▁▁
📦 Dev Server (Webpack) (Legacy)

📦 Dev Server (Webpack)

Metric Canary PR Change Trend
Cold (Listen) 455ms 456ms █▁▁▁█
Cold (Ready in log) 436ms 438ms ▁▁▁▁▁
Cold (First Request) 1.838s 1.859s ████▃
Warm (Listen) 456ms 456ms ▅▅▅▅▁
Warm (Ready in log) 436ms 435ms ▁▁▁▁▁
Warm (First Request) 1.863s 1.862s ▇██▇▃

⚡ Production Builds

Metric Canary PR Change Trend
Fresh Build 4.097s 4.014s ▁▁█▁▁
Cached Build 4.018s 4.051s ▁▁█▁▁
📦 Production Builds (Webpack) (Legacy)

📦 Production Builds (Webpack)

Metric Canary PR Change Trend
Fresh Build 14.271s 14.234s ▄▄▄▄▃
Cached Build 14.442s 14.435s ▅▅▄▄▄
node_modules Size 484 MB 484 MB 🔴 +48.4 kB (+0%) █████
📦 Bundle Sizes

Bundle Sizes

⚡ Turbopack

Client

Main Bundles
Canary PR Change
0-a3rz67ec0it.js gzip 157 B N/A -
0~lwfcrlb4v_9.css gzip 115 B 115 B
00h0nz7r436~l.js gzip 13.3 kB N/A -
00ivb_iunbucu.js gzip 13 kB N/A -
02ku7edzc_wf7.js gzip 450 B N/A -
03~yq9q893hmn.js gzip 39.4 kB 39.4 kB
037mxw~u2_79t.js gzip 154 B N/A -
08wow2p6zxy.b.js gzip 7.61 kB N/A -
092lcb3fqrrf9.js gzip 8.52 kB N/A -
0aj~xs1l1g8tg.js gzip 8.53 kB N/A -
0gob4q88vgv3u.js gzip 156 B N/A -
0h35gmp9u328z.js gzip 8.54 kB N/A -
0h6fkavebp.iz.js gzip 8.47 kB N/A -
0hcg0snee_9wv.js gzip 156 B N/A -
0i8jiw50w3l38.js gzip 154 B N/A -
0ino_yf1k3h6k.js gzip 10.4 kB N/A -
0kkeoe2n.293z.js gzip 160 B N/A -
0l~j-k_rjuult.js gzip 70.8 kB N/A -
0mc16gv2x1bet.js gzip 13.7 kB N/A -
0mcszt6vwd60_.js gzip 154 B N/A -
0mgzv7x.0y719.js gzip 169 B N/A -
0moy~uao4dl.m.js gzip 9.19 kB N/A -
0n5ln2l5jgra~.js gzip 152 B N/A -
0ovzdapbcjgc7.js gzip 65.7 kB N/A -
0p2fwrxw124by.js gzip 160 B N/A -
0q50rtpusjy90.js gzip 2.28 kB N/A -
0smgy2grrrlka.js gzip 8.58 kB N/A -
0t1dzhdfh0txh.js gzip 215 B 215 B
0vt7pofxnk8in.js gzip 10.1 kB N/A -
0zid7o0-vupvp.js gzip 225 B N/A -
1030wmumq.hbq.js gzip 156 B N/A -
11yo3xfd6b147.js gzip 12.9 kB N/A -
13.84hqxl_1p7.js gzip 9.76 kB N/A -
13ddjl2tc8beg.js gzip 153 B N/A -
14_hwphcs58-s.js gzip 48.6 kB N/A -
1554wr-t7p6z-.js gzip 8.55 kB N/A -
15pd.z8aymtma.js gzip 155 B N/A -
15tjst79~qy3_.js gzip 1.46 kB N/A -
15z_v00ne4ud0.js gzip 8.47 kB N/A -
17d_m3p4j9w6r.js gzip 5.62 kB N/A -
17yu~3yiu7d2m.js gzip 8.52 kB N/A -
turbopack-0-..rr~~.js gzip 4.15 kB N/A -
turbopack-01..zoj7.js gzip 4.16 kB N/A -
turbopack-01..cl9..js gzip 4.16 kB N/A -
turbopack-0d..r9ub.js gzip 4.15 kB N/A -
turbopack-0f..1w4v.js gzip 4.15 kB N/A -
turbopack-0g..5lrl.js gzip 4.16 kB N/A -
turbopack-0l..d~my.js gzip 4.14 kB N/A -
turbopack-0l..aco3.js gzip 4.16 kB N/A -
turbopack-0p..4qy0.js gzip 4.17 kB N/A -
turbopack-0p..y0cg.js gzip 4.16 kB N/A -
turbopack-0q..cwm4.js gzip 4.15 kB N/A -
turbopack-0z..uyvd.js gzip 4.16 kB N/A -
turbopack-10..ov9~.js gzip 4.16 kB N/A -
turbopack-15..lg62.js gzip 4.16 kB N/A -
0_.49f9yku.5j.js gzip N/A 48.6 kB -
01fz~yk-xpt_j.js gzip N/A 155 B -
03q~t68gnhli5.js gzip N/A 151 B -
03t__~.5lvgeu.js gzip N/A 5.62 kB -
04d6ll75jqx3r.js gzip N/A 9.19 kB -
04ohz21fsta_x.js gzip N/A 155 B -
0583exyh-yhc7.js gzip N/A 9.76 kB -
072lv63r8dcz~.js gzip N/A 8.58 kB -
075t9dxgbf0m8.js gzip N/A 13.7 kB -
0aayvzj0bc0sv.js gzip N/A 65.7 kB -
0ar1~bwpezfgw.js gzip N/A 13.3 kB -
0b8f8fliy73oo.js gzip N/A 156 B -
0bf-.01jgmps6.js gzip N/A 155 B -
0bh~qxl7qejt_.js gzip N/A 156 B -
0c99mq1ez2bke.js gzip N/A 450 B -
0cq-cmde_ws6u.js gzip N/A 8.47 kB -
0ejf9o-j2g.v8.js gzip N/A 161 B -
0fbbsxh94xk-..js gzip N/A 153 B -
0fwf102w10o9~.js gzip N/A 8.52 kB -
0g1-kfhbbj91x.js gzip N/A 153 B -
0gtmn.q_j1v5r.js gzip N/A 10.4 kB -
0h5~v-tahitcf.js gzip N/A 10.1 kB -
0jvqf.i8i3nyb.js gzip N/A 168 B -
0l3rjj4_ye7_a.js gzip N/A 159 B -
0nclq9z6yzzm5.js gzip N/A 1.46 kB -
0nzumcogektg7.js gzip N/A 8.55 kB -
0p5sjual.nuis.js gzip N/A 13 kB -
0p88ggrxiy7bp.js gzip N/A 7.6 kB -
0s.c-cn5eebrx.js gzip N/A 8.47 kB -
0tna7lg6q4zne.js gzip N/A 12.9 kB -
0votdfxr5fb5u.js gzip N/A 2.28 kB -
0ykl9bs_qj.5..js gzip N/A 8.52 kB -
0zfen0tnxp4gh.js gzip N/A 8.55 kB -
101h7adwwavut.js gzip N/A 70.8 kB -
10wkq1h9jzkg..js gzip N/A 225 B -
11bj8iuigzpg6.js gzip N/A 150 B -
11jeywtke9fil.js gzip N/A 154 B -
149ndfh8zfcaz.js gzip N/A 8.53 kB -
turbopack-0~..8_c3.js gzip N/A 4.15 kB -
turbopack-0~..u794.js gzip N/A 4.15 kB -
turbopack-00..vw48.js gzip N/A 4.16 kB -
turbopack-07.._bvj.js gzip N/A 4.14 kB -
turbopack-08..mj...js gzip N/A 4.16 kB -
turbopack-0b..lw-4.js gzip N/A 4.16 kB -
turbopack-0f..y01v.js gzip N/A 4.15 kB -
turbopack-0p..3gzv.js gzip N/A 4.16 kB -
turbopack-0s..kjmy.js gzip N/A 4.17 kB -
turbopack-0y..8oe7.js gzip N/A 4.16 kB -
turbopack-0y..ft~1.js gzip N/A 4.15 kB -
turbopack-10..noez.js gzip N/A 4.15 kB -
turbopack-11..x5rm.js gzip N/A 4.15 kB -
turbopack-17..oaih.js gzip N/A 4.15 kB -
Total 463 kB 463 kB ✅ -24 B

Server

Middleware
Canary PR Change
middleware-b..fest.js gzip 711 B 714 B
Total 711 B 714 B ⚠️ +3 B
Build Details
Build Manifests
Canary PR Change
_buildManifest.js gzip 433 B 426 B 🟢 7 B (-2%)
Total 433 B 426 B ✅ -7 B

📦 Webpack

Client

Main Bundles
Canary PR Change
5528-HASH.js gzip 5.54 kB N/A -
6280-HASH.js gzip 60.4 kB N/A -
6335.HASH.js gzip 169 B N/A -
912-HASH.js gzip 4.59 kB N/A -
e8aec2e4-HASH.js gzip 62.7 kB N/A -
framework-HASH.js gzip 59.7 kB 59.7 kB
main-app-HASH.js gzip 256 B 254 B
main-HASH.js gzip 39.3 kB 39.2 kB
webpack-HASH.js gzip 1.68 kB 1.68 kB
262-HASH.js gzip N/A 4.59 kB -
2889.HASH.js gzip N/A 169 B -
5602-HASH.js gzip N/A 5.55 kB -
6948ada0-HASH.js gzip N/A 62.7 kB -
9544-HASH.js gzip N/A 61.1 kB -
Total 234 kB 235 kB ⚠️ +673 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 194 B
_error-HASH.js gzip 183 B 180 B 🟢 3 B (-2%)
css-HASH.js gzip 331 B 330 B
dynamic-HASH.js gzip 1.81 kB 1.81 kB
edge-ssr-HASH.js gzip 256 B 256 B
head-HASH.js gzip 351 B 352 B
hooks-HASH.js gzip 384 B 383 B
image-HASH.js gzip 580 B 581 B
index-HASH.js gzip 260 B 260 B
link-HASH.js gzip 2.51 kB 2.51 kB
routerDirect..HASH.js gzip 320 B 319 B
script-HASH.js gzip 386 B 386 B
withRouter-HASH.js gzip 315 B 315 B
1afbb74e6ecf..834.css gzip 106 B 106 B
Total 7.98 kB 7.98 kB ✅ -1 B

Server

Edge SSR
Canary PR Change
edge-ssr.js gzip 125 kB 125 kB
page.js gzip 269 kB 268 kB
Total 394 kB 394 kB ✅ -353 B
Middleware
Canary PR Change
middleware-b..fest.js gzip 617 B 613 B
middleware-r..fest.js gzip 156 B 155 B
middleware.js gzip 44 kB 43.8 kB
edge-runtime..pack.js gzip 842 B 842 B
Total 45.6 kB 45.4 kB ✅ -194 B
Build Details
Build Manifests
Canary PR Change
_buildManifest.js gzip 715 B 718 B
Total 715 B 718 B ⚠️ +3 B
Build Cache
Canary PR Change
0.pack gzip 4.28 MB 4.27 MB
index.pack gzip 110 kB 111 kB 🔴 +1.39 kB (+1%)
index.pack.old gzip 110 kB 110 kB
Total 4.5 MB 4.5 MB ✅ -1.48 kB

🔄 Shared (bundler-independent)

Runtimes
Canary PR Change
app-page-exp...dev.js gzip 333 kB 333 kB
app-page-exp..prod.js gzip 181 kB 181 kB
app-page-tur...dev.js gzip 333 kB 333 kB
app-page-tur..prod.js gzip 181 kB 181 kB
app-page-tur...dev.js gzip 329 kB 329 kB
app-page-tur..prod.js gzip 179 kB 179 kB
app-page.run...dev.js gzip 330 kB 330 kB
app-page.run..prod.js gzip 179 kB 179 kB
app-route-ex...dev.js gzip 76 kB 76.1 kB
app-route-ex..prod.js gzip 51.7 kB 51.8 kB
app-route-tu...dev.js gzip 76 kB 76.1 kB
app-route-tu..prod.js gzip 51.7 kB 51.8 kB
app-route-tu...dev.js gzip 75.6 kB 75.7 kB
app-route-tu..prod.js gzip 51.5 kB 51.6 kB
app-route.ru...dev.js gzip 75.6 kB 75.7 kB
app-route.ru..prod.js gzip 51.5 kB 51.5 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.4 kB 43.4 kB
pages-api-tu..prod.js gzip 33 kB 33 kB
pages-api.ru...dev.js gzip 43.3 kB 43.3 kB
pages-api.ru..prod.js gzip 33 kB 33 kB
pages-turbo....dev.js gzip 52.7 kB 52.7 kB
pages-turbo...prod.js gzip 38.6 kB 38.6 kB
pages.runtim...dev.js gzip 52.7 kB 52.7 kB
pages.runtim..prod.js gzip 38.6 kB 38.6 kB
server.runti..prod.js gzip 62.4 kB 62.4 kB
Total 2.95 MB 2.95 MB ⚠️ +627 B
📝 Changed Files (8 files)

Files with changes:

  • 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-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/92b1dfe72c8d26faa9ecfa9189c8d07a5054f991/next

@wbinnssmith wbinnssmith force-pushed the wbinnssmith/server-hmr-routes branch 10 times, most recently from b24f341 to 991945f Compare March 18, 2026 01:04
App router handlers are already built with the Turbopack runtime, so they can use server HMR. We also already get subscription events for their chunks. However, unlike app pages which use `__next_app__.require()` for dynamic devModuleCache lookup, route handlers capture userland exports statically in AppRouteRouteModule at construction time. This change makes routes behave a lot like pages, dynamically loading the user's code when requests are made. This way, we can freely invalidate and reload it when changes are made without evicting the entire require cache.

This change removes the `isAppPage` restriction from `usesServerHmr`, extending server HMR coverage to all App Router entries (pages and route handlers) built with the Node.js runtime.

For route handlers, we also clear the entry chunk from Node.js `require.cache` on each rebuild so the next `requirePage()` call re-executes the entry and obtains fresh module exports from `devModuleCache` (which HMR updates in-place).

Test Plan: Added an additional e2e test, `with-dep`
@wbinnssmith wbinnssmith force-pushed the wbinnssmith/server-hmr-routes branch 4 times, most recently from 40bf868 to d6949f6 Compare March 18, 2026 19:33
@wbinnssmith wbinnssmith marked this pull request as ready for review March 18, 2026 20:29
@wbinnssmith wbinnssmith requested a review from sokra March 18, 2026 20:30
Comment thread crates/next-core/src/next_app/app_route_entry.rs Outdated
Comment thread packages/next/src/build/templates/app-route.ts
Comment thread packages/next/src/build/templates/app-route.ts Outdated
@wbinnssmith wbinnssmith force-pushed the wbinnssmith/server-hmr-routes branch 3 times, most recently from 92b1dfe to e819839 Compare March 19, 2026 01:37
@wbinnssmith wbinnssmith force-pushed the wbinnssmith/server-hmr-routes branch from e819839 to 3e8bcd8 Compare March 19, 2026 01:44
@wbinnssmith wbinnssmith enabled auto-merge (squash) March 19, 2026 03:49
@wbinnssmith wbinnssmith merged commit 62393b0 into canary Mar 19, 2026
396 of 407 checks passed
@wbinnssmith wbinnssmith deleted the wbinnssmith/server-hmr-routes branch March 19, 2026 03:57
wbinnssmith added a commit that referenced this pull request Mar 28, 2026
…responses

Metadata routes (manifest.ts, robots.ts, sitemap.ts, icon.tsx, etc.) were
incorrectly getting usesServerHmr=true after PR #91466 broadened it from
app-page only to all app entries. This caused clearRequireCache() to skip
deleting their chunks from require.cache and skip __next__clear_chunk_cache__(),
so edits to manifest.ts (and other metadata routes) would not be reflected
on the next request in Turbopack dev mode.

Fix by adding an !isMetadataRoute(entryPage) guard so metadata routes always
take the full cache-clearing path, while regular route handlers continue to
benefit from server HMR.

Co-Authored-By: Claude <noreply@anthropic.com>
wbinnssmith added a commit that referenced this pull request Mar 30, 2026
…responses

Metadata routes (manifest.ts, robots.ts, sitemap.ts, icon.tsx, etc.) were
incorrectly getting usesServerHmr=true after PR #91466 broadened it from
app-page only to all app entries. This caused clearRequireCache() to skip
deleting their chunks from require.cache and skip __next__clear_chunk_cache__(),
so edits to manifest.ts (and other metadata routes) would not be reflected
on the next request in Turbopack dev mode.

Fix by adding an !isMetadataRoute(entryPage) guard so metadata routes always
take the full cache-clearing path, while regular route handlers continue to
benefit from server HMR.

Co-Authored-By: Claude <noreply@anthropic.com>
wbinnssmith added a commit that referenced this pull request Mar 31, 2026
### What?

Metadata routes (`manifest.ts`, `robots.ts`, `sitemap.ts`, `icon.tsx`,
`apple-icon.tsx`, etc.) were not being hot-reloaded in Turbopack dev
mode — changes to those files would not be reflected on subsequent
requests until a full server restart.

### Why?

PR #91466 extended `usesServerHmr = true` in `clearRequireCache()` (in
`hot-reloader-turbopack.ts`) from `app-page` entries only to **all**
`app`-type entries (pages + route handlers). The motivation was correct:
regular route handlers like `app/api/hello/route.ts` use Turbopack's
in-place module update model and benefit from server HMR.

However, metadata routes (`/manifest.webmanifest/route`,
`/robots.txt/route`, etc.) are also `app`-type entries but they are
**not** suitable for in-place server HMR. When `usesServerHmr = true`
for a metadata route, `clearRequireCache()` skips two critical
invalidation steps:

1. Deleting the compiled chunk from `require.cache`
2. Calling `__next__clear_chunk_cache__()`

Without those steps, the old module stays in-memory and all subsequent
requests to `/manifest.webmanifest` (etc.) return the stale content.

### How?

Added an `!isMetadataRoute(entryPage)` guard to the `usesServerHmr`
expression in `clearRequireCache()`. This restores full cache
invalidation for metadata routes on every rebuild while leaving regular
route handler server HMR (added in #91466) intact.

```ts
// Before
const usesServerHmr =
  serverFastRefresh &&
  entryType === 'app' &&
  writtenEndpoint.type !== 'edge'

// After
const usesServerHmr =
  serverFastRefresh &&
  entryType === 'app' &&
  writtenEndpoint.type !== 'edge' &&
  !isMetadataRoute(entryPage)   // ← metadata routes always clear the cache
```

`isMetadataRoute('/manifest.webmanifest/route')` → `true` (excluded from
server HMR)
`isMetadataRoute('/api/hello/route')` → `false` (keeps server HMR, no
regression)

Also added a regression test: `metadata route hmr > reflects manifest.ts
changes on fetch/refresh` in the `server-hmr` test suite, with a
`manifest.ts` fixture that starts at `name: 'Version 0'`. The test
patches the file and asserts the updated JSON is returned on the next
fetch.

Fixes #91981

---------

Co-authored-by: Will Binns-Smith <wbinnssmith@users.noreply.github.com>
Co-authored-by: Claude <noreply@anthropic.com>
wbinnssmith added a commit that referenced this pull request Mar 31, 2026
Rebase of #91318 on top of #91466. The lazy userland loading approach
from #91318 (using a require() thunk instead of a static import)
accurately captures devRequestTimingInternalsEnd — framework time ends
when userland starts executing, not at module load time.

Combined with the async getUserland() mechanism from #91466 for Turbopack
dev HMR, which re-reads devModuleCache on every request so server HMR
updates are picked up without re-executing the entry chunk.

Co-Authored-By: Claude <noreply@anthropic.com>
wbinnssmith added a commit that referenced this pull request Mar 31, 2026
App router handlers are already built with the Turbopack runtime, so
they can use server HMR. **We also already get subscription events for
their chunks.** However, unlike app pages which use
`__next_app__.require()` for dynamic devModuleCache lookup, route
handlers capture userland exports statically in AppRouteRouteModule at
construction time. This change makes routes behave a lot like pages,
dynamically loading the user's code when requests are made. This way, we
can freely invalidate and reload it when changes are made without
evicting the entire require cache.

Only `next dev` uses this lazy evaluation, as `next start` continues to
eagerly import route handlers.

This change removes the `isAppPage` restriction from `usesServerHmr`,
extending server HMR coverage to all App Router entries (pages and route
handlers) built with the Node.js runtime.

For route handlers, we also clear the entry chunk from Node.js
`require.cache` on each rebuild so the next `requirePage()` call
re-executes the entry and obtains fresh module exports from
`devModuleCache` (which HMR updates in-place).

Test Plan: Added an additional e2e test, `with-dep`
wbinnssmith added a commit that referenced this pull request Mar 31, 2026
### What?

Metadata routes (`manifest.ts`, `robots.ts`, `sitemap.ts`, `icon.tsx`,
`apple-icon.tsx`, etc.) were not being hot-reloaded in Turbopack dev
mode — changes to those files would not be reflected on subsequent
requests until a full server restart.

### Why?

PR #91466 extended `usesServerHmr = true` in `clearRequireCache()` (in
`hot-reloader-turbopack.ts`) from `app-page` entries only to **all**
`app`-type entries (pages + route handlers). The motivation was correct:
regular route handlers like `app/api/hello/route.ts` use Turbopack's
in-place module update model and benefit from server HMR.

However, metadata routes (`/manifest.webmanifest/route`,
`/robots.txt/route`, etc.) are also `app`-type entries but they are
**not** suitable for in-place server HMR. When `usesServerHmr = true`
for a metadata route, `clearRequireCache()` skips two critical
invalidation steps:

1. Deleting the compiled chunk from `require.cache`
2. Calling `__next__clear_chunk_cache__()`

Without those steps, the old module stays in-memory and all subsequent
requests to `/manifest.webmanifest` (etc.) return the stale content.

### How?

Added an `!isMetadataRoute(entryPage)` guard to the `usesServerHmr`
expression in `clearRequireCache()`. This restores full cache
invalidation for metadata routes on every rebuild while leaving regular
route handler server HMR (added in #91466) intact.

```ts
// Before
const usesServerHmr =
  serverFastRefresh &&
  entryType === 'app' &&
  writtenEndpoint.type !== 'edge'

// After
const usesServerHmr =
  serverFastRefresh &&
  entryType === 'app' &&
  writtenEndpoint.type !== 'edge' &&
  !isMetadataRoute(entryPage)   // ← metadata routes always clear the cache
```

`isMetadataRoute('/manifest.webmanifest/route')` → `true` (excluded from
server HMR)
`isMetadataRoute('/api/hello/route')` → `false` (keeps server HMR, no
regression)

Also added a regression test: `metadata route hmr > reflects manifest.ts
changes on fetch/refresh` in the `server-hmr` test suite, with a
`manifest.ts` fixture that starts at `name: 'Version 0'`. The test
patches the file and asserts the updated JSON is returned on the next
fetch.

Fixes #91981

---------

Co-authored-by: Will Binns-Smith <wbinnssmith@users.noreply.github.com>
Co-authored-by: Claude <noreply@anthropic.com>
@github-actions github-actions Bot added the locked label Apr 2, 2026
@github-actions github-actions Bot locked as resolved and limited conversation to collaborators Apr 2, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

created-by: Turbopack team PRs by the Turbopack team. locked tests Turbopack Related to Turbopack with Next.js. type: next

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants