Unverified Commit 6dd4b27b authored by Tobias Koppers's avatar Tobias Koppers Committed by GitHub

Turbopack: chain the Pages Router development client module graph (#98823)

### What?

Chains the Pages Router **development** client module graph so that
`/_app` and the page being chunked live in the same `ModuleGraph`.

This is PR 1 of a six-PR stack, based on `canary` (#98733, #99686 and
#99749 are already merged):

1. **This PR** — Pages Router dev client graph chaining
2. #99910 — regression test for Pages Router `_app` availability
(#99789)
3. #98734 — use graph-derived merged groups
4. #99750 — chunking takes a `ChunkGroupKey`
5. #98685 — chunk-group availability bitmaps
6. #99050 — keyed chunk-group index lookups

### Why?

In development `Project::per_page_module_graph()` is true, so every page
endpoint builds its own client `ModuleGraph`, each with its own
`ChunkGroupInfo` and its own index space starting at `0`.

`/_app` is always loaded before the page in the browser, so the page
reuses `/_app`'s availability when chunking. Today that availability is
a set of concrete modules, which is graph-independent, so the split
graphs go unnoticed.

That stops being true as soon as availability is expressed as
chunk-group indices (#98685): `/_app` would record index `0` in *its*
graph, and the page would resolve that same `0` against *its own* graph
— where it means the page's own entry group. Every module of the group
being chunked then looks already-available and is dropped, producing
runtime errors like:

```
Module [hmr-entry]/hmr-entry.js { ENTRY => "[project]/pages/_error" }
  was instantiated as a runtime entry of chunk …,
  but the module factory is not available.
```

### How?

- `PageEndpoint::client_module_graph` builds a graph *chain* in per-page
mode: `/_app`'s entry group first, then the page's, with
`VisitedModules` threaded between them — the same layout-segment
approach `ssr_module_graph` already uses for document → app → page.
- Whole-app mode (production) is unchanged and still goes through
`module_graph_for_modules`.
- `/_app` itself is not chained onto anything.
- Extracts a small `client_entry_modules` helper, since the entry module
list is now needed in two places.

Availability is still carried as the set of modules `/_app` emitted.
Making the two endpoints share one graph also fixes #99789: with
separate graphs, batching in the page graph could produce a node for a
module shared with `/_app` whose edges point at page-only modules, and
those modules were skipped as already available ("module factory is not
available" on hydration). #99910 adds the regression test. Lining up the
graph identities is also what #98685 builds on.

### Verification

- `turbopack-core` — 160 passed
- `next-core` — 84 passed
- Turbopack snapshots — 125 passed
- `404-page-router` and `server-navigation-error` development suites
- Affected Cargo checks and Rust formatting

<!-- NEXT_JS_LLM -->


<!-- fleet 4f67c6f7-bbe2-454b-9a17-822a60ef8f7e -->
Co-authored-by: 's avatarvercel-fleet-prod[bot] <318278635+vercel-fleet-prod[bot]@users.noreply.github.com>
Co-authored-by: 's avatarTobias Koppers <1365881+sokra@users.noreply.github.com>
parent 7d525155
...@@ -642,6 +642,17 @@ enum EmitManifests { ...@@ -642,6 +642,17 @@ enum EmitManifests {
Full, Full,
} }
/// The chunk group entry modules of a set of client evaluatable assets.
async fn client_entry_modules(
evaluatable_assets: Vc<EvaluatableAssets>,
) -> Result<Vec<ResolvedVc<Box<dyn Module>>>> {
Ok(evaluatable_assets
.await?
.iter()
.map(|asset| ResolvedVc::upcast(*asset))
.collect())
}
#[turbo_tasks::value_impl] #[turbo_tasks::value_impl]
impl PageEndpoint { impl PageEndpoint {
#[turbo_tasks::function] #[turbo_tasks::function]
...@@ -730,8 +741,54 @@ impl PageEndpoint { ...@@ -730,8 +741,54 @@ impl PageEndpoint {
async fn client_module_graph(self: Vc<Self>) -> Result<Vc<ModuleGraph>> { async fn client_module_graph(self: Vc<Self>) -> Result<Vc<ModuleGraph>> {
let this = self.await?; let this = self.await?;
let project = this.pages_project.project(); let project = this.pages_project.project();
let evaluatable_assets = self.client_evaluatable_assets();
Ok(project.module_graph_for_modules(evaluatable_assets)) if !*project.per_page_module_graph().await? {
return Ok(project.module_graph_for_modules(self.client_evaluatable_assets()));
}
// With a per-page module graph every endpoint would otherwise get its own graph, and
// chunk group indices are only meaningful within the `ChunkGroupInfo` of a single graph.
// `/_app` is always loaded before the page in the browser and seeds the page's
// availability, so both entry groups have to live in the same graph. Build a graph
// "chain" for app, page -- the same layout segment optimization `ssr_module_graph` uses
// for document, app, page.
let should_trace = *project.should_write_nft_manifests().await?;
let should_read_binding_usage = project.next_mode().await?.is_production();
let mut graphs = vec![];
let mut visited_modules = VisitedModules::empty();
if this.pathname != "/_app" {
let graph = SingleModuleGraph::new_with_entries_visited_intern(
GraphEntries::from_chunk_groups(vec![ChunkGroupEntry::Entry {
modules: client_entry_modules(
this.pages_project
.app_page_endpoint()
.client_evaluatable_assets(),
)
.await?,
heuristics: EntryHeuristics::default(),
}]),
visited_modules,
should_trace,
should_read_binding_usage,
);
graphs.push(graph);
visited_modules = VisitedModules::concatenate(visited_modules, graph);
}
let graph = SingleModuleGraph::new_with_entries_visited_intern(
GraphEntries::from_chunk_groups(vec![ChunkGroupEntry::Entry {
modules: client_entry_modules(self.client_evaluatable_assets()).await?,
heuristics: EntryHeuristics::default(),
}]),
visited_modules,
should_trace,
should_read_binding_usage,
);
graphs.push(graph);
Ok(ModuleGraph::from_graphs(graphs, None).connect())
} }
#[turbo_tasks::function] #[turbo_tasks::function]
...@@ -808,12 +865,7 @@ impl PageEndpoint { ...@@ -808,12 +865,7 @@ impl PageEndpoint {
let module_graph = self.client_module_graph(); let module_graph = self.client_module_graph();
let evaluatable_assets = self let evaluatable_assets = client_entry_modules(self.client_evaluatable_assets()).await?;
.client_evaluatable_assets()
.await?
.iter()
.map(|m| ResolvedVc::upcast(*m))
.collect();
// Like App Router layouts, `/_app` is always loaded before the page. Chunk it first so // Like App Router layouts, `/_app` is always loaded before the page. Chunk it first so
// the page's chunks don't include modules that the browser already downloaded with // the page's chunks don't include modules that the browser already downloaded with
// `/_app`. // `/_app`.
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment