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

build(edge): extract buildId into environment #64521

Merged
merged 16 commits into from
May 6, 2024
45 changes: 43 additions & 2 deletions packages/next-swc/crates/napi/src/next_api/project.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
use next_api::{
entrypoints::Entrypoints,
project::{
DefineEnv, Instrumentation, Middleware, PartialProjectOptions, Project, ProjectContainer,
ProjectOptions,
DefineEnv, DraftModeOptions, Instrumentation, Middleware, PartialProjectOptions, Project,
ProjectContainer, ProjectOptions,
},
route::{Endpoint, Route},
};
Expand Down Expand Up @@ -63,6 +63,23 @@
pub value: String,
}

#[napi(object)]
pub struct NapiDraftModeOptions {
pub preview_mode_id: String,
pub preview_mode_encryption_key: String,
pub preview_mode_signing_key: String,
}

impl From<NapiDraftModeOptions> for DraftModeOptions {
fn from(val: NapiDraftModeOptions) -> Self {
DraftModeOptions {
preview_mode_id: val.preview_mode_id,
preview_mode_encryption_key: val.preview_mode_encryption_key,
preview_mode_signing_key: val.preview_mode_signing_key,
}
}
}

#[napi(object)]
pub struct NapiProjectOptions {
/// A root path from which all files must be nested under. Trying to access
Expand Down Expand Up @@ -94,6 +111,15 @@

/// The mode in which Next.js is running.
pub dev: bool,

/// The server actions encryption key.
pub encryption_key: String,

/// The build id.
pub build_id: String,

/// Options for draft mode.
pub preview_props: NapiDraftModeOptions,
}

/// [NapiProjectOptions] with all fields optional.
Expand Down Expand Up @@ -128,6 +154,15 @@

/// The mode in which Next.js is running.
pub dev: Option<bool>,

/// The server actions encryption key.
pub encryption_key: Option<String>,

/// The build id.
pub build_id: Option<String>,

/// Options for draft mode.
pub preview_props: Option<NapiDraftModeOptions>,
}

#[napi(object)]
Expand Down Expand Up @@ -159,6 +194,9 @@
.collect(),
define_env: val.define_env.into(),
dev: val.dev,
encryption_key: val.encryption_key,
build_id: val.build_id,
preview_props: val.preview_props.into(),
}
}
}
Expand All @@ -176,6 +214,9 @@
.map(|env| env.into_iter().map(|var| (var.name, var.value)).collect()),
define_env: val.define_env.map(|env| env.into()),
dev: val.dev,
encryption_key: val.encryption_key,
build_id: val.build_id,
preview_props: val.preview_props.map(|props| props.into()),
}
}
}
Expand Down Expand Up @@ -761,7 +802,7 @@
}
}

/// Subscribes to lifecycle events of the compilation.

Check warning on line 805 in packages/next-swc/crates/napi/src/next_api/project.rs

View workflow job for this annotation

GitHub Actions / rustdoc check / build

public documentation for `project_update_info_subscribe` links to private item `UpdateMessage::Start`

Check warning on line 805 in packages/next-swc/crates/napi/src/next_api/project.rs

View workflow job for this annotation

GitHub Actions / rustdoc check / build

public documentation for `project_update_info_subscribe` links to private item `UpdateMessage::End`

Check warning on line 805 in packages/next-swc/crates/napi/src/next_api/project.rs

View workflow job for this annotation

GitHub Actions / rustdoc check / build

public documentation for `project_update_info_subscribe` links to private item `UpdateMessage::End`

Check warning on line 805 in packages/next-swc/crates/napi/src/next_api/project.rs

View workflow job for this annotation

GitHub Actions / rustdoc check / build

