Skip to content

Commit 3676d72

Browse files
authored
Turbopack: add experimental.turbopackClient/ServerSideNestedAsyncChunking (#85827)
### What? Allow to configure nested vs flat async chunking with a new config option. ``` experimental: { turbopackClientSideNestedAsyncChunking: false, turbopackServerSideNestedAsyncChunking: true, } ```
1 parent 66b6a80 commit 3676d72

File tree

7 files changed

+66
-6
lines changed

7 files changed

+66
-6
lines changed

crates/next-api/src/project.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1119,6 +1119,9 @@ impl Project {
11191119
source_maps: self.next_config().client_source_maps(self.next_mode()),
11201120
no_mangling: self.no_mangling(),
11211121
scope_hoisting: self.next_config().turbo_scope_hoisting(self.next_mode()),
1122+
nested_async_chunking: self
1123+
.next_config()
1124+
.turbo_nested_async_chunking(self.next_mode(), true),
11221125
debug_ids: self.next_config().turbopack_debug_ids(),
11231126
should_use_absolute_url_references: self.next_config().inline_css(),
11241127
}))
@@ -1141,6 +1144,9 @@ impl Project {
11411144
turbo_source_maps: self.next_config().server_source_maps(),
11421145
no_mangling: self.no_mangling(),
11431146
scope_hoisting: self.next_config().turbo_scope_hoisting(self.next_mode()),
1147+
nested_async_chunking: self
1148+
.next_config()
1149+
.turbo_nested_async_chunking(self.next_mode(), false),
11441150
debug_ids: self.next_config().turbopack_debug_ids(),
11451151
client_root: self.client_relative_path().owned().await?,
11461152
asset_prefix: self.next_config().computed_asset_prefix().owned().await?,
@@ -1169,6 +1175,9 @@ impl Project {
11691175
turbo_source_maps: self.next_config().server_source_maps(),
11701176
no_mangling: self.no_mangling(),
11711177
scope_hoisting: self.next_config().turbo_scope_hoisting(self.next_mode()),
1178+
nested_async_chunking: self
1179+
.next_config()
1180+
.turbo_nested_async_chunking(self.next_mode(), false),
11721181
client_root: self.client_relative_path().owned().await?,
11731182
asset_prefix: self.next_config().computed_asset_prefix().owned().await?,
11741183
};

crates/next-core/src/next_client/context.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -426,6 +426,7 @@ pub struct ClientChunkingContextOptions {
426426
pub source_maps: Vc<bool>,
427427
pub no_mangling: Vc<bool>,
428428
pub scope_hoisting: Vc<bool>,
429+
pub nested_async_chunking: Vc<bool>,
429430
pub debug_ids: Vc<bool>,
430431
pub should_use_absolute_url_references: Vc<bool>,
431432
}
@@ -448,6 +449,7 @@ pub async fn get_client_chunking_context(
448449
source_maps,
449450
no_mangling,
450451
scope_hoisting,
452+
nested_async_chunking,
451453
debug_ids,
452454
should_use_absolute_url_references,
453455
} = options;
@@ -484,7 +486,8 @@ pub async fn get_client_chunking_context(
484486
.export_usage(*export_usage.await?)
485487
.module_id_strategy(module_id_strategy.to_resolved().await?)
486488
.debug_ids(*debug_ids.await?)
487-
.should_use_absolute_url_references(*should_use_absolute_url_references.await?);
489+
.should_use_absolute_url_references(*should_use_absolute_url_references.await?)
490+
.nested_async_availability(*nested_async_chunking.await?);
488491

489492
if next_mode.is_development() {
490493
builder = builder
@@ -510,7 +513,6 @@ pub async fn get_client_chunking_context(
510513
},
511514
)
512515
.use_content_hashing(ContentHashing::Direct { length: 16 })
513-
.nested_async_availability(true)
514516
.module_merging(*scope_hoisting.await?);
515517
}
516518

crates/next-core/src/next_config.rs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -886,6 +886,8 @@ pub struct ExperimentalConfig {
886886
turbopack_source_maps: Option<bool>,
887887
turbopack_tree_shaking: Option<bool>,
888888
turbopack_scope_hoisting: Option<bool>,
889+
turbopack_client_side_nested_async_chunking: Option<bool>,
890+
turbopack_server_side_nested_async_chunking: Option<bool>,
889891
turbopack_import_type_bytes: Option<bool>,
890892
turbopack_use_system_tls_certs: Option<bool>,
891893
/// Disable automatic configuration of the sass loader.
@@ -1790,6 +1792,29 @@ impl NextConfig {
17901792
}))
17911793
}
17921794

