Turbopack: preload _app's next/dynamic chunks with _app's own availability (#100091)
## Summary Fixes a Turbopack dev regression from #98823 in the Pages Router. When `pages/_app` renders a `next/dynamic` component, a page's SSR HTML can preload a chunk that 404s. This was reported in #99789, with a repro at [dcheng1290/next-loadable-404](https://github.com/dcheng1290/next-loadable-404). The component still renders, but the browser logs a failed request. This PR also makes Turbopack production preload `_app`'s `next/dynamic` chunks on every page, matching dev and webpack. **Why:** #98823 chains the `/_app` and page client module graphs in development. `next/dynamic` imports are collected from every layer of that graph, so each page's `react-loadable-manifest` now also lists `/_app`'s dynamic imports, but chunked with the **page's** availability. At runtime those imports are loaded by `/_app`'s own async loader, which is chunked with `/_app`'s graph and availability. The preloaded chunk names therefore don't match the chunks that are emitted and requested. In production, `/_app`'s dynamic imports were only listed in `server/pages/_app/react-loadable-manifest.json`, which page renders don't read, so they were never preloaded. **How:** a Pages-only helper in `crates/next-api/src/pages.rs` (`page_next_dynamic_chunks`), used for every page except `/_app`: - it chunks `/_app`'s dynamic imports with the `/_app` endpoint's client module graph and client availability, which is exactly what `/_app` itself lists; - it chunks the page's own imports as before, with `/_app`'s filtered out (in per-page/dev mode they are reachable from the page's chained graph); - it merges both into the page's manifest. The generic collector in `module_graph.rs` is untouched, and so is App Router. This should be backported to `releases/lts/active` together with #98823 (backport PR #100008). ## Verification - New e2e suite `test/e2e/pages-app-next-dynamic-preload`, based on the repro. It checks that: - every `/_next/static` asset in the SSR HTML of `/` and `/other` returns 200; - the `_app` dynamic component renders; - both pages preload the same non-empty set of `_app` dynamic chunks. - The suite fails without the fix in Turbopack dev (a 404 asset). With the fix it passes in Turbopack dev and start and in webpack dev and start, using a locally built native binary. - Existing suites pass in Turbopack dev and start: `next-dynamic`, `next-dynamic-css`, `next-dynamic-lazy-compilation`, `pages-app-availability-boundary`, `app-document-import-order` (#99910), `app-dir/dynamic` and `app-dir/dynamic-import`. - Manual check, using the repro with a headless browser in dev and in production: - there are no failed requests or console errors on either page; - production manifests list `_app`'s entry with the chunk `_app` itself uses. - `cargo clippy -p next-api` and `cargo fmt --check` pass. <!-- NEXT_JS_LLM --> <!-- fleet 652922cb-dc34-493d-9be5-c241a5164cad --> 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