Skip to content

Commit 46b5437

Browse files
committed
Aggregate PagesData routes into the module graph when enabled
Fix duplicate module idents by constructing PageData reference types with a query parameter
1 parent c27c49f commit 46b5437

File tree

10 files changed

+56
-26
lines changed

10 files changed

+56
-26
lines changed

crates/next-api/benches/hmr.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -349,7 +349,9 @@ impl HmrBenchmark {
349349
data_endpoint,
350350
} => {
351351
let _ = endpoint_write_to_disk(**html_endpoint).await?;
352-
let _ = endpoint_write_to_disk(**data_endpoint).await?;
352+
if let Some(data_endpoint) = data_endpoint {
353+
let _ = endpoint_write_to_disk(**data_endpoint).await?;
354+
}
353355
}
354356
next_api::route::Route::PageApi { endpoint } => {
355357
let _ = endpoint_write_to_disk(**endpoint).await?;

crates/next-api/src/operation.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ async fn pick_endpoint(
166166
}
167167
EndpointSelector::RoutePageData(name) => {
168168
if let Some(Route::Page { data_endpoint, .. }) = endpoints.routes.get(&name) {
169-
Some(*data_endpoint)
169+
*data_endpoint
170170
} else {
171171
None
172172
}

crates/next-api/src/pages.rs

Lines changed: 26 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -199,18 +199,23 @@ impl PagesProject {
199199
.to_resolved()
200200
.await?,
201201
),
202-
data_endpoint: ResolvedVc::upcast(
203-
PageEndpoint::new(
204-
PageEndpointType::Data,
205-
self,
206-
pathname,
207-
original_name,
208-
page,
209-
pages_structure,
210-
)
211-
.to_resolved()
212-
.await?,
213-
),
202+
// The data endpoint is only needed in development mode to support HMR
203+
data_endpoint: if self.project().next_mode().await?.is_development() {
204+
Some(ResolvedVc::upcast(
205+
PageEndpoint::new(
206+
PageEndpointType::Data,
207+
self,
208+
pathname,
209+
original_name,
210+
page,
211+
pages_structure,
212+
)
213+
.to_resolved()
214+
.await?,
215+
))
216+
} else {
217+
None
218+
},
214219
})
215220
})
216221
};
@@ -635,9 +640,9 @@ impl PageEndpoint {
635640
// When creating a data endpoint we also create an Html endpoint for the same source
636641
// So add a query parameter to differentiate between the two.
637642
if self.ty == PageEndpointType::Data {
638-
RcStr::from("?server-data")
643+
rcstr!("?server-data")
639644
} else {
640-
RcStr::default()
645+
rcstr!("")
641646
},
642647
)))
643648
}
@@ -703,8 +708,8 @@ impl PageEndpoint {
703708
let this = self.await?;
704709
let project = this.pages_project.project();
705710

706-
let should_trace = project.next_mode().await?.is_production();
707711
if *project.per_page_module_graph().await? {
712+
let should_trace = project.next_mode().await?.is_production();
708713
let ssr_chunk_module = self.internal_ssr_chunk_module().await?;
709714
// Implements layout segment optimization to compute a graph "chain" for document, app,
710715
// page
@@ -996,7 +1001,12 @@ impl PageEndpoint {
9961001
ssr_module_graph,
9971002
current_availability_info,
9981003
)
999-
.await?;
1004+
.await
1005+
.context(format!(
1006+
"failed to find chunk group for {} for {:?}",
1007+
layout.ident().to_string().await?,
1008+
this.ty,
1009+
))?;
10001010

10011011
current_chunks = current_chunks
10021012
.concatenate(*chunk_group.assets)

crates/next-api/src/project.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -863,10 +863,13 @@ impl Project {
863863
match route {
864864
Route::Page {
865865
html_endpoint,
866-
data_endpoint: _,
866+
data_endpoint,
867867
} => {
868868
if !app_dir_only {
869869
endpoints.push(*html_endpoint);
870+
if let Some(data_endpoint) = data_endpoint {
871+
endpoints.push(*data_endpoint);
872+
}
870873
}
871874
}
872875
Route::PageApi { endpoint } => {

crates/next-api/src/route.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ pub struct AppPageRoute {
3434
pub enum Route {
3535
Page {
3636
html_endpoint: ResolvedVc<Box<dyn Endpoint>>,
37-
data_endpoint: ResolvedVc<Box<dyn Endpoint>>,
37+
data_endpoint: Option<ResolvedVc<Box<dyn Endpoint>>>,
3838
},
3939
PageApi {
4040
endpoint: ResolvedVc<Box<dyn Endpoint>>,

crates/next-core/src/next_config.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1821,7 +1821,7 @@ impl NextConfig {
18211821
NextMode::Development => self
18221822
.experimental
18231823
.turbopack_use_whole_app_module_graph_in_dev
1824-
.unwrap_or(false),
1824+
.unwrap_or(true),
18251825
NextMode::Build => true,
18261826
}))
18271827
}

crates/next-core/src/next_pages/page_entry.rs

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -112,8 +112,10 @@ pub async fn create_page_ssr_entry_module(
112112

113113
let file = File::from(result.build());
114114

115-
source = Vc::upcast(VirtualSource::new(
116-
source.ident().path().owned().await?,
115+
source = Vc::upcast(VirtualSource::new_with_ident(
116+
// Reuse the ident of the original source since it may contain query parameters or
117+
// modifiers that are used to make the source unique.
118+
source.ident(),
117119
AssetContent::file(file.into()),
118120
));
119121
}
@@ -197,8 +199,17 @@ async fn process_global_item(
197199
reference_type: ReferenceType,
198200
module_context: Vc<Box<dyn AssetContext>>,
199201
) -> Result<Vc<Box<dyn Module>>> {
200-
let source = Vc::upcast(FileSource::new(item.file_path().owned().await?));
201-
Ok(module_context.process(source, reference_type).module())
202+
let source = Vc::upcast(FileSource::new_with_query(
203+
item.file_path().owned().await?,
204+
if reference_type == ReferenceType::Entry(EntryReferenceSubType::PageData) {
205+
rcstr!("?server-data")
206+
} else {
207+
rcstr!("")
208+
},
209+
));
210+
Ok(module_context
211+
.process(source, reference_type.clone())
212+
.module())
202213
}
203214

204215
#[turbo_tasks::function]

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -499,7 +499,8 @@ export interface ExperimentalConfig {
499499
turbopackUseBuiltinSass?: boolean
500500

501501
/**
502-
* Whether to use the whole app module graph in development mode. This makes development mode chunking more like production but may come with an HMR performance regression.
502+
* Whether to compute the whole app module graph in development mode. This may reduce memory usage
503+
* by enabling more sharing of chunking decisions across routes.
503504
*/
504505
turbopackUseWholeAppModuleGraphInDev?: boolean
505506

turbopack/crates/turbopack-core/src/chunk/chunking/dev.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,8 @@ pub async fn app_vendors_split<'l>(
8686
let mut chunk_group_specific_chunk_items = Vec::new();
8787
let mut app_chunk_items = Vec::new();
8888
let mut vendors_chunk_items = Vec::new();
89+
// TODO this needs to create new chunk groups for each async entry point to preserver `import()`
90+
// semantics, even if it leads to more chunks.
8991
for item in chunk_items {
9092
let ChunkItemOrBatchWithInfo::ChunkItem {
9193
chunk_item: ChunkItemWithAsyncModuleInfo { module, .. },

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,7 @@ pub struct ChunkingConfigs(FxHashMap<ResolvedVc<Box<dyn ChunkType>>, ChunkingCon
136136
pub trait ChunkingContext {
137137
#[turbo_tasks::function]
138138
fn name(self: Vc<Self>) -> Vc<RcStr>;
139+
/// Whether to use file URIs in source maps.
139140
#[turbo_tasks::function]
140141
fn should_use_file_source_map_uris(self: Vc<Self>) -> Vc<bool>;
141142
/// The root path of the project

0 commit comments

Comments
 (0)