Unverified Commit 10463c73 authored by Luke Sandberg's avatar Luke Sandberg Committed by GitHub

Turbopack: keep env and preview props invalidations key-granular on warm start (#100425)

parent 3cb82f9c
......@@ -133,6 +133,10 @@ pub struct DraftModeOptions {
pub preview_mode_signing_key: RcStr,
}
/// A cell holding [`DraftModeOptions`], some of these change in every session.
#[turbo_tasks::value(transparent)]
struct DraftModeOptionsValue(DraftModeOptions);
#[turbo_tasks::task_input]
#[derive(
Debug,
......@@ -973,7 +977,7 @@ impl ProjectContainer {
versioned_content_map: self.versioned_content_map,
build_id,
encryption_key,
preview_props,
preview_props: ResolvedVc::cell(preview_props),
no_mangling,
write_routes_hashes_manifest,
current_node_js_version,
......@@ -1063,7 +1067,7 @@ pub struct Project {
encryption_key: RcStr,
preview_props: DraftModeOptions,
preview_props: ResolvedVc<DraftModeOptionsValue>,
/// When the code is minified, this opts out of the default mangling of
/// local names for variables, functions etc., which can be useful for
......@@ -1680,15 +1684,16 @@ impl Project {
}
#[turbo_tasks::function]
pub(super) fn edge_env(&self) -> Vc<EnvMap> {
pub(super) async fn edge_env(&self) -> Result<Vc<EnvMap>> {
let preview_props = self.preview_props.await?;
let edge_env = fxindexmap! {
rcstr!("__NEXT_BUILD_ID") => self.build_id.clone(),
rcstr!("NEXT_SERVER_ACTIONS_ENCRYPTION_KEY") => self.encryption_key.clone(),
rcstr!("__NEXT_PREVIEW_MODE_ID") => self.preview_props.preview_mode_id.clone(),
rcstr!("__NEXT_PREVIEW_MODE_ENCRYPTION_KEY") => self.preview_props.preview_mode_encryption_key.clone(),
rcstr!("__NEXT_PREVIEW_MODE_SIGNING_KEY") => self.preview_props.preview_mode_signing_key.clone(),
rcstr!("__NEXT_PREVIEW_MODE_ID") => preview_props.preview_mode_id.clone(),
rcstr!("__NEXT_PREVIEW_MODE_ENCRYPTION_KEY") => preview_props.preview_mode_encryption_key.clone(),
rcstr!("__NEXT_PREVIEW_MODE_SIGNING_KEY") => preview_props.preview_mode_signing_key.clone(),
};
Vc::cell(edge_env)
Ok(Vc::cell(edge_env))
}
#[turbo_tasks::function]
......
......@@ -30,9 +30,28 @@ impl TransientEnvMap {
}
}
#[turbo_tasks::value(transparent)]
/// A map of environment variables.
#[turbo_tasks::value(transparent, cell = "keyed")]
pub struct EnvMap(#[bincode(with = "turbo_bincode::indexmap")] FxIndexMap<RcStr, RcStr>);
/// An [`EnvMap`] with uppercased keys, used for key-granular case-insensitive lookups.
///
/// Unlike [`TransientEnvMap`] this is persisted: it only contains values that are already
/// persisted in the [`EnvMap`] it's derived from. Keyed comparison needs the previous content, so
/// persisting it keeps a dirty recomputation after a warm restore from invalidating every reader.
#[turbo_tasks::value(transparent, cell = "keyed")]
struct UppercaseEnvMap(#[bincode(with = "turbo_bincode::indexmap")] FxIndexMap<RcStr, RcStr>);
#[turbo_tasks::function]
async fn uppercase_env_map(map: Vc<EnvMap>) -> Result<Vc<UppercaseEnvMap>> {
let map = &*map.await?;
let mut new = FxIndexMap::with_capacity_and_hasher(map.len(), Default::default());
for (k, v) in map {
new.insert(k.to_uppercase().into(), v.clone());
}
Ok(Vc::cell(new))
}
#[turbo_tasks::value_impl]
impl EnvMap {
#[turbo_tasks::function]
......@@ -48,9 +67,14 @@ impl ProcessEnv for EnvMap {
Ok(Vc::cell((*self.await?).clone()))
}
/// Reads a single env variable, ignoring casing. Only depends on the requested variable, so
/// it isn't invalidated when other variables change.
#[turbo_tasks::function]
fn read(self: Vc<Self>, name: RcStr) -> Vc<Option<RcStr>> {
case_insensitive_read(self.read_all(), name)
async fn read(self: Vc<Self>, name: RcStr) -> Result<Vc<Option<RcStr>>> {
let value = uppercase_env_map(self)
.get(name.to_uppercase().as_str())
.await?;
Ok(Vc::cell(value.as_deref().cloned()))
}
}
......
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