Unverified Commit 795e69ca authored by Niklas Mischkulnig's avatar Niklas Mischkulnig Committed by GitHub

explicit entry vs segment config

parent fa15e6f1
......@@ -1246,7 +1246,7 @@ impl AppEndpoint {
// assets to add to the middleware manifest (to be loaded in the edge runtime).
let mut middleware_assets = fxindexset![];
let runtime = app_entry.config.await?.runtime.unwrap_or_default();
let runtime = app_entry.config.await?.entry.runtime.unwrap_or_default();
let rsc_entry = app_entry.rsc_entry;
......@@ -1758,6 +1758,7 @@ impl AppEndpoint {
regions: app_entry
.config
.await?
.entry
.preferred_region
.as_ref()
.map(|region| Regions::Multiple(region.to_vec())),
......@@ -1908,7 +1909,7 @@ impl AppEndpoint {
let this = self.await?;
let project = this.app_project.project();
let app_entry = self.app_endpoint_entry().await?;
let runtime = app_entry.config.await?.runtime.unwrap_or_default();
let runtime = app_entry.config.await?.entry.runtime.unwrap_or_default();
let chunking_context = project.runtime_chunking_context(process_client_assets, runtime);
......@@ -2481,7 +2482,7 @@ impl Endpoint for AppEndpoint {
let this = self.await?;
let app_entry = self.app_endpoint_entry().await?;
let rsc_entry = app_entry.rsc_entry;
let runtime = app_entry.config.await?.runtime.unwrap_or_default();
let runtime = app_entry.config.await?.entry.runtime.unwrap_or_default();
let actions = ServerActionsGraphs::new(
graph,
......
......@@ -232,13 +232,17 @@ impl MiddlewareEndpoint {
let all_assets =
get_asset_paths_from_root(&node_root_value, edge_all_assets.await?).await?;
let regions = config.preferred_region.as_ref().map(|region| match region {
NextSegmentRegion::Single(region) => Regions::Single(region.clone()),
NextSegmentRegion::Multiple(regions) if regions.len() == 1 => {
Regions::Single(regions[0].clone())
}
NextSegmentRegion::Multiple(regions) => Regions::Multiple(regions.clone()),
});
let regions = config
.entry
.preferred_region
.as_ref()
.map(|region| match region {
NextSegmentRegion::Single(region) => Regions::Single(region.clone()),
NextSegmentRegion::Multiple(regions) if regions.len() == 1 => {
Regions::Single(regions[0].clone())
}
NextSegmentRegion::Multiple(regions) => Regions::Multiple(regions.clone()),
});
let edge_function_definition = EdgeFunctionDefinition {
files: file_paths_from_root,
......
......@@ -1035,7 +1035,7 @@ impl PageEndpoint {
let config =
parse_segment_config_from_source(self.source(), ParseSegmentMode::Base).await?;
let runtime = config.runtime.unwrap_or(NextRuntime::NodeJs);
let runtime = config.entry.runtime.unwrap_or(NextRuntime::NodeJs);
Ok(
// `/_app` and `/_document` never get rendered directly so they don't need to be
......@@ -1053,6 +1053,7 @@ impl PageEndpoint {
// they're included in the page bundle.
runtime: NextRuntime::NodeJs,
regions: config
.entry
.preferred_region
.as_ref()
.map(NextSegmentRegion::to_vec),
......@@ -1081,6 +1082,7 @@ impl PageEndpoint {
document_module: modules.document_module,
runtime,
regions: config
.entry
.preferred_region
.as_ref()
.map(NextSegmentRegion::to_vec),
......
......@@ -2386,7 +2386,7 @@ impl Project {
ParseSegmentMode::Base
},
);
let runtime = config.await?.runtime.unwrap_or(if is_proxy {
let runtime = config.await?.entry.runtime.unwrap_or(if is_proxy {
NextRuntime::NodeJs
} else {
NextRuntime::Edge
......
......@@ -38,7 +38,7 @@ pub async fn get_app_page_entry(
next_config: Vc<NextConfig>,
) -> Result<Vc<AppEntry>> {
let config = parse_segment_config_from_loader_tree(loader_tree, page.clone());
let is_edge = matches!(config.await?.runtime, Some(NextRuntime::Edge));
let is_edge = matches!(config.await?.entry.runtime, Some(NextRuntime::Edge));
let module_asset_context = if is_edge {
edge_context
} else {
......
......@@ -38,15 +38,16 @@ pub async fn get_app_route_entry(
next_config: Vc<NextConfig>,
) -> Result<Vc<AppEntry>> {
let segment_from_source = parse_segment_config_from_source(source, ParseSegmentMode::App);
// TODO this merging seems wrong/unnnecessary
let config = if let Some(original_segment_config) = original_segment_config {
let mut segment_config = segment_from_source.owned().await?;
segment_config.apply_parent_config(&*original_segment_config.await?);
segment_config.apply_parent_config(&*original_segment_config.await?, true);
segment_config.cell()
} else {
segment_from_source
};
let is_edge = matches!(config.await?.runtime, Some(NextRuntime::Edge));
let is_edge = matches!(config.await?.entry.runtime, Some(NextRuntime::Edge));
let module_asset_context = if is_edge {
edge_context
} else {
......
......@@ -100,17 +100,26 @@ impl NextSegmentRegion {
}
}
/// Runtime and deployment configuration for an entry. For App Router pages, only the entry page
/// and its ancestor layouts contribute these fields.
#[derive(Debug, Default, Clone, PartialEq, Eq, NonLocalValue, Encode, Decode)]
pub struct NextEntryConfig {
pub runtime: Option<NextRuntime>,
pub preferred_region: Option<NextSegmentRegion>,
pub max_duration: Option<u32>,
}
// Route segment configuration are inherited across the entire loader tree, including parallel
// routes.
#[turbo_tasks::value(shared)]
#[derive(Debug, Default, Clone)]
pub struct NextSegmentConfig {
pub entry: NextEntryConfig,
pub rsc: Option<NextSegmentRsc>,
pub dynamic: Option<NextSegmentDynamic>,
pub dynamic_params: Option<bool>,
pub revalidate: Option<NextRevalidate>,
pub max_duration: Option<u32>,
pub fetch_cache: Option<NextSegmentFetchCache>,
pub runtime: Option<NextRuntime>,
pub preferred_region: Option<NextSegmentRegion>,
pub middleware_matcher: Option<Vec<MiddlewareMatcherKind>>,
pub unstable_allow_dynamic: Option<Vec<RcStr>>,
......@@ -146,20 +155,71 @@ impl ValueDefault for NextSegmentConfig {
}
}
fn merge_parallel<T: PartialEq + Clone>(
a: &mut Option<T>,
b: &Option<T>,
name: &str,
) -> Result<()> {
match (a.as_ref(), b) {
(Some(a), Some(b)) if *a != *b => {
bail!(
"Sibling segment configs have conflicting values for {}",
name
)
}
(None, Some(b)) => {
*a = Some(b.clone());
}
_ => {}
}
Ok(())
}
impl NextEntryConfig {
fn apply_parent_config(&mut self, parent: &Self) {
let Self {
runtime,
preferred_region,
max_duration,
} = parent;
self.runtime = self.runtime.or(*runtime);
self.preferred_region = self.preferred_region.take().or(preferred_region.clone());
self.max_duration = self.max_duration.or(*max_duration);
}
fn apply_parallel_config(&mut self, parallel_config: &Self) -> Result<()> {
let Self {
runtime,
preferred_region,
max_duration,
} = parallel_config;
merge_parallel(&mut self.runtime, runtime, "runtime")?;
merge_parallel(&mut self.max_duration, max_duration, "maxDuration")?;
match (self.preferred_region.as_ref(), &preferred_region) {
(Some(a), Some(b)) if a.to_vec() != b.to_vec() => {
bail!("Sibling segment configs have conflicting values for preferredRegion")
}
(None, Some(b)) => {
self.preferred_region = Some(b.clone());
}
_ => {}
}
Ok(())
}
}
impl NextSegmentConfig {
/// Applies the parent config to this config, setting any unset values to
/// the parent's values.
pub fn apply_parent_config(&mut self, parent: &Self) {
/// the parent's values. Entry config is inherited only when `is_ancestor` is true.
pub fn apply_parent_config(&mut self, parent: &Self, is_ancestor: bool) {
let NextSegmentConfig {
entry,
// A layout's RSC classification does not apply to its child page.
rsc: _,
dynamic,
dynamic_params,
revalidate,
max_duration,
fetch_cache,
runtime,
preferred_region,
// TODO what about these?
instant: _,
prefetch: _,
......@@ -171,46 +231,25 @@ impl NextSegmentConfig {
generate_sitemaps: _,
generate_static_params: _,
} = self;
if is_ancestor {
entry.apply_parent_config(&parent.entry);
}
*dynamic = dynamic.or(parent.dynamic);
*dynamic_params = dynamic_params.or(parent.dynamic_params);
*revalidate = revalidate.or(parent.revalidate);
*max_duration = max_duration.or(parent.max_duration);
*fetch_cache = fetch_cache.or(parent.fetch_cache);
*runtime = runtime.or(parent.runtime);
*preferred_region = preferred_region.take().or(parent.preferred_region.clone());
}
/// Applies a config from a parallel route to this config, returning an
/// error if there are conflicting values.
pub fn apply_parallel_config(&mut self, parallel_config: &Self) -> Result<()> {
fn merge_parallel<T: PartialEq + Clone>(
a: &mut Option<T>,
b: &Option<T>,
name: &str,
) -> Result<()> {
match (a.as_ref(), b) {
(Some(a), Some(b)) if *a != *b => {
bail!(
"Sibling segment configs have conflicting values for {}",
name
)
}
(None, Some(b)) => {
*a = Some(b.clone());
}
_ => {}
}
Ok(())
}
let Self {
entry,
rsc,
dynamic,
dynamic_params,
revalidate,
max_duration,
fetch_cache,
runtime,
preferred_region,
// TODO what about these?
instant: _,
prefetch: _,
......@@ -222,6 +261,7 @@ impl NextSegmentConfig {
generate_sitemaps: _,
generate_static_params: _,
} = self;
entry.apply_parallel_config(&parallel_config.entry)?;
// Carry the leaf page's RSC classification up through its loader-tree branch. Unlike route
// segment config, classifications in different parallel branches are not conflicts.
*rsc = (*rsc).or(parallel_config.rsc);
......@@ -232,18 +272,7 @@ impl NextSegmentConfig {
"dynamicParams",
)?;
merge_parallel(revalidate, &parallel_config.revalidate, "revalidate")?;
merge_parallel(max_duration, &parallel_config.max_duration, "maxDuration")?;
merge_parallel(fetch_cache, &parallel_config.fetch_cache, "fetchCache")?;
merge_parallel(runtime, &parallel_config.runtime, "runtime")?;
match (preferred_region.as_ref(), &parallel_config.preferred_region) {
(Some(a), Some(b)) if a.to_vec() != b.to_vec() => {
bail!("Sibling segment configs have conflicting values for preferredRegion")
}
(None, Some(b)) => {
*preferred_region = Some(b.clone());
}
_ => {}
}
Ok(())
}
......@@ -622,8 +651,8 @@ pub async fn parse_segment_config_from_source(
}
// Individual exports take precedence over legacy config fields, regardless of
// export order. App Router ignores legacy config, so its fallback stays empty.
config.runtime = config.runtime.or(legacy_config.runtime);
config.max_duration = config.max_duration.or(legacy_config.max_duration);
config.entry.runtime = config.entry.runtime.or(legacy_config.runtime);
config.entry.max_duration = config.entry.max_duration.or(legacy_config.max_duration);
anyhow::Ok(config)
},
|f, ctx| GLOBALS.set(globals, || f.poll(ctx)),
......@@ -890,7 +919,7 @@ async fn parse_config_value(
.await?;
}
"regions" => {
config.preferred_region = parse_preferred_region_from_js_value(
config.entry.preferred_region = parse_preferred_region_from_js_value(
source, span, "config", "regions", value,
)
.await?;
......@@ -1032,7 +1061,7 @@ async fn parse_config_value(
.await;
};
config.max_duration = match parse_max_duration(&value) {
config.entry.max_duration = match parse_max_duration(&value) {
Ok(duration) => duration,
Err(error) => {
return invalid_config(
......@@ -1116,7 +1145,7 @@ async fn parse_config_value(
.await;
};
config.runtime = match serde_json::from_value(Value::String(val.to_string())) {
config.entry.runtime = match serde_json::from_value(Value::String(val.to_string())) {
Ok(runtime) => Some(runtime),
Err(err) => {
return invalid_config(
......@@ -1147,7 +1176,7 @@ async fn parse_config_value(
return Ok(());
}
config.preferred_region = parse_preferred_region_from_js_value(
config.entry.preferred_region = parse_preferred_region_from_js_value(
source,
span,
"preferredRegion",
......@@ -1558,30 +1587,42 @@ pub async fn parse_segment_config_from_loader_tree(
)
}
// AppPageLoaderTree looks like this:
//
// app/ modules.layout = root layout
// └─ dashboard/ modules.layout = dashboard layout
// ├─ children modules.page = dashboard/page.tsx
// └─ @sidebar/
// └─ children modules.default = @sidebar/default.tsx
async fn parse_segment_config_from_loader_tree_internal(
loader_tree: &AppPageLoaderTree,
page: &AppPage,
) -> Result<NextSegmentConfig> {
let modules = &loader_tree.modules;
// Only layouts on the entry page's directory ancestry contribute runtime/deployment config,
// layouts in sibling parallel routes are excluded.
let is_ancestor = page.0.starts_with(&loader_tree.page.0);
let mut config = if let Some(module) = &modules.page {
let source = Vc::upcast(FileSource::new(module.clone()));
parse_segment_config_from_source(source, ParseSegmentMode::App)
let mut config = if let Some(leaf) = modules.page.as_ref().or(modules.default.as_ref()) {
let source = Vc::upcast(FileSource::new(leaf.clone()));
let mut config = parse_segment_config_from_source(source, ParseSegmentMode::App)
.owned()
.await?
.await?;
// A __PAGE__ leaf stores its directory's AppPage. Compare it with the entry's directory,
// excluding the final `page` segment; sibling pages and default leaves do not qualify.
let is_entry_page = modules.page.is_some()
&& page
.0
.split_last()
.is_some_and(|(_, entry_directory)| loader_tree.page.0 == entry_directory);
if !is_entry_page {
config.entry = NextEntryConfig::default();
}
config
} else {
NextSegmentConfig::default()
};
let is_ancestor = page.0.starts_with(&loader_tree.page.0);
let is_entry_page = is_ancestor && loader_tree.page.0.len() + 1 == page.0.len();
if !is_entry_page {
// Sibling pages cannot choose the entry's runtime or deployment configuration.
config.runtime = None;
config.preferred_region = None;
config.max_duration = None;
}
let parallel_configs = loader_tree
.parallel_routes
.values()
......@@ -1598,24 +1639,10 @@ async fn parse_segment_config_from_loader_tree_internal(
config.apply_parallel_config(&tree?)?;
}
for (path, inherit_static_info) in [
(modules.default.as_ref(), false),
(modules.layout.as_ref(), is_ancestor),
] {
let Some(path) = path else {
continue;
};
if let Some(path) = &modules.layout {
let source = Vc::upcast(FileSource::new(path.clone()));
let mut parent_config = parse_segment_config_from_source(source, ParseSegmentMode::App)
.owned()
.await?;
if !inherit_static_info {
// Match getStaticInfoIncludingLayouts: only ancestor layouts contribute these fields.
parent_config.runtime = None;
parent_config.preferred_region = None;
parent_config.max_duration = None;
}
config.apply_parent_config(&parent_config);
let parent_config = parse_segment_config_from_source(source, ParseSegmentMode::App).await?;
config.apply_parent_config(&parent_config, is_ancestor);
}
Ok(config)
......
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