Unverified Commit c0c6790f authored by Tobias Koppers's avatar Tobias Koppers Committed by GitHub

fix(next): register the chunk groups that are chunked (#99749)

### What?

Registers chunk groups in the module graph exactly as output code chunks
them, in two places:

- **Pages layout segments**: `_document` and `_app` are registered as
their own `Entry` groups (`layout_chunk_group_entry`), in both the
per-page (dev) graph chain and the whole-app `entries()`.
`internal_ssr_chunk` chunks them as `ChunkGroup::Entry(vec![layout])`
instead of `ChunkGroup::Shared(layout)`.
- **turbopack-cli dev**: `create_web_entry_source` registers one `Entry`
group per entry, with the same entries `DevHtmlAsset` chunks: the
runtime entries followed by the entry for evaluatable modules, or the
module alone otherwise. It passes `runtime_entries` without
`with_entry(entry)`, because `DevHtmlAsset` appends the entry itself.

### Why?

A chunk group is identified by its key, and for an `Entry` group that is
its ordered entries. The next PR in the stack looks every chunked group
up in `ChunkGroupInfo` by key, so a chunked group the graph didn't
register fails.

- Pages layouts were declared `Shared(layout)`. The whole-app graph also
registers the standalone `/_document` and `/_app` endpoints'
`Entry([module])` group for the same module and keeps only one root
group per module, so `Shared(layout)` wasn't registered there.
- turbopack-cli registered a single `Entry` group with all entries and
runtime entries, while `DevHtmlAsset` chunks one group per entry (with
the entry appended twice).

No output change is intended: the same modules are chunked, but the
registered groups now match them.

This is PR 3 of an eight-PR stack (on top of #99100, already merged):

1. #98733 — canonical chunk-group identity
2. #99686 — chunk several chunk groups in one `chunk_group` call
3. **This PR** — register the chunk groups that are chunked
4. #98734 — use graph-derived merged groups
5. #99750 — chunking takes a `ChunkGroupKey`
6. #98823 — Pages Router dev client graph chaining
7. #98685 — chunk-group availability bitmaps
8. #99050 — keyed chunk-group index lookups

### Verification

- Workspace `cargo clippy --all-targets -D warnings`, `cargo check
--workspace --all-targets`, `cargo fmt --check`
- Turbopack snapshots: 132 passed, no snapshot changes
- `turbo-tasks` + `turbopack-core` + `next-core` unit tests: 362 passed
- Turbopack production and dev `404-page-router` (custom `_document` /
`_app`): 25/25, 12/12

<!-- 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 26365e05
...@@ -747,7 +747,8 @@ impl PageEndpoint { ...@@ -747,7 +747,8 @@ impl PageEndpoint {
let ssr_chunk_module = self.internal_ssr_chunk_module().await?; let ssr_chunk_module = self.internal_ssr_chunk_module().await?;
// Implements layout segment optimization to compute a graph "chain" for document, app, // Implements layout segment optimization to compute a graph "chain" for document, app,
// page // page. Each layout is its own entry chunk group, as in the whole-app graph (see
// `entries`), so `internal_ssr_chunk` chunks it with the same key in every mode.
let mut graphs = vec![]; let mut graphs = vec![];
let mut visited_modules = VisitedModules::empty(); let mut visited_modules = VisitedModules::empty();
for module in [ for module in [
...@@ -758,7 +759,7 @@ impl PageEndpoint { ...@@ -758,7 +759,7 @@ impl PageEndpoint {
.flatten() .flatten()
{ {
let graph = SingleModuleGraph::new_with_entries_visited_intern( let graph = SingleModuleGraph::new_with_entries_visited_intern(
GraphEntries::from_chunk_groups(vec![ChunkGroupEntry::Shared(module)]), GraphEntries::from_chunk_groups(vec![layout_chunk_group_entry(module)]),
visited_modules, visited_modules,
should_trace, should_trace,
should_read_binding_usage, should_read_binding_usage,
...@@ -1026,9 +1027,10 @@ impl PageEndpoint { ...@@ -1026,9 +1027,10 @@ impl PageEndpoint {
name = display(layout.ident().to_string().await?) name = display(layout.ident().to_string().await?)
); );
async { async {
// Registered as an entry group by `layout_chunk_group_entry`.
let chunk_group = chunking_context.chunk_group( let chunk_group = chunking_context.chunk_group(
layout.ident(), layout.ident(),
smallvec![ChunkGroup::Shared(layout)], smallvec![ChunkGroup::Entry(vec![layout])],
ssr_module_graph, ssr_module_graph,
current_chunk_group.await?.availability_info, current_chunk_group.await?.availability_info,
); );
...@@ -1645,6 +1647,20 @@ pub struct InternalSsrChunkModule { ...@@ -1645,6 +1647,20 @@ pub struct InternalSsrChunkModule {
pub regions: Option<Vec<RcStr>>, pub regions: Option<Vec<RcStr>>,
} }
/// The chunk group a Pages layout segment (`_document` or `_app`) is registered as.
///
/// A layout is an entry group of its own, like the standalone `/_document` and `/_app`
/// endpoints register their module, so the graph never has to choose between a shared and an
/// entry group for the same module and `internal_ssr_chunk` can always chunk it as
/// `ChunkGroup::Entry(vec![layout])`. Default heuristics keep a page's clusters off the layout
/// groups shared between pages.
fn layout_chunk_group_entry(layout: ResolvedVc<Box<dyn Module>>) -> ChunkGroupEntry {
ChunkGroupEntry::Entry {
modules: vec![layout],
heuristics: EntryHeuristics::default(),
}
}
#[turbo_tasks::value_impl] #[turbo_tasks::value_impl]
impl Endpoint for PageEndpoint { impl Endpoint for PageEndpoint {
#[turbo_tasks::function] #[turbo_tasks::function]
...@@ -1837,7 +1853,7 @@ impl Endpoint for PageEndpoint { ...@@ -1837,7 +1853,7 @@ impl Endpoint for PageEndpoint {
let modules = shared_entries let modules = shared_entries
.into_iter() .into_iter()
.flatten() .flatten()
.map(ChunkGroupEntry::Shared) .map(layout_chunk_group_entry)
.chain(std::iter::once(ChunkGroupEntry::Entry { .chain(std::iter::once(ChunkGroupEntry::Entry {
modules: vec![ssr_chunk_module.ssr_module], modules: vec![ssr_chunk_module.ssr_module],
heuristics: heuristics.clone(), heuristics: heuristics.clone(),
......
...@@ -157,23 +157,39 @@ pub async fn create_web_entry_source( ...@@ -157,23 +157,39 @@ pub async fn create_web_entry_source(
.try_flat_join() .try_flat_join()
.await?; .await?;
let all_modules = entries // The chunk groups registered here have to be exactly the ones `DevHtmlAsset` chunks later:
// a chunk group is identified by its entries, in order, so a different order or a repeated
// module is a different chunk group that `ChunkGroupInfo` wouldn't know.
let runtime_entry_modules = runtime_entries
.await?
.iter() .iter()
.copied() .map(|&entry| ResolvedVc::upcast::<Box<dyn Module>>(entry))
.chain( .collect::<Vec<_>>();
runtime_entries let chunk_group_entries = entries
.await? .iter()
.iter() .map(|&module| {
.map(|&entry| ResolvedVc::upcast(entry)), let modules = if ResolvedVc::try_sidecast::<Box<dyn EvaluatableAsset>>(module).is_some()
) {
.collect::<Vec<ResolvedVc<Box<dyn Module>>>>(); // `DevHtmlAsset` appends the entry to the runtime entries and evaluates all of
// them.
runtime_entry_modules
.iter()
.copied()
.chain(std::iter::once(module))
.collect()
} else {
// A module that isn't evaluatable is chunked on its own, without the runtime.
vec![module]
};
ChunkGroupEntry::Entry {
modules,
heuristics: EntryHeuristics::default(),
}
})
.collect::<Vec<_>>();
let module_graph = ModuleGraph::from_graphs( let module_graph = ModuleGraph::from_graphs(
vec![SingleModuleGraph::new_with_entries( vec![SingleModuleGraph::new_with_entries(
GraphEntries::from_chunk_groups(vec![ChunkGroupEntry::Entry { GraphEntries::from_chunk_groups(chunk_group_entries).resolved_cell(),
modules: all_modules,
heuristics: EntryHeuristics::default(),
}])
.resolved_cell(),
false, false,
false, false,
)], )],
...@@ -184,7 +200,7 @@ pub async fn create_web_entry_source( ...@@ -184,7 +200,7 @@ pub async fn create_web_entry_source(
let entries: Vec<_> = entries let entries: Vec<_> = entries
.into_iter() .into_iter()
.map(async |module| { .map(async |module| {
if let (Some(chunkable_module), Some(entry)) = ( if let (Some(chunkable_module), Some(_)) = (
ResolvedVc::try_sidecast::<Box<dyn ChunkableModule>>(module), ResolvedVc::try_sidecast::<Box<dyn ChunkableModule>>(module),
ResolvedVc::try_sidecast::<Box<dyn EvaluatableAsset>>(module), ResolvedVc::try_sidecast::<Box<dyn EvaluatableAsset>>(module),
) { ) {
...@@ -192,7 +208,9 @@ pub async fn create_web_entry_source( ...@@ -192,7 +208,9 @@ pub async fn create_web_entry_source(
chunkable_module, chunkable_module,
module_graph, module_graph,
chunking_context, chunking_context,
runtime_entries: Some(runtime_entries.with_entry(*entry).to_resolved().await?), // Not `with_entry(module)`: `DevHtmlAsset` appends the entry itself, and
// appending it here too would chunk it twice.
runtime_entries: Some(runtime_entries.to_resolved().await?),
}) })
} else if let Some(chunkable_module) = } else if let Some(chunkable_module) =
ResolvedVc::try_sidecast::<Box<dyn ChunkableModule>>(module) ResolvedVc::try_sidecast::<Box<dyn ChunkableModule>>(module)
......
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