Skip to content

Commit 2c0d2e4

Browse files
committed
[turbopack] Add inline_chunk_group_bootstrap to BrowserChunkingContext and chunk_group_bootstrap_params to ChunkGroupResult
1 parent da06b98 commit 2c0d2e4

4 files changed

Lines changed: 40 additions & 5 deletions

File tree

crates/next-core/src/next_app/app_client_references_chunks.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,7 @@ pub async fn get_app_client_references_chunks(
168168
referenced_assets: ResolvedVc::cell(vec![]),
169169
references: ResolvedVc::cell(vec![]),
170170
availability_info: client_availability_info,
171+
chunk_group_bootstrap_params: None,
171172
}
172173
.resolved_cell();
173174
let mut current_ssr_chunk_group = ChunkGroupResult::empty_resolved();

turbopack/crates/turbopack-browser/src/chunking_context.rs

Lines changed: 34 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,11 @@ impl BrowserChunkingContextBuilder {
154154
self
155155
}
156156

157+
pub fn inline_chunk_group_bootstrap(mut self, inline_chunk_group_bootstrap: bool) -> Self {
158+
self.chunking_context.inline_chunk_group_bootstrap = inline_chunk_group_bootstrap;
159+
self
160+
}
161+
157162
pub fn should_use_absolute_url_references(
158163
mut self,
159164
should_use_absolute_url_references: bool,
@@ -301,6 +306,9 @@ pub struct BrowserChunkingContext {
301306
enable_dynamic_chunk_content_loading: bool,
302307
/// Enable debug IDs for chunks and source maps.
303308
debug_ids: bool,
309+
/// Inline each entrypoint's chunk group bootstrap into the HTML (as the
310+
/// `ChunkGroupResult.chunk_group_bootstrap_params`).
311+
inline_chunk_group_bootstrap: bool,
304312
/// The environment chunks will be evaluated in.
305313
environment: ResolvedVc<Environment>,
306314
/// The kind of runtime to include in the output.
@@ -373,6 +381,7 @@ impl BrowserChunkingContext {
373381
enable_module_merging: false,
374382
enable_dynamic_chunk_content_loading: false,
375383
debug_ids: false,
384+
inline_chunk_group_bootstrap: false,
376385
environment,
377386
runtime_type,
378387
minify_type: MinifyType::NoMinify,
@@ -786,6 +795,7 @@ impl ChunkingContext for BrowserChunkingContext {
786795
referenced_assets: OutputAssets::empty_resolved(),
787796
references: ResolvedVc::cell(references),
788797
availability_info,
798+
chunk_group_bootstrap_params: None,
789799
}
790800
.cell())
791801
}
@@ -894,11 +904,29 @@ impl ChunkingContext for BrowserChunkingContext {
894904
);
895905
}
896906

897-
assets.push(ResolvedVc::upcast(
898-
self.generate_evaluate_chunk(ident, other_assets, entries)
899-
.to_resolved()
900-
.await?,
901-
));
907+
// The evaluate chunk registers this entry's chunks/modules onto the
908+
// `globalThis[TURBOPACK]` queue. When `inline_chunk_group_bootstrap` is enabled we
909+
// return that chunk group's bootstrap params for Next to inline into the HTML and
910+
// skip emitting the per-route evaluate chunk file.
911+
//
912+
// Only `ChunkGroup::Entry` groups (the page/app client entries Next renders into
913+
// HTML) can be inlined. Other groups — notably `Isolated` web workers, which
914+
// bootstrap via `importScripts` with no HTML to inline into — must keep their
915+
// evaluate chunk so the entry still registers.
916+
let evaluate_chunk = self.generate_evaluate_chunk(ident, other_assets, entries);
917+
let chunk_group_bootstrap_params = if this.inline_chunk_group_bootstrap
918+
&& matches!(chunk_group, ChunkGroup::Entry(_))
919+
{
920+
Some(
921+
evaluate_chunk
922+
.chunk_group_bootstrap_params()
923+
.owned()
924+
.await?,
925+
)
926+
} else {
927+
assets.push(ResolvedVc::upcast(evaluate_chunk.to_resolved().await?));
928+
None
929+
};
902930

903931
// The shared runtime chunk must be the LAST asset of the group. It drains
904932
// the registration queue set up by the chunks above, so it has to load
@@ -918,6 +946,7 @@ impl ChunkingContext for BrowserChunkingContext {
918946
referenced_assets: OutputAssets::empty_resolved(),
919947
references: ResolvedVc::cell(references),
920948
availability_info,
949+
chunk_group_bootstrap_params,
921950
}
922951
.cell())
923952
}

turbopack/crates/turbopack-core/src/chunk/chunking_context.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,7 @@ pub struct ChunkGroupResult {
129129
pub referenced_assets: ResolvedVc<OutputAssets>,
130130
pub references: ResolvedVc<OutputAssetsReferences>,
131131
pub availability_info: AvailabilityInfo,
132+
pub chunk_group_bootstrap_params: Option<RcStr>,
132133
}
133134

134135
impl ChunkGroupResult {
@@ -138,6 +139,7 @@ impl ChunkGroupResult {
138139
referenced_assets: ResolvedVc::cell(vec![]),
139140
references: ResolvedVc::cell(vec![]),
140141
availability_info: AvailabilityInfo::root(),
142+
chunk_group_bootstrap_params: None,
141143
}
142144
.cell()
143145
}
@@ -148,6 +150,7 @@ impl ChunkGroupResult {
148150
referenced_assets: ResolvedVc::cell(vec![]),
149151
references: ResolvedVc::cell(vec![]),
150152
availability_info: AvailabilityInfo::root(),
153+
chunk_group_bootstrap_params: None,
151154
}
152155
.resolved_cell()
153156
}
@@ -181,6 +184,7 @@ impl ChunkGroupResult {
181184
.to_resolved()
182185
.await?,
183186
availability_info: next.availability_info,
187+
chunk_group_bootstrap_params: next.chunk_group_bootstrap_params.clone(),
184188
}
185189
.cell())
186190
}

turbopack/crates/turbopack-nodejs/src/chunking_context.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -545,6 +545,7 @@ impl ChunkingContext for NodeJsChunkingContext {
545545
referenced_assets: OutputAssets::empty_resolved(),
546546
references: ResolvedVc::cell(references),
547547
availability_info,
548+
chunk_group_bootstrap_params: None,
548549
}
549550
.cell())
550551
}

0 commit comments

Comments
 (0)