public documentation for `project_update_info_subscribe` links to private item `UpdateMessage::Start`
/// Emits an [UpdateMessage::Start] event when any computation starts.
/// Emits an [UpdateMessage::End] event when there was no computation for the
/// specified time (`aggregation_ms`). The [UpdateMessage::End] event contains
Expand Down
1 change: 1 addition & 0 deletions packages/next-swc/crates/next-api/src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1100,6 +1100,7 @@ impl AppEndpoint {
.clone()
.map(Regions::Multiple),
matchers: vec![matchers],
env: this.app_project.project().edge_env().await?.clone_value(),
..Default::default()
};
let middleware_manifest_v2 = MiddlewaresManifestV2 {
Expand Down
1 change: 1 addition & 0 deletions packages/next-swc/crates/next-api/src/middleware.rs
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,7 @@ impl MiddlewareEndpoint {
page: "/".to_string(),
regions: None,
matchers,
env: this.project.edge_env().await?.clone_value(),
..Default::default()
};
let middleware_manifest_v2 = MiddlewaresManifestV2 {
Expand Down
1 change: 1 addition & 0 deletions packages/next-swc/crates/next-api/src/pages.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1095,6 +1095,7 @@ impl PageEndpoint {
page: original_name.to_string(),
regions: None,
matchers: vec![matchers],
env: this.pages_project.project().edge_env().await?.clone_value(),
..Default::default()
};
let middleware_manifest_v2 = MiddlewaresManifestV2 {
Expand Down
135 changes: 106 additions & 29 deletions packages/next-swc/crates/next-api/src/project.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::path::MAIN_SEPARATOR;

use anyhow::Result;
use indexmap::{map::Entry, IndexMap};
use indexmap::{indexmap, map::Entry, IndexMap};
use next_core::{
all_assets_from_entries,
app_structure::find_app_dir,
Expand Down Expand Up @@ -68,6 +68,14 @@ use crate::{
versioned_content_map::{OutputAssetsOperation, VersionedContentMap},
};

#[derive(Debug, Serialize, Deserialize, Clone, TaskInput, PartialEq, Eq, TraceRawVcs)]
#[serde(rename_all = "camelCase")]
pub struct DraftModeOptions {
pub preview_mode_id: String,
pub preview_mode_encryption_key: String,
pub preview_mode_signing_key: String,
}

#[derive(Debug, Serialize, Deserialize, Clone, TaskInput, PartialEq, Eq, TraceRawVcs)]
#[serde(rename_all = "camelCase")]
pub struct ProjectOptions {
Expand Down Expand Up @@ -96,6 +104,15 @@ pub struct ProjectOptions {

/// The mode in which Next.js is running.
pub dev: bool,

/// The server actions encryption key.
pub encryption_key: String,

/// The build id.
pub build_id: String,

/// Options for draft mode.
pub preview_props: DraftModeOptions,
}

#[derive(Debug, Serialize, Deserialize, Clone, TaskInput, PartialEq, Eq, TraceRawVcs)]
Expand Down Expand Up @@ -126,6 +143,15 @@ pub struct PartialProjectOptions {

/// The mode in which Next.js is running.
pub dev: Option<bool>,

/// The server actions encryption key.
pub encryption_key: Option<String>,

/// The build id.
pub build_id: Option<String>,

/// Options for draft mode.
pub preview_props: Option<DraftModeOptions>,
}

#[derive(Debug, Serialize, Deserialize, Clone, TaskInput, PartialEq, Eq, TraceRawVcs)]
Expand Down Expand Up @@ -166,29 +192,55 @@ impl ProjectContainer {

#[turbo_tasks::function]
pub fn update(&self, options: PartialProjectOptions) -> Vc<()> {
let PartialProjectOptions {
root_path,
project_path,
next_config,
js_config,
env,
define_env,
watch,
dev,
encryption_key,
build_id,
preview_props,
} = options;

let mut new_options = self.options_state.get().clone();

if let Some(root_path) = options.root_path {
if let Some(root_path) = root_path {
new_options.root_path = root_path;
}
if let Some(project_path) = options.project_path {
if let Some(project_path) = project_path {
new_options.project_path = project_path;
}
if let Some(next_config) = options.next_config {
if let Some(next_config) = next_config {
new_options.next_config = next_config;
}
if let Some(js_config) = options.js_config {
if let Some(js_config) = js_config {
new_options.js_config = js_config;
}
if let Some(env) = options.env {
if let Some(env) = env {
new_options.env = env;
}
if let Some(define_env) = options.define_env {
if let Some(define_env) = define_env {
new_options.define_env = define_env;
}
if let Some(watch) = options.watch {
if let Some(watch) = watch {
new_options.watch = watch;
}
if let Some(dev) = dev {
new_options.dev = dev;
}
if let Some(encryption_key) = encryption_key {
new_options.encryption_key = encryption_key;
}
if let Some(build_id) = build_id {
new_options.build_id = build_id;
}
if let Some(preview_props) = preview_props {
new_options.preview_props = preview_props;
}

// TODO: Handle mode switch, should prevent mode being switched.

Expand All @@ -201,32 +253,36 @@ impl ProjectContainer {
pub async fn project(self: Vc<Self>) -> Result<Vc<Project>> {
let this = self.await?;

let (env, define_env, next_config, js_config, root_path, project_path, watch, dev) = {
let env_map: Vc<EnvMap>;
let next_config;
let define_env;
let js_config;
let root_path;
let project_path;
let watch;
let dev;
let encryption_key;
let build_id;
let preview_props;
{
let options = this.options_state.get();
let env: Vc<EnvMap> = Vc::cell(options.env.iter().cloned().collect());
let define_env: Vc<ProjectDefineEnv> = ProjectDefineEnv {
env_map = Vc::cell(options.env.iter().cloned().collect());
define_env = ProjectDefineEnv {
client: Vc::cell(options.define_env.client.iter().cloned().collect()),
edge: Vc::cell(options.define_env.edge.iter().cloned().collect()),
nodejs: Vc::cell(options.define_env.nodejs.iter().cloned().collect()),
}
.cell();
let next_config = NextConfig::from_string(Vc::cell(options.next_config.clone()));
let js_config = JsConfig::from_string(Vc::cell(options.js_config.clone()));
let root_path = options.root_path.clone();
let project_path = options.project_path.clone();
let watch = options.watch;
let dev = options.dev;
(
env,
define_env,
next_config,
js_config,
root_path,
project_path,
watch,
dev,
)
};
next_config = NextConfig::from_string(Vc::cell(options.next_config.clone()));
js_config = JsConfig::from_string(Vc::cell(options.js_config.clone()));
root_path = options.root_path.clone();
project_path = options.project_path.clone();
watch = options.watch;
dev = options.dev;
encryption_key = options.encryption_key.clone();
build_id = options.build_id.clone();
preview_props = options.preview_props.clone();
}

let dist_dir = next_config
.await?
Expand All @@ -241,7 +297,7 @@ impl ProjectContainer {
next_config,
js_config,
dist_dir,
env: Vc::upcast(env),
env: Vc::upcast(env_map),
define_env,
browserslist_query: "last 1 Chrome versions, last 1 Firefox versions, last 1 Safari \
versions, last 1 Edge versions"
Expand All @@ -252,6 +308,9 @@ impl ProjectContainer {
NextMode::Build.cell()
},
versioned_content_map: this.versioned_content_map,
build_id,
encryption_key,
preview_props,
}
.cell())
}
Expand Down Expand Up @@ -323,6 +382,12 @@ pub struct Project {
mode: Vc<NextMode>,

versioned_content_map: Vc<VersionedContentMap>,

build_id: String,

encryption_key: String,

preview_props: DraftModeOptions,
}

#[turbo_tasks::value]
Expand Down Expand Up @@ -545,6 +610,18 @@ impl Project {
))
}

#[turbo_tasks::function]
pub(super) fn edge_env(&self) -> Vc<EnvMap> {
let edge_env = indexmap! {
"__NEXT_BUILD_ID".to_string() => self.build_id.clone(),
"NEXT_SERVER_ACTIONS_ENCRYPTION_KEY".to_string() => self.encryption_key.clone(),
"__NEXT_PREVIEW_MODE_ID".to_string() => self.preview_props.preview_mode_id.clone(),
"__NEXT_PREVIEW_MODE_ENCRYPTION_KEY".to_string() => self.preview_props.preview_mode_encryption_key.clone(),
"__NEXT_PREVIEW_MODE_SIGNING_KEY".to_string() => self.preview_props.preview_mode_signing_key.clone(),
};
Vc::cell(edge_env)
}

#[turbo_tasks::function]
pub(super) async fn client_chunking_context(
self: Vc<Self>,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,6 @@ async fn wrap_edge_page(
let next_config = &*next_config.await?;

// TODO(WEB-1824): add build support
let build_id = "development";
let dev = true;

// TODO(timneutkens): remove this
Expand All @@ -174,7 +173,6 @@ async fn wrap_edge_page(
indexmap! {
"VAR_USERLAND" => INNER.to_string(),
"VAR_PAGE" => page.to_string(),
"VAR_BUILD_ID" => build_id.to_string(),
},
indexmap! {
"sriEnabled" => serde_json::Value::Bool(sri_enabled).to_string(),
Expand Down
3 changes: 2 additions & 1 deletion packages/next-swc/crates/next-core/src/next_manifests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ pub(crate) mod client_reference_manifest;

use std::collections::HashMap;

use indexmap::IndexSet;
use indexmap::{IndexMap, IndexSet};
use serde::{Deserialize, Serialize};
use turbo_tasks::{trace::TraceRawVcs, TaskInput};

Expand Down Expand Up @@ -88,6 +88,7 @@ pub struct EdgeFunctionDefinition {
pub assets: Vec<AssetBinding>,
#[serde(skip_serializing_if = "Option::is_none")]
pub regions: Option<Regions>,
pub env: IndexMap<String, String>,
}

#[derive(Serialize, Default, Debug)]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,6 @@ async fn wrap_edge_page(
let next_config = &*next_config.await?;

// TODO(WEB-1824): add build support
let build_id = "development";
let dev = true;

let sri_enabled = !dev
Expand All @@ -229,7 +228,6 @@ async fn wrap_edge_page(
indexmap! {
"VAR_USERLAND" => INNER.to_string(),
"VAR_PAGE" => pathname.clone(),
"VAR_BUILD_ID" => build_id.to_string(),
"VAR_MODULE_DOCUMENT" => INNER_DOCUMENT.to_string(),
"VAR_MODULE_APP" => INNER_APP.to_string(),
"VAR_MODULE_GLOBAL_ERROR" => INNER_ERROR.to_string(),
Expand Down
1 change: 0 additions & 1 deletion packages/next/src/build/entries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -406,7 +406,6 @@ export function getEdgeServerEntry(opts: {
absoluteDocumentPath: opts.pages['/_document'],
absoluteErrorPath: opts.pages['/_error'],
absolutePagePath: opts.absolutePagePath,
buildId: opts.buildId,
dev: opts.isDev,
isServerComponent: opts.isServerComponent,
page: opts.page,
Expand Down
Loading
Loading