next.js
1010711b - dev: skip source map URLs for server-side React fake functions (#101522)

Commit
1 day ago
dev: skip source map URLs for server-side React fake functions (#101522) ### What? In `next dev`, the server-side React Flight clients (SSR, `"use cache"`, encryption, segment data, instant validation) no longer give React a source map URL for the "fake functions" React creates for Server Component stack frames. This only happens when **no Node.js inspector is attached** and the frame's script is a **plain absolute POSIX path**, i.e. a chunk loaded from disk. Every other case keeps the existing behavior. The change is in `findSourceMapURLDEV` (`packages/next/src/server/lib/source-maps.ts`). ### Why? Part of #100250 (making Turbopack `next dev` / `next build` faster and scale beyond 16 cores). For every Server Component stack frame it replays, React's dev Flight client `eval`s a fake function, padded with as many newlines as the frame's line in the chunk. When a source map URL is available, the script ends in `//# sourceURL=about://React/<env>/<chunk>?<n>` and `//# sourceMappingURL=<url>`. `next dev` runs Node.js with `--enable-source-maps`, so Node.js eagerly records source map metadata for each of these scripts, including the length of every line of the padding. With many frames this is a large amount of single-threaded work and memory on the SSR side. Without a source map URL, React names the fake function after the chunk it points into instead of a virtual URL. The positions are already padded to match the chunk, so the frames are mapped lazily through the chunk's own source map, which Node.js already has. The same is true for Next's own stack handling (`patch-error-inspect.ts` and the dev overlay middlewares). Owner stacks, replayed logs, terminal errors and overlay frames stay mapped to the original source. ### How? `findSourceMapURLDEV` returns `null` when both of these hold: - `inspector.url() === undefined`. With `next dev --inspect` or an attached debugger the source map URL is kept, because a debugger can only map the fake function scripts themselves through it. React caches fake functions, so frames created before a debugger attached late stay unmapped in that debugger. - The script name starts with `/` and has no `?`, `#` or `%`. Everything else keeps its source map URL: - **Modules evaluated by server HMR** (`file:///…chunk.js?id=%5Bproject%5D…`): without the `about://React` wrapper, React `encodeURI`s the already-encoded URL again (`%5B` → `%255B`), and the frame would no longer match the script Node.js has a source map for. `test/development/app-dir/dev-validation-worker-source-maps` catches this. - **Windows paths** (React doesn't turn them into `file:` URLs) and **`webpack-internal://` URLs**. These get no speedup from this PR. - **Edge runtime**: unchanged. **Proper upstream fixes, not part of this PR:** - React could use the virtual `about://React/…` URL independently of whether a source map URL is given, and make it round-trip losslessly (see also react/react#37105). - Node.js could compute the line lengths of `sourceMappingURL` scripts lazily instead of eagerly. ### Tests - New `server-source-maps` case "Server Component frames in SSR owner stacks are sourcemapped": the terminal shows `ServerComponent (app/ssr-owner-stack/page.tsx:4:10)` / `Page (…:8:10)`. - Dev overlay mapping of Server Component owner frames in SSR is already covered by `cache-components-errors/client-hook-abort-reasons` (`Page app/…/page.tsx (8:7)` in the "Blocking Route" redbox), which passes with this change. - Existing suites pass with Turbopack dev: - `server-source-maps` (incl. the `--inspect` debugger suite `fake-frame-source-maps` and edge) - `dev-validation-worker-source-maps` - `owner-stack*`, `capture-console-error-owner-stack` - `source-mapping`, `dynamic-error-trace`, `replayed-internal-errors` - `cache-components-errors/client`, `acceptance-app/rsc-runtime-errors`, `dedupe-rsc-error-log` - With webpack dev, `server-source-maps › handles invalid sourcemaps gracefully` fails locally the same way on canary without this change (pre-existing). ### Benchmarks (`next dev`) **Setup** - Linux VM with 32 vCPUs (16 physical cores × 2 SMT), 64 GB RAM, Node.js 24. - next-swc built natively in release mode; the same binary for both arms. Before = canary `4d51724b`, after = canary + this PR. - 16 CPUs = `taskset -c 0,2,…,30` (one thread per physical core); 32 CPUs = `taskset -c 0-31`. `TURBO_TASKS_AVAILABLE_PARALLELISM` set to match, `TURBO_ENGINE_IGNORE_DIRTY=1`. **Method** - Before and after are interleaved, with the order alternating each run. 5 runs per cell, mean ± stddev. - **cold**: `.next` deleted. **warm**: restart on the persistent cache from the cold session. - First request = first `GET` after "Ready". Second route = a different route right after. - HMR = edit of the first route's page → first SSR response containing the edit, mean of 3 edits. Browser-side hot update is not included. - Peak memory = summed RSS of the `next dev` process tree, sampled every 100 ms until exit (incl. graceful shutdown), plus the sum of each process's kernel high-water mark (`VmHWM`). - An A/A run (two identical builds) on large-app-router showed noise of up to ±2.8% on these memory metrics. **Apps** - `bench/nested-deps-app-router-many-pages`: `/page77/server-and-client-components`, then `/page78/…`. - `bench/large-app-router` (from #101502, default size): `/s0/r1`, then `/s1/r1`. **nested-deps-app-router-many-pages** | CPUs | Cache | First request | Second route | HMR (edit → updated SSR) | Peak RSS (tree) | Σ VmHWM | |---|---|---|---|---|---|---| | 16 | cold | 17.04 ± 0.73s → **8.92 ± 0.58s (-48%)** | 4.42 ± 0.43s → 3.26 ± 0.38s (-26%) | 1.22 ± 0.16s → 1.28 ± 0.11s (+5%) | 4.14 ± 0.04 GB → **2.26 ± 0.11 GB (-45%)** | 4.14 ± 0.04 GB → 2.26 ± 0.11 GB (-45%) | | 16 | warm | 11.38 ± 0.52s → **4.63 ± 2.19s (-59%)** | 2.14 ± 1.88s → 3.92 ± 0.46s (+84%)¹ | 1.40 ± 0.13s → 1.30 ± 0.02s (-7%) | 3.85 ± 0.15 GB → **1.85 ± 0.07 GB (-52%)** | 3.85 ± 0.15 GB → 1.85 ± 0.07 GB (-52%) | | 32 | cold | 16.86 ± 1.86s → **8.92 ± 0.90s (-47%)** | 4.66 ± 0.46s → 3.86 ± 0.83s (-17%) | 1.34 ± 0.10s → 1.32 ± 0.04s (-1%) | 4.13 ± 0.03 GB → **2.46 ± 0.27 GB (-40%)** | 4.14 ± 0.03 GB → 2.47 ± 0.27 GB (-40%) | | 32 | warm | 11.40 ± 0.63s → **4.46 ± 1.80s (-61%)** | 4.42 ± 0.41s → 4.00 ± 0.90s (-9%) | 1.29 ± 0.05s → 1.26 ± 0.09s (-2%) | 4.03 ± 0.01 GB → **1.89 ± 0.07 GB (-53%)** | 4.03 ± 0.01 GB → 1.89 ± 0.08 GB (-53%) | **large-app-router** | CPUs | Cache | First request | Second route | HMR (edit → updated SSR) | Peak RSS (tree) | Σ VmHWM | |---|---|---|---|---|---|---| | 16 | cold | 5.33 ± 0.45s → 5.06 ± 0.25s (-5%) | 0.81 ± 0.09s → 0.75 ± 0.04s (-7%) | 0.99 ± 0.06s → 0.97 ± 0.08s (-2%) | 1.24 ± 0.03 GB → 1.26 ± 0.02 GB (+1%) | 1.25 ± 0.03 GB → 1.26 ± 0.02 GB (+1%) | | 16 | warm | 5.28 ± 0.42s → 5.12 ± 0.38s (-3%) | 0.74 ± 0.08s → 0.74 ± 0.06s (-0%) | 0.95 ± 0.03s → 1.02 ± 0.12s (+7%) | 1.23 ± 0.02 GB → 1.23 ± 0.02 GB (-0%) | 1.23 ± 0.02 GB → 1.23 ± 0.02 GB (-0%) | | 32 | cold | 4.90 ± 0.10s → 4.91 ± 0.34s (+0%) | 0.67 ± 0.03s → 0.72 ± 0.09s (+8%) | 0.95 ± 0.04s → 0.97 ± 0.07s (+2%) | 1.35 ± 0.01 GB → 1.32 ± 0.03 GB (-2%) | 1.36 ± 0.01 GB → 1.32 ± 0.03 GB (-2%) | | 32 | warm | 4.92 ± 0.27s → 4.88 ± 0.30s (-1%) | 0.70 ± 0.06s → 0.71 ± 0.07s (+1%) | 0.93 ± 0.02s → 0.92 ± 0.02s (-1%) | 1.31 ± 0.02 GB → 1.31 ± 0.03 GB (-0%) | 1.32 ± 0.02 GB → 1.31 ± 0.03 GB (-0%) | **Summary** - On nested-deps, the first request is ~2× faster cold and ~2.5× faster warm, and peak memory drops 40–53%. - On large-app-router, no difference is detected. Its pages produce few Server Component frames, and all deltas are within run-to-run noise, including memory (largest increase +1.0%, Welch t = 0.73, below the A/A noise). - **No peak-memory regression was detected** in any cell. ¹ The warm second route of nested-deps is bimodal in both arms. It is either ~0.8 s, when the route was restored from the persistent cache, or ~4 s, when it was recompiled. Three of the five "before" runs at 16 CPUs happened to hit the cache. This is not attributable to this change. <!-- NEXT_JS_LLM --> <!-- fleet bebe8a66-0f6a-4e5b-8044-f585c38a1403 --> --------- Co-authored-by: vercel-fleet-prod[bot] <318278635+vercel-fleet-prod[bot]@users.noreply.github.com> Co-authored-by: Tobias Koppers <1365881+sokra@users.noreply.github.com>
Author
Parents
Loading