Skip to content

Commit 329b373

Browse files
committed
enum ParseSegmentMode
1 parent 90c59c0 commit 329b373

File tree

7 files changed

+43
-25
lines changed

7 files changed

+43
-25
lines changed

crates/next-api/src/app.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ use next_core::{
3333
},
3434
next_server_utility::{NEXT_SERVER_UTILITY_MERGE_TAG, NextServerUtilityTransition},
3535
parse_segment_config_from_source,
36-
segment_config::NextSegmentConfig,
36+
segment_config::{NextSegmentConfig, ParseSegmentMode},
3737
util::{NextRuntime, app_function_name, module_styles_rule_condition, styles_rule_condition},
3838
};
3939
use serde::{Deserialize, Serialize};
@@ -1106,7 +1106,7 @@ impl AppEndpoint {
11061106

11071107
for layout in root_layouts.iter().rev() {
11081108
let source = Vc::upcast(FileSource::new(layout.clone()));
1109-
let layout_config = parse_segment_config_from_source(source, true);
1109+
let layout_config = parse_segment_config_from_source(source, ParseSegmentMode::App);
11101110
config.apply_parent_config(&*layout_config.await?);
11111111
}
11121112

crates/next-api/src/middleware.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ use next_core::{
88
next_manifests::{EdgeFunctionDefinition, MiddlewareMatcher, MiddlewaresManifestV2, Regions},
99
next_server::{ServerContextType, get_server_runtime_entries},
1010
parse_segment_config_from_source,
11+
segment_config::ParseSegmentMode,
1112
util::{MiddlewareMatcherKind, NextRuntime},
1213
};
1314
use tracing::Instrument;
@@ -87,7 +88,7 @@ impl MiddlewareEndpoint {
8788
userland_module,
8889
);
8990

90-
let runtime = parse_segment_config_from_source(*self.source, false)
91+
let runtime = parse_segment_config_from_source(*self.source, ParseSegmentMode::Base)
9192
.await?
9293
.runtime
9394
.unwrap_or(NextRuntime::Edge);
@@ -178,7 +179,8 @@ impl MiddlewareEndpoint {
178179
async fn output_assets(self: Vc<Self>) -> Result<Vc<OutputAssets>> {
179180
let this = self.await?;
180181

181-
let config = parse_segment_config_from_source(*self.await?.source, false).await?;
182+
let config =
183+
parse_segment_config_from_source(*self.await?.source, ParseSegmentMode::Base).await?;
182184
let runtime = config.runtime.unwrap_or(NextRuntime::Edge);
183185

184186
let next_config = this.project.next_config().await?;

crates/next-api/src/pages.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ use next_core::{
2424
PagesDirectoryStructure, PagesStructure, PagesStructureItem, find_pages_structure,
2525
},
2626
parse_segment_config_from_source,
27+
segment_config::ParseSegmentMode,
2728
util::{NextRuntime, get_asset_prefix_from_pathname, pages_function_name},
2829
};
2930
use serde::{Deserialize, Serialize};
@@ -928,7 +929,8 @@ impl PageEndpoint {
928929
.process(self.source(), reference_type.clone())
929930
.module();
930931

931-
let config = parse_segment_config_from_source(self.source(), false).await?;
932+
let config =
933+
parse_segment_config_from_source(self.source(), ParseSegmentMode::Base).await?;
932934

933935
let runtime = config.runtime.unwrap_or(NextRuntime::NodeJs);
934936

crates/next-api/src/project.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ use next_core::{
2222
},
2323
next_telemetry::NextFeatureTelemetry,
2424
parse_segment_config_from_source,
25+
segment_config::ParseSegmentMode,
2526
util::{NextRuntime, OptionEnvMap},
2627
};
2728
use serde::{Deserialize, Serialize};
@@ -1412,7 +1413,7 @@ impl Project {
14121413
};
14131414
let source = Vc::upcast(FileSource::new(fs_path.clone()));
14141415

1415-
let runtime = parse_segment_config_from_source(source, false)
1416+
let runtime = parse_segment_config_from_source(source, ParseSegmentMode::Base)
14161417
.await?
14171418
.runtime
14181419
.unwrap_or(NextRuntime::Edge);

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ use crate::{
1515
next_config::{NextConfig, OutputType},
1616
next_edge::entry::wrap_edge_entry,
1717
parse_segment_config_from_source,
18-
segment_config::NextSegmentConfig,
18+
segment_config::{NextSegmentConfig, ParseSegmentMode},
1919
util::{NextRuntime, app_function_name, load_next_js_template},
2020
};
2121

@@ -36,7 +36,7 @@ pub async fn get_app_route_entry(
3636
original_segment_config: Option<Vc<NextSegmentConfig>>,
3737
next_config: Vc<NextConfig>,
3838
) -> Result<Vc<AppEntry>> {
39-
let segment_from_source = parse_segment_config_from_source(source, true);
39+
let segment_from_source = parse_segment_config_from_source(source, ParseSegmentMode::App);
4040
let config = if let Some(original_segment_config) = original_segment_config {
4141
let mut segment_config = segment_from_source.owned().await?;
4242
segment_config.apply_parent_config(&*original_segment_config.await?);

crates/next-core/src/next_app/metadata/route.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ use crate::{
2727
},
2828
next_config::NextConfig,
2929
parse_segment_config_from_source,
30+
segment_config::ParseSegmentMode,
3031
};
3132

3233
/// Computes the route source for a Next.js metadata file.
@@ -68,7 +69,7 @@ pub async fn get_app_metadata_route_entry(
6869
let original_path = metadata.clone().into_path();
6970

7071
let source = Vc::upcast(FileSource::new(original_path));
71-
let segment_config = parse_segment_config_from_source(source, true);
72+
let segment_config = parse_segment_config_from_source(source, ParseSegmentMode::App);
7273
let is_dynamic_metadata = matches!(metadata, MetadataItem::Dynamic { .. });
7374
let is_multi_dynamic: bool = if Some(segment_config).is_some() {
7475
// is_multi_dynamic is true when config.generateSitemaps or

crates/next-core/src/segment_config.rs

Lines changed: 28 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ use swc_core::{
1515
};
1616
use turbo_rcstr::{RcStr, rcstr};
1717
use turbo_tasks::{
18-
NonLocalValue, ResolvedVc, TryJoinIterExt, ValueDefault, Vc, trace::TraceRawVcs,
18+
NonLocalValue, ResolvedVc, TaskInput, TryJoinIterExt, ValueDefault, Vc, trace::TraceRawVcs,
1919
util::WrapFuture,
2020
};
2121
use turbo_tasks_fs::FileSystemPath;
@@ -281,10 +281,29 @@ impl Issue for NextSegmentConfigParsingIssue {
281281
}
282282
}
283283

284+
#[derive(
285+
Debug,
286+
Clone,
287+
Copy,
288+
PartialEq,
289+
Eq,
290+
Hash,
291+
Serialize,
292+
Deserialize,
293+
TaskInput,
294+
NonLocalValue,
295+
TraceRawVcs,
296+
)]
297+
pub enum ParseSegmentMode {
298+
Base,
299+
// Disallows "use client + generateStatic" and ignores/warns about `export const config`
300+
App,
301+
}
302+
284303
#[turbo_tasks::function]
285304
pub async fn parse_segment_config_from_source(
286305
source: ResolvedVc<Box<dyn Source>>,
287-
is_app_router: bool,
306+
mode: ParseSegmentMode,
288307
) -> Result<Vc<NextSegmentConfig>> {
289308
let path = source.ident().path().await?;
290309

@@ -334,16 +353,7 @@ pub async fn parse_segment_config_from_source(
334353
let mut config = NextSegmentConfig::default();
335354

336355
let mut parse = async |ident, init, span| {
337-
parse_config_value(
338-
source,
339-
is_app_router,
340-
&mut config,
341-
eval_context,
342-
ident,
343-
init,
344-
span,
345-
)
346-
.await
356+
parse_config_value(source, mode, &mut config, eval_context, ident, init, span).await
347357
};
348358

349359
for item in &module_ast.body {
@@ -425,7 +435,7 @@ pub async fn parse_segment_config_from_source(
425435
)
426436
.await?;
427437

428-
if is_app_router
438+
if mode == ParseSegmentMode::App
429439
&& let Some(span) = config.generate_static_params
430440
&& module_ast
431441
.body
@@ -491,7 +501,7 @@ async fn invalid_config(
491501

492502
async fn parse_config_value(
493503
source: ResolvedVc<Box<dyn Source>>,
494-
is_app_router: bool,
504+
mode: ParseSegmentMode,
495505
config: &mut NextSegmentConfig,
496506
eval_context: &EvalContext,
497507
key: &str,
@@ -534,7 +544,7 @@ async fn parse_config_value(
534544
.await;
535545
};
536546

537-
if is_app_router {
547+
if mode == ParseSegmentMode::App {
538548
return invalid_config(
539549
source,
540550
"config",
@@ -1258,7 +1268,9 @@ async fn parse_segment_config_from_loader_tree_internal(
12581268
.flatten()
12591269
{
12601270
let source = Vc::upcast(FileSource::new(path.clone()));
1261-
config.apply_parent_config(&*parse_segment_config_from_source(source, true).await?);
1271+
config.apply_parent_config(
1272+
&*parse_segment_config_from_source(source, ParseSegmentMode::App).await?,
1273+
);
12621274
}
12631275

12641276
Ok(config)

0 commit comments

Comments
 (0)