Skip to content

Commit

Permalink
Implement preferredRegions array in Turbopack
Browse files Browse the repository at this point in the history
  • Loading branch information
timneutkens committed Oct 12, 2023
1 parent ebebb6a commit d4c91d7
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 6 deletions.
2 changes: 1 addition & 1 deletion packages/next-swc/crates/next-api/src/app.rs
Expand Up @@ -766,7 +766,7 @@ impl AppEndpoint {
.await?
.preferred_region
.clone()
.map(Regions::Single),
.map(Regions::Multiple),
matchers: vec![matchers],
..Default::default()
};
Expand Down
35 changes: 30 additions & 5 deletions packages/next-swc/crates/next-core/src/app_segment_config.rs
Expand Up @@ -69,7 +69,7 @@ pub struct NextSegmentConfig {
pub revalidate: Option<NextRevalidate>,
pub fetch_cache: Option<NextSegmentFetchCache>,
pub runtime: Option<NextRuntime>,
pub preferred_region: Option<String>,
pub preferred_region: Option<Vec<String>>,
}

#[turbo_tasks::value_impl]
Expand Down Expand Up @@ -358,12 +358,37 @@ fn parse_config_value(
}
"preferredRegion" => {
let value = eval_context.eval(init);
let Some(val) = value.as_str() else {
invalid_config("`preferredRegion` needs to be a static string", &value);
return;

let preferred_region = match value {
// Single value is turned into a single-element Vec.
JsValue::Constant(ConstantValue::Str(str)) => vec![str.to_string()],
// Array of strings is turned into a Vec. If one of the values in not a String it
// will error.
JsValue::Array { items, .. } => {
let mut regions = Vec::new();
for item in items {
if let JsValue::Constant(ConstantValue::Str(str)) = item {
regions.push(str.to_string());
} else {
invalid_config(
"Values of the `preferredRegion` array need to static strings",
&item,
);
return;
}
}
regions
}
_ => {
invalid_config(
"`preferredRegion` needs to be a static string or array of static strings",
&value,
);
return;
}
};

config.preferred_region = Some(val.to_string());
config.preferred_region = Some(preferred_region);
}
_ => {}
}
Expand Down

0 comments on commit d4c91d7

Please sign in to comment.