Turbopack: only search <head> for existing chunk tags (#100175)
### What? Turbopack's browser runtime now looks for existing chunk tags in `<head>` only, and `turbopack-dev-server` puts its chunk scripts into `<head>` as `<script async>` instead of at the end of `<body>`. Follow-up to #100047, answering [this review comment](https://github.com/vercel/next.js/pull/100047#discussion_r4267298397). ### Why? #100047 removed the document-wide `querySelectorAll` from the chunk-tag lookup, but the lookup still iterated every `<script>`/`<link>` in the document, so its cost grew with the page content. I checked where chunk tags actually land in Next.js (Chromium; hard loads, App Router soft navigation, Pages Router, Suspense streaming, `loading.tsx`, PPR in prod): - The tags the runtime looks up are all in `<head>`. - The tags in `<body>` are the bootstrap `<script>` (a runtime chunk, never looked up), late `<link rel=preload>` tags for `next/dynamic` chunks in streamed boundaries, and, under PPR, a second copy of the `noModule` polyfills script. `turbopack-dev-server` was the one producer that put chunk scripts into `<body>`. Synthetic Chromium microbenchmark of the compiled lookup helper, 80 lookups, ms (before → after): | CPU | Body elements | Before | After | |---|---|---|---| | 1× | 800 | 3.5 | 2.6 | | 1× | 8,000 | 8.1 | 2.5 | | 1× | 32,000 | 23.3 | 2.5 | | 4× | 800 | 14.4 | 10.8 | | 4× | 8,000 | 33.9 | 10.5 | | 4× | 32,000 | 108.1 | 10.9 | ### How? - **Runtime** (`runtime-backend-dom.ts`): `findChunkScripts` / `findChunkLinks` iterate `document.head.getElementsByTagName(...)`. Matching is unchanged (exact URL, `?query` suffix, decoded variant, `rel` filter). This applies to chunk loads, CSS reloads and dev HMR unloads. Tags in `<body>` are deliberately ignored: - a chunk whose tag is only in `<body>` gets a new tag in `<head>`; - HMR unload leaves `<body>` copies, such as streamed `rel=preload` links, in place. - **Dev server** (`html.rs`): the HTML is rendered by a small pure function. Stylesheets come first, then the chunk scripts as `<script async>`, all in `<head>`; the page body stays in `<body>`. The runtime doesn't depend on chunk execution order. Entry code may now run before `<body>` is parsed, so it has to wait for the DOM itself if it touches it. - **Tests**: - `dynamic-chunk-dedupe` gains: - a streamed `next/dynamic` route that asserts each chunk is requested once and no tags are duplicated; - a Turbopack-only on-demand case, where inert copies of the chunk's tags in `<body>` must be ignored (it fails on the old lookup); - a Turbopack dev case where HMR unload removes only the `<head>` tags (it also fails on the old lookup). - The Rust unit tests cover the new dev-server HTML. - **Snapshots** for the files that inline the DOM runtime are regenerated. Verified locally: - `dynamic-chunk-dedupe` in Turbopack start, dev and cache components, plus webpack start, where the Turbopack-only cases are skipped; - `chunk-load-failure`, and the CSS and dynamic HMR suites; - `cargo test -p turbopack-dev-server` and the snapshot tests; - a `turbopack-cli dev` smoke test with a dynamic import and CSS. <!-- NEXT_JS_LLM --> <!-- fleet 00169b97-050c-43e3-a74e-5c9141d0996e --> 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
This diff is collapsed.
This source diff could not be displayed because it is too large.
You can
view the blob
instead.
This source diff could not be displayed because it is too large.
You can
view the blob
instead.
This source diff could not be displayed because it is too large.
You can
view the blob
instead.
This source diff could not be displayed because it is too large.
You can
view the blob
instead.
This source diff could not be displayed because it is too large.
You can
view the blob
instead.
This source diff could not be displayed because it is too large.
You can
view the blob
instead.
This source diff could not be displayed because it is too large.
You can
view the blob
instead.
Please
register
or
sign in
to comment