perf(build): lazily require page-data helpers in build/utils (#100822)
### What? Defer the requires in `packages/next/src/build/utils.ts` that are only needed while collecting page data, so the main `next build` process no longer evaluates them at startup. The deferred modules are the edge sandbox, `load-components`, the incremental cache helper, app segment collection, the app/pages static-path builders and the HTTP agent setup. ### Why? `build/utils` is loaded eagerly by the main process, through `build/index` and through route discovery → `get-page-static-info`. Its top-level imports pulled in large dependency graphs, including `compiled/edge-runtime`, the incremental cache / response cache, the tracer and OpenTelemetry. Those are only used by `isPageStatic` and the component-loading helpers, which run in the build workers. The main process paid for evaluating them before `build()` started, then never used them. ### How? - Each of those dependencies is now `require`d at its call site, keeping the original call order. The types still come from `import type`. - The side-effect imports at the top of the file (`require-hook`, `node-polyfill-crypto`, `node-environment`) are untouched, so initialization order is the same in the main process and in workers. - After the change, the deferred modules are never loaded in the main process for the whole build. The work is removed, not moved inside `build()`. ### Measurements These are local measurements on one machine, alternating base and change runs. `bench/nested-deps-app-router-many-pages` has 1,000 pages and uses Turbopack. The metric is the time from profiler start to the first sample inside `build()`, taken from `next build --experimental-cpu-prof`. | Pair | Before | After | |---|---:|---:| | 1 | 244.7ms | 200.5ms | | 2 | 243.4ms | 184.4ms | | 3 | 255.6ms | 184.6ms | - **Mean:** 247.9 → 189.8ms (−23%). - **Total build time:** unchanged (~52–55s). - **`cli/next-build.js` require time:** median 251.8 → 207.3ms in a fresh process (30 interleaved samples each). - **Main-process modules, whole build of a small mixed app:** - Turbopack: 424 → 348 `next/dist` modules. - Webpack: 551 → 476 `next/dist` modules. ### Verification - **Smoke builds:** a small app with app router, edge, SSG, pages `getStaticProps`/`getStaticPaths` and middleware produces an identical route table before and after, with both Turbopack and webpack. - `pnpm test-start-turbo test/production/app-dir-edge-runtime-with-wasm test/production/conflicting-ssg-paths` - `pnpm test-start-webpack test/e2e/app-dir/app/standalone.test.ts test/production/build-output` - `packages/next/src/build/analysis/get-page-static-info.test.ts` unit tests - **Not run:** a webpack build of the 1,000-page benchmark. It fails during compilation with a native `std::bad_alloc` on the base commit as well, so it is unrelated to this change. <!-- NEXT_JS_LLM --> <!-- fleet 60ff1dd9-ad2b-411e-8a67-e3842d2a1798 --> 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>
Showing
Please
register
or
sign in
to comment