1795+
#[turbo_tasks::function]
1796+
pub async fn turbo_nested_async_chunking(
1797+
&self,
1798+
mode: Vc<NextMode>,
1799+
client_side: bool,
1800+
) -> Result<Vc<bool>> {
1801+
let option = if client_side {
1802+
self.experimental
1803+
.turbopack_client_side_nested_async_chunking
1804+
} else {
1805+
self.experimental
1806+
.turbopack_server_side_nested_async_chunking
1807+
};
1808+
Ok(Vc::cell(if let Some(value) = option {
1809+
value
1810+
} else {
1811+
match *mode.await? {
1812+
NextMode::Development => false,
1813+
NextMode::Build => client_side,
1814+
}
1815+
}))
1816+
}
1817+
17931818
#[turbo_tasks::function]
17941819
pub async fn turbopack_import_type_bytes(&self) -> Vc<bool> {
17951820
Vc::cell(

crates/next-core/src/next_edge/context.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,7 @@ pub struct EdgeChunkingContextOptions {
208208
pub turbo_source_maps: Vc<bool>,
209209
pub no_mangling: Vc<bool>,
210210
pub scope_hoisting: Vc<bool>,
211+
pub nested_async_chunking: Vc<bool>,
211212
pub client_root: FileSystemPath,
212213
pub asset_prefix: RcStr,
213214
}
@@ -229,6 +230,7 @@ pub async fn get_edge_chunking_context_with_client_assets(
229230
turbo_source_maps,
230231
no_mangling,
231232
scope_hoisting,
233+
nested_async_chunking,
232234
client_root,
233235
asset_prefix,
234236
} = options;
@@ -259,7 +261,8 @@ pub async fn get_edge_chunking_context_with_client_assets(
259261
SourceMapsType::None
260262
})
261263
.module_id_strategy(module_id_strategy.to_resolved().await?)
262-
.export_usage(*export_usage.await?);
264+
.export_usage(*export_usage.await?)
265+
.nested_async_availability(*nested_async_chunking.await?);
263266

264267
if !next_mode.is_development() {
265268
builder = builder
@@ -300,6 +303,7 @@ pub async fn get_edge_chunking_context(
300303
turbo_source_maps,
301304
no_mangling,
302305
scope_hoisting,
306+
nested_async_chunking,
303307
client_root,
304308
asset_prefix,
305309
} = options;
@@ -336,7 +340,8 @@ pub async fn get_edge_chunking_context(
336340
SourceMapsType::None
337341
})
338342
.module_id_strategy(module_id_strategy.to_resolved().await?)
339-
.export_usage(*export_usage.await?);
343+
.export_usage(*export_usage.await?)
344+
.nested_async_availability(*nested_async_chunking.await?);
340345

341346
if !next_mode.is_development() {
342347
builder = builder

crates/next-core/src/next_server/context.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1000,6 +1000,7 @@ pub struct ServerChunkingContextOptions {
10001000
pub turbo_source_maps: Vc<bool>,
10011001
pub no_mangling: Vc<bool>,
10021002
pub scope_hoisting: Vc<bool>,
1003+
pub nested_async_chunking: Vc<bool>,
10031004
pub debug_ids: Vc<bool>,
10041005
pub client_root: FileSystemPath,
10051006
pub asset_prefix: RcStr,
@@ -1022,6 +1023,7 @@ pub async fn get_server_chunking_context_with_client_assets(
10221023
turbo_source_maps,
10231024
no_mangling,
10241025
scope_hoisting,
1026+
nested_async_chunking,
10251027
debug_ids,
10261028
client_root,
10271029
asset_prefix,
@@ -1058,7 +1060,8 @@ pub async fn get_server_chunking_context_with_client_assets(
10581060
.module_id_strategy(module_id_strategy.to_resolved().await?)
10591061
.export_usage(*export_usage.await?)
10601062
.file_tracing(next_mode.is_production())
1061-
.debug_ids(*debug_ids.await?);
1063+
.debug_ids(*debug_ids.await?)
1064+
.nested_async_availability(*nested_async_chunking.await?);
10621065

10631066
builder = builder.source_map_source_type(if next_mode.is_development() {
10641067
SourceMapSourceType::AbsoluteFileUri
@@ -1106,6 +1109,7 @@ pub async fn get_server_chunking_context(
11061109
turbo_source_maps,
11071110
no_mangling,
11081111
scope_hoisting,
1112+
nested_async_chunking,
11091113
debug_ids,
11101114
client_root,
11111115
asset_prefix,
@@ -1142,7 +1146,8 @@ pub async fn get_server_chunking_context(
11421146
.module_id_strategy(module_id_strategy.to_resolved().await?)
11431147
.export_usage(*export_usage.await?)
11441148
.file_tracing(next_mode.is_production())
1145-
.debug_ids(*debug_ids.await?);
1149+
.debug_ids(*debug_ids.await?)
1150+
.nested_async_availability(*nested_async_chunking.await?);
11461151

11471152
if next_mode.is_development() {
11481153
builder = builder.source_map_source_type(SourceMapSourceType::AbsoluteFileUri);

packages/next/src/server/config-schema.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -301,6 +301,8 @@ export const experimentalSchema = {
301301
turbopackTreeShaking: z.boolean().optional(),
302302
turbopackRemoveUnusedExports: z.boolean().optional(),
303303
turbopackScopeHoisting: z.boolean().optional(),
304+
turbopackClientSideNestedAsyncChunking: z.boolean().optional(),
305+
turbopackServerSideNestedAsyncChunking: z.boolean().optional(),
304306
turbopackImportTypeBytes: z.boolean().optional(),
305307
turbopackUseSystemTlsCerts: z.boolean().optional(),
306308
turbopackUseBuiltinBabel: z.boolean().optional(),

packages/next/src/server/config-shared.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -413,6 +413,18 @@ export interface ExperimentalConfig {
413413
*/
414414
turbopackScopeHoisting?: boolean
415415

416+
/**
417+
* Enable nested async chunking for client side assets. Defaults to true in build mode and false in dev mode.
418+
* This optimization computes all possible paths through dynamic imports in the applications to figure out the modules needed at dynamic imports for every path.
419+
*/
420+
turbopackClientSideNestedAsyncChunking?: boolean
421+
422+
/**
423+
* Enable nested async chunking for server side assets. Defaults to false in dev and build mode.
424+
* This optimization computes all possible paths through dynamic imports in the applications to figure out the modules needed at dynamic imports for every path.
425+
*/
426+
turbopackServerSideNestedAsyncChunking?: boolean
427+
416428
/**
417429
* Enable filesystem cache for the turbopack dev server.
418430
*/

0 commit comments

Comments
 (0)