Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

revalidate should be more than a boolean #51627

Merged
merged 5 commits into from Jun 24, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
39 changes: 32 additions & 7 deletions packages/next-swc/crates/next-core/src/app_segment_config.rs
Expand Up @@ -21,7 +21,7 @@ use turbopack_binding::turbopack::{
source_asset::SourceAssetVc,
},
ecmascript::{
analyzer::{graph::EvalContext, JsValue},
analyzer::{graph::EvalContext, ConstantNumber, ConstantValue, JsValue},
parse::ParseResult,
EcmascriptModuleAssetVc,
},
Expand Down Expand Up @@ -52,12 +52,22 @@ pub enum NextSegmentFetchCache {
ForceNoStore,
}

#[derive(Default, PartialEq, Eq, Clone, Copy, Debug, TraceRawVcs, Serialize, Deserialize)]
pub enum NextRevalidate {
#[default]
Never,
ForceCache,
Frequency {
seconds: u32,
},
}

#[turbo_tasks::value]
#[derive(Debug, Default)]
pub struct NextSegmentConfig {
pub dynamic: Option<NextSegmentDynamic>,
pub dynamic_params: Option<bool>,
pub revalidate: Option<bool>,
pub revalidate: Option<NextRevalidate>,
pub fetch_cache: Option<NextSegmentFetchCache>,
pub runtime: Option<NextRuntime>,
pub preferred_region: Option<String>,
Expand Down Expand Up @@ -294,11 +304,26 @@ fn parse_config_value(
}
"revalidate" => {
let value = eval_context.eval(init);
let Some(val) = value.as_bool() else {
return invalid_config("`revalidate` needs to be a static boolean", &value);
};

config.revalidate = Some(val);
match value {
JsValue::Constant(ConstantValue::Num(ConstantNumber(val))) if val >= 0.0 => {
config.revalidate = Some(NextRevalidate::Frequency {
seconds: val as u32,
});
}
JsValue::Constant(ConstantValue::False) => {
config.revalidate = Some(NextRevalidate::Never);
}
JsValue::Constant(ConstantValue::Str(str)) if str.as_str() == "force-cache" => {
config.revalidate = Some(NextRevalidate::ForceCache);
}
_ => {
return invalid_config(
"`revalidate` needs to be static false, static 'force-cache' or a static \
positive integer",
&value,
)
}
}
}
"fetchCache" => {
let value = eval_context.eval(init);
Expand Down