diff --git a/packages/next-swc/crates/napi/src/next_api/project.rs b/packages/next-swc/crates/napi/src/next_api/project.rs index 1c70a93c7a5f9..396f6ad2b6c58 100644 --- a/packages/next-swc/crates/napi/src/next_api/project.rs +++ b/packages/next-swc/crates/napi/src/next_api/project.rs @@ -9,8 +9,8 @@ use napi::{ use next_api::{ entrypoints::Entrypoints, project::{ - DefineEnv, DraftModeOptions, Instrumentation, Middleware, PartialProjectOptions, Project, - ProjectContainer, ProjectOptions, + DefineEnv, Instrumentation, Middleware, PartialProjectOptions, Project, ProjectContainer, + ProjectOptions, }, route::{Endpoint, Route}, }; @@ -63,23 +63,6 @@ pub struct NapiEnvVar { 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 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 @@ -111,15 +94,6 @@ pub struct NapiProjectOptions { /// 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. @@ -154,15 +128,6 @@ pub struct NapiPartialProjectOptions { /// The mode in which Next.js is running. pub dev: Option, - - /// The server actions encryption key. - pub encryption_key: Option, - - /// The build id. - pub build_id: Option, - - /// Options for draft mode. - pub preview_props: Option, } #[napi(object)] @@ -194,9 +159,6 @@ impl From for ProjectOptions { .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(), } } } @@ -214,9 +176,6 @@ impl From for PartialProjectOptions { .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()), } } } diff --git a/packages/next-swc/crates/next-api/src/app.rs b/packages/next-swc/crates/next-api/src/app.rs index 61515c5c4f35c..21763411e2ef8 100644 --- a/packages/next-swc/crates/next-api/src/app.rs +++ b/packages/next-swc/crates/next-api/src/app.rs @@ -1100,7 +1100,6 @@ 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 { diff --git a/packages/next-swc/crates/next-api/src/middleware.rs b/packages/next-swc/crates/next-api/src/middleware.rs index f509b954155f3..89fbd46c50a86 100644 --- a/packages/next-swc/crates/next-api/src/middleware.rs +++ b/packages/next-swc/crates/next-api/src/middleware.rs @@ -161,7 +161,6 @@ impl MiddlewareEndpoint { page: "/".to_string(), regions: None, matchers, - env: this.project.edge_env().await?.clone_value(), ..Default::default() }; let middleware_manifest_v2 = MiddlewaresManifestV2 { diff --git a/packages/next-swc/crates/next-api/src/pages.rs b/packages/next-swc/crates/next-api/src/pages.rs index e4e7595cc2bb4..b76e96322af53 100644 --- a/packages/next-swc/crates/next-api/src/pages.rs +++ b/packages/next-swc/crates/next-api/src/pages.rs @@ -1095,7 +1095,6 @@ 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 { diff --git a/packages/next-swc/crates/next-api/src/project.rs b/packages/next-swc/crates/next-api/src/project.rs index 9c9c9eaf05367..ee0ee8333549c 100644 --- a/packages/next-swc/crates/next-api/src/project.rs +++ b/packages/next-swc/crates/next-api/src/project.rs @@ -1,7 +1,7 @@ use std::path::MAIN_SEPARATOR; use anyhow::Result; -use indexmap::{indexmap, map::Entry, IndexMap}; +use indexmap::{map::Entry, IndexMap}; use next_core::{ all_assets_from_entries, app_structure::find_app_dir, @@ -68,14 +68,6 @@ 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 { @@ -104,15 +96,6 @@ 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)] @@ -143,15 +126,6 @@ pub struct PartialProjectOptions { /// The mode in which Next.js is running. pub dev: Option, - - /// The server actions encryption key. - pub encryption_key: Option, - - /// The build id. - pub build_id: Option, - - /// Options for draft mode. - pub preview_props: Option, } #[derive(Debug, Serialize, Deserialize, Clone, TaskInput, PartialEq, Eq, TraceRawVcs)] @@ -192,55 +166,29 @@ 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) = root_path { + if let Some(root_path) = options.root_path { new_options.root_path = root_path; } - if let Some(project_path) = project_path { + if let Some(project_path) = options.project_path { new_options.project_path = project_path; } - if let Some(next_config) = next_config { + if let Some(next_config) = options.next_config { new_options.next_config = next_config; } - if let Some(js_config) = js_config { + if let Some(js_config) = options.js_config { new_options.js_config = js_config; } - if let Some(env) = env { + if let Some(env) = options.env { new_options.env = env; } - if let Some(define_env) = define_env { + if let Some(define_env) = options.define_env { new_options.define_env = define_env; } - if let Some(watch) = watch { + if let Some(watch) = options.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. @@ -253,36 +201,32 @@ impl ProjectContainer { pub async fn project(self: Vc) -> Result> { let this = self.await?; - let env_map: Vc; - 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 (env, define_env, next_config, js_config, root_path, project_path, watch, dev) = { let options = this.options_state.get(); - env_map = Vc::cell(options.env.iter().cloned().collect()); - define_env = ProjectDefineEnv { + let env: Vc = Vc::cell(options.env.iter().cloned().collect()); + let define_env: Vc = 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(); - 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 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, + ) + }; let dist_dir = next_config .await? @@ -297,7 +241,7 @@ impl ProjectContainer { next_config, js_config, dist_dir, - env: Vc::upcast(env_map), + env: Vc::upcast(env), define_env, browserslist_query: "last 1 Chrome versions, last 1 Firefox versions, last 1 Safari \ versions, last 1 Edge versions" @@ -308,9 +252,6 @@ impl ProjectContainer { NextMode::Build.cell() }, versioned_content_map: this.versioned_content_map, - build_id, - encryption_key, - preview_props, } .cell()) } @@ -382,12 +323,6 @@ pub struct Project { mode: Vc, versioned_content_map: Vc, - - build_id: String, - - encryption_key: String, - - preview_props: DraftModeOptions, } #[turbo_tasks::value] @@ -610,18 +545,6 @@ impl Project { )) } - #[turbo_tasks::function] - pub(super) fn edge_env(&self) -> Vc { - 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, diff --git a/packages/next-swc/crates/next-core/src/next_app/app_page_entry.rs b/packages/next-swc/crates/next-core/src/next_app/app_page_entry.rs index 77f4a1b14857c..fdb7dc3193efe 100644 --- a/packages/next-swc/crates/next-core/src/next_app/app_page_entry.rs +++ b/packages/next-swc/crates/next-core/src/next_app/app_page_entry.rs @@ -152,6 +152,7 @@ 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 @@ -173,6 +174,7 @@ 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(), diff --git a/packages/next-swc/crates/next-core/src/next_manifests/mod.rs b/packages/next-swc/crates/next-core/src/next_manifests/mod.rs index 409078f8e0a00..632e0cf051232 100644 --- a/packages/next-swc/crates/next-core/src/next_manifests/mod.rs +++ b/packages/next-swc/crates/next-core/src/next_manifests/mod.rs @@ -4,7 +4,7 @@ pub(crate) mod client_reference_manifest; use std::collections::HashMap; -use indexmap::{IndexMap, IndexSet}; +use indexmap::IndexSet; use serde::{Deserialize, Serialize}; use turbo_tasks::{trace::TraceRawVcs, TaskInput}; @@ -88,7 +88,6 @@ pub struct EdgeFunctionDefinition { pub assets: Vec, #[serde(skip_serializing_if = "Option::is_none")] pub regions: Option, - pub env: IndexMap, } #[derive(Serialize, Default, Debug)] diff --git a/packages/next-swc/crates/next-core/src/next_pages/page_entry.rs b/packages/next-swc/crates/next-core/src/next_pages/page_entry.rs index 4800d5b772d4d..dbdfffaedac98 100644 --- a/packages/next-swc/crates/next-core/src/next_pages/page_entry.rs +++ b/packages/next-swc/crates/next-core/src/next_pages/page_entry.rs @@ -212,6 +212,7 @@ 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 @@ -228,6 +229,7 @@ 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(), diff --git a/packages/next/src/build/entries.ts b/packages/next/src/build/entries.ts index 9652b0a356d49..7ae4c637f1b98 100644 --- a/packages/next/src/build/entries.ts +++ b/packages/next/src/build/entries.ts @@ -406,6 +406,7 @@ 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, diff --git a/packages/next/src/build/index.ts b/packages/next/src/build/index.ts index 0bc4c325e3da4..6b82ef457517a 100644 --- a/packages/next/src/build/index.ts +++ b/packages/next/src/build/index.ts @@ -1397,9 +1397,6 @@ export default async function build( // TODO: Implement middlewareMatchers: undefined, }), - buildId: NextBuildContext.buildId!, - encryptionKey: NextBuildContext.encryptionKey!, - previewProps: NextBuildContext.previewProps!, }) await fs.mkdir(path.join(distDir, 'server'), { recursive: true }) @@ -2753,8 +2750,7 @@ export default async function build( }, ] - // Always sort the routes to get consistent output in manifests - getSortedRoutes(routes).forEach((route) => { + routes.forEach((route) => { if (isDynamicRoute(page) && route === page) return if (route === UNDERSCORE_NOT_FOUND_ROUTE) return diff --git a/packages/next/src/build/swc/index.ts b/packages/next/src/build/swc/index.ts index 183605e4c1ba0..92a1e4845fb5c 100644 --- a/packages/next/src/build/swc/index.ts +++ b/packages/next/src/build/swc/index.ts @@ -19,7 +19,6 @@ import { isDeepStrictEqual } from 'util' import type { DefineEnvPluginOptions } from '../webpack/plugins/define-env-plugin' import { getDefineEnv } from '../webpack/plugins/define-env-plugin' import type { PageExtensions } from '../page-extensions-type' -import type { __ApiPreviewProps } from '../../server/api-utils' const nextVersion = process.env.__NEXT_VERSION as string @@ -388,6 +387,7 @@ function logLoadFailure(attempts: any, triedWasm = false) { process.exit(1) }) } + export interface ProjectOptions { /** * A root path from which all files must be nested under. Trying to access @@ -432,21 +432,6 @@ export interface ProjectOptions { * The mode in which Next.js is running. */ dev: boolean - - /** - * The server actions encryption key. - */ - encryptionKey: string - - /** - * The build id. - */ - buildId: string - - /** - * Options for draft mode. - */ - previewProps: __ApiPreviewProps } type RustifiedEnv = { name: string; value: string }[] diff --git a/packages/next/src/build/templates/edge-ssr-app.ts b/packages/next/src/build/templates/edge-ssr-app.ts index 195f9d1726de1..4d0de74d437f3 100644 --- a/packages/next/src/build/templates/edge-ssr-app.ts +++ b/packages/next/src/build/templates/edge-ssr-app.ts @@ -78,7 +78,7 @@ const render = getRender({ serverActions: isServerComponent ? serverActions : undefined, subresourceIntegrityManifest, config: nextConfig, - buildId: process.env.__NEXT_BUILD_ID!, + buildId: 'VAR_BUILD_ID', nextFontManifest, incrementalCacheHandler, interceptionRouteRewrites, diff --git a/packages/next/src/build/templates/edge-ssr.ts b/packages/next/src/build/templates/edge-ssr.ts index 6a311abe7b145..d5611928ab102 100644 --- a/packages/next/src/build/templates/edge-ssr.ts +++ b/packages/next/src/build/templates/edge-ssr.ts @@ -104,7 +104,7 @@ const render = getRender({ reactLoadableManifest, subresourceIntegrityManifest, config: nextConfig, - buildId: process.env.__NEXT_BUILD_ID!, + buildId: 'VAR_BUILD_ID', nextFontManifest, incrementalCacheHandler, }) diff --git a/packages/next/src/build/webpack-build/impl.ts b/packages/next/src/build/webpack-build/impl.ts index 39ff87a40a0b4..452060269102c 100644 --- a/packages/next/src/build/webpack-build/impl.ts +++ b/packages/next/src/build/webpack-build/impl.ts @@ -152,14 +152,7 @@ export async function webpackBuildImpl( middlewareMatchers: entrypoints.middlewareMatchers, compilerType: COMPILER_NAMES.edgeServer, entrypoints: entrypoints.edgeServer, - edgePreviewProps: { - __NEXT_PREVIEW_MODE_ID: - NextBuildContext.previewProps!.previewModeId, - __NEXT_PREVIEW_MODE_ENCRYPTION_KEY: - NextBuildContext.previewProps!.previewModeEncryptionKey, - __NEXT_PREVIEW_MODE_SIGNING_KEY: - NextBuildContext.previewProps!.previewModeSigningKey, - }, + edgePreviewProps: NextBuildContext.previewProps!, ...info, }), ]) diff --git a/packages/next/src/build/webpack-config.ts b/packages/next/src/build/webpack-config.ts index 594d5972eec5a..4541fb108b73e 100644 --- a/packages/next/src/build/webpack-config.ts +++ b/packages/next/src/build/webpack-config.ts @@ -1821,11 +1821,7 @@ export default async function getBaseWebpackConfig( dev, sriEnabled: !dev && !!config.experimental.sri?.algorithm, rewrites, - edgeEnvironments: { - __NEXT_BUILD_ID: buildId, - NEXT_SERVER_ACTIONS_ENCRYPTION_KEY: encryptionKey, - ...edgePreviewProps, - }, + edgeEnvironments: edgePreviewProps || {}, }), isClient && new BuildManifestPlugin({ diff --git a/packages/next/src/build/webpack/loaders/next-edge-ssr-loader/index.ts b/packages/next/src/build/webpack/loaders/next-edge-ssr-loader/index.ts index e2163b023a09a..8e82cf9aabe23 100644 --- a/packages/next/src/build/webpack/loaders/next-edge-ssr-loader/index.ts +++ b/packages/next/src/build/webpack/loaders/next-edge-ssr-loader/index.ts @@ -16,6 +16,7 @@ export type EdgeSSRLoaderQuery = { absoluteDocumentPath: string absoluteErrorPath: string absolutePagePath: string + buildId: string dev: boolean isServerComponent: boolean page: string @@ -64,6 +65,7 @@ const edgeSSRLoader: webpack.LoaderDefinitionFunction = const { dev, page, + buildId, absolutePagePath, absoluteAppPath, absoluteDocumentPath, @@ -143,6 +145,7 @@ const edgeSSRLoader: webpack.LoaderDefinitionFunction = { VAR_USERLAND: pageModPath, VAR_PAGE: page, + VAR_BUILD_ID: buildId, }, { sriEnabled: JSON.stringify(sriEnabled), @@ -164,6 +167,7 @@ const edgeSSRLoader: webpack.LoaderDefinitionFunction = { VAR_USERLAND: pageModPath, VAR_PAGE: page, + VAR_BUILD_ID: buildId, VAR_MODULE_DOCUMENT: documentPath, VAR_MODULE_APP: appPath, VAR_MODULE_GLOBAL_ERROR: errorPath, diff --git a/packages/next/src/build/webpack/plugins/build-manifest-plugin.ts b/packages/next/src/build/webpack/plugins/build-manifest-plugin.ts index 9be30ecee2499..bc2b5621d3e58 100644 --- a/packages/next/src/build/webpack/plugins/build-manifest-plugin.ts +++ b/packages/next/src/build/webpack/plugins/build-manifest-plugin.ts @@ -29,38 +29,6 @@ export type ClientBuildManifest = { // generated). export const srcEmptySsgManifest = `self.__SSG_MANIFEST=new Set;self.__SSG_MANIFEST_CB&&self.__SSG_MANIFEST_CB()` -// nodejs: '/static//low-priority.js' -function buildNodejsLowPriorityPath(filename: string, buildId: string) { - return `${CLIENT_STATIC_FILES_PATH}/${buildId}/${filename}` -} - -function createEdgeRuntimeManifest(originAssetMap: BuildManifest): string { - const manifestFilenames = ['_buildManifest.js', '_ssgManifest.js'] - - const assetMap: BuildManifest = { - ...originAssetMap, - lowPriorityFiles: [], - } - - const manifestDefCode = `self.__BUILD_MANIFEST = ${JSON.stringify( - assetMap, - null, - 2 - )};\n` - // edge lowPriorityFiles item: '"/static/" + process.env.__NEXT_BUILD_ID + "/low-priority.js"'. - // Since lowPriorityFiles is not fixed and relying on `process.env.__NEXT_BUILD_ID`, we'll produce code creating it dynamically. - const lowPriorityFilesCode = - `self.__BUILD_MANIFEST.lowPriorityFiles = [\n` + - manifestFilenames - .map((filename) => { - return `"/static/" + process.env.__NEXT_BUILD_ID + "/${filename}",\n` - }) - .join(',') + - `\n];` - - return manifestDefCode + lowPriorityFilesCode -} - function normalizeRewrite(item: { source: string destination: string @@ -263,25 +231,19 @@ export default class BuildManifestPlugin { // Add the runtime build manifest file (generated later in this file) // as a dependency for the app. If the flag is false, the file won't be // downloaded by the client. - const buildManifestPath = buildNodejsLowPriorityPath( - '_buildManifest.js', - this.buildId + assetMap.lowPriorityFiles.push( + `${CLIENT_STATIC_FILES_PATH}/${this.buildId}/_buildManifest.js` ) - const ssgManifestPath = buildNodejsLowPriorityPath( - '_ssgManifest.js', - this.buildId - ) - assetMap.lowPriorityFiles.push(buildManifestPath, ssgManifestPath) + const ssgManifestPath = `${CLIENT_STATIC_FILES_PATH}/${this.buildId}/_ssgManifest.js` + + assetMap.lowPriorityFiles.push(ssgManifestPath) assets[ssgManifestPath] = new sources.RawSource(srcEmptySsgManifest) } assetMap.pages = Object.keys(assetMap.pages) .sort() - .reduce( - // eslint-disable-next-line - (a, c) => ((a[c] = assetMap.pages[c]), a), - {} as typeof assetMap.pages - ) + // eslint-disable-next-line + .reduce((a, c) => ((a[c] = assetMap.pages[c]), a), {} as any) let buildManifestName = BUILD_MANIFEST @@ -294,7 +256,7 @@ export default class BuildManifestPlugin { ) assets[`server/${MIDDLEWARE_BUILD_MANIFEST}.js`] = new sources.RawSource( - `${createEdgeRuntimeManifest(assetMap)}` + `self.__BUILD_MANIFEST=${JSON.stringify(assetMap)}` ) if (!this.isDevFallback) { diff --git a/packages/next/src/build/webpack/plugins/flight-client-entry-plugin.ts b/packages/next/src/build/webpack/plugins/flight-client-entry-plugin.ts index 101171976b011..932c5356c3e04 100644 --- a/packages/next/src/build/webpack/plugins/flight-client-entry-plugin.ts +++ b/packages/next/src/build/webpack/plugins/flight-client-entry-plugin.ts @@ -1016,26 +1016,19 @@ export class FlightClientEntryPlugin { edgeServerActions[id] = action } - const serverManifest = { - node: serverActions, - edge: edgeServerActions, - encryptionKey: this.encryptionKey, - } - const edgeServerManifest = { - ...serverManifest, - encryptionKey: 'process.env.NEXT_SERVER_ACTIONS_ENCRYPTION_KEY', - } - - const json = JSON.stringify(serverManifest, null, this.dev ? 2 : undefined) - const edgeJson = JSON.stringify( - edgeServerManifest, + const json = JSON.stringify( + { + node: serverActions, + edge: edgeServerActions, + encryptionKey: this.encryptionKey, + }, null, this.dev ? 2 : undefined ) assets[`${this.assetPrefix}${SERVER_REFERENCE_MANIFEST}.js`] = new sources.RawSource( - `self.__RSC_SERVER_MANIFEST=${JSON.stringify(edgeJson)}` + `self.__RSC_SERVER_MANIFEST=${JSON.stringify(json)}` ) as unknown as webpack.sources.RawSource assets[`${this.assetPrefix}${SERVER_REFERENCE_MANIFEST}.json`] = new sources.RawSource(json) as unknown as webpack.sources.RawSource diff --git a/packages/next/src/build/webpack/plugins/middleware-plugin.ts b/packages/next/src/build/webpack/plugins/middleware-plugin.ts index 866aff2ae1a32..5c9ec94fd14c2 100644 --- a/packages/next/src/build/webpack/plugins/middleware-plugin.ts +++ b/packages/next/src/build/webpack/plugins/middleware-plugin.ts @@ -42,10 +42,10 @@ export interface EdgeFunctionDefinition { name: string page: string matchers: MiddlewareMatcher[] - env: Record wasm?: AssetBinding[] assets?: AssetBinding[] regions?: string[] | string + environments?: Record } export interface MiddlewareManifest { @@ -227,7 +227,7 @@ function getCreateAssets(params: { name, filePath, })), - env: opts.edgeEnvironments, + environments: opts.edgeEnvironments, ...(metadata.regions && { regions: metadata.regions }), } @@ -739,26 +739,18 @@ function getExtractMetadata(params: { } } -// These values will be replaced again in edge runtime deployment build. -// `buildId` represents BUILD_ID to be externalized in env vars. -// `encryptionKey` represents server action encryption key to be externalized in env vars. -type EdgeRuntimeEnvironments = Record & { - __NEXT_BUILD_ID: string - NEXT_SERVER_ACTIONS_ENCRYPTION_KEY: string -} - interface Options { dev: boolean sriEnabled: boolean rewrites: CustomRoutes['rewrites'] - edgeEnvironments: EdgeRuntimeEnvironments + edgeEnvironments: Record } export default class MiddlewarePlugin { private readonly dev: Options['dev'] private readonly sriEnabled: Options['sriEnabled'] private readonly rewrites: Options['rewrites'] - private readonly edgeEnvironments: EdgeRuntimeEnvironments + private readonly edgeEnvironments: Record constructor({ dev, sriEnabled, rewrites, edgeEnvironments }: Options) { this.dev = dev diff --git a/packages/next/src/server/dev/hot-reloader-turbopack.ts b/packages/next/src/server/dev/hot-reloader-turbopack.ts index f6409ae24184f..3220403c2a1cb 100644 --- a/packages/next/src/server/dev/hot-reloader-turbopack.ts +++ b/packages/next/src/server/dev/hot-reloader-turbopack.ts @@ -122,7 +122,6 @@ export async function createHotReloaderTurbopack( // of the current `next dev` invocation. hotReloaderSpan.stop() - const encryptionKey = await generateEncryptionKeyBase64(true) const project = await bindings.turbo.createProject({ projectPath: dir, rootPath: opts.nextConfig.experimental.outputFileTracingRoot || dir, @@ -143,9 +142,6 @@ export async function createHotReloaderTurbopack( // TODO: Implement middlewareMatchers: undefined, }), - buildId, - encryptionKey, - previewProps: opts.fsChecker.prerenderManifest.preview, }) const entrypointsSubscription = project.entrypointsSubscribe() @@ -169,7 +165,7 @@ export async function createHotReloaderTurbopack( const manifestLoader = new TurbopackManifestLoader({ buildId, distDir, - encryptionKey, + encryptionKey: await generateEncryptionKeyBase64(), }) // Dev specific diff --git a/packages/next/src/server/next-server.ts b/packages/next/src/server/next-server.ts index f05e37e22ad66..ad96b43ef48da 100644 --- a/packages/next/src/server/next-server.ts +++ b/packages/next/src/server/next-server.ts @@ -1435,7 +1435,6 @@ export default class NextNodeServer extends BaseServer< name: string paths: string[] wasm: { filePath: string; name: string }[] - env: { [key: string]: string } assets?: { filePath: string; name: string }[] } | null { const manifest = this.getMiddlewareManifest() @@ -1477,7 +1476,6 @@ export default class NextNodeServer extends BaseServer< filePath: join(this.distDir, binding.filePath), } }), - env: pageInfo.env, } } diff --git a/packages/next/src/server/web/sandbox/context.ts b/packages/next/src/server/web/sandbox/context.ts index 899758cb46f2a..a872f4dd31c51 100644 --- a/packages/next/src/server/web/sandbox/context.ts +++ b/packages/next/src/server/web/sandbox/context.ts @@ -107,14 +107,9 @@ async function loadWasm( return modules } -function buildEnvironmentVariablesFrom( - injectedEnvironments: Record -): Record { +function buildEnvironmentVariablesFrom(): Record { const pairs = Object.keys(process.env).map((key) => [key, process.env[key]]) const env = Object.fromEntries(pairs) - for (const key of Object.keys(injectedEnvironments)) { - env[key] = injectedEnvironments[key] - } env.NEXT_RUNTIME = 'edge' return env } @@ -127,16 +122,15 @@ Learn more: https://nextjs.org/docs/api-reference/edge-runtime`) throw error } -function createProcessPolyfill(env: Record) { - const processPolyfill = { env: buildEnvironmentVariablesFrom(env) } - const overriddenValue: Record = {} - +function createProcessPolyfill() { + const processPolyfill = { env: buildEnvironmentVariablesFrom() } + const overridenValue: Record = {} for (const key of Object.keys(process)) { if (key === 'env') continue Object.defineProperty(processPolyfill, key, { get() { - if (overriddenValue[key] !== undefined) { - return overriddenValue[key] + if (overridenValue[key] !== undefined) { + return overridenValue[key] } if (typeof (process as any)[key] === 'function') { return () => throwUnsupportedAPIError(`process.${key}`) @@ -144,7 +138,7 @@ function createProcessPolyfill(env: Record) { return undefined }, set(value) { - overriddenValue[key] = value + overridenValue[key] = value }, enumerable: false, }) @@ -250,15 +244,14 @@ export const requestStore = new AsyncLocalStorage<{ async function createModuleContext(options: ModuleContextOptions) { const warnedEvals = new Set() const warnedWasmCodegens = new Set() - const { edgeFunctionEntry } = options - const wasm = await loadWasm(edgeFunctionEntry.wasm ?? []) + const wasm = await loadWasm(options.edgeFunctionEntry.wasm ?? []) const runtime = new EdgeRuntime({ codeGeneration: process.env.NODE_ENV !== 'production' ? { strings: true, wasm: true } : undefined, extend: (context) => { - context.process = createProcessPolyfill(edgeFunctionEntry.env) + context.process = createProcessPolyfill() Object.defineProperty(context, 'require', { enumerable: false, @@ -477,7 +470,7 @@ interface ModuleContextOptions { onWarning: (warn: Error) => void useCache: boolean distDir: string - edgeFunctionEntry: Pick + edgeFunctionEntry: Pick } function getModuleContextShared(options: ModuleContextOptions) { diff --git a/test/development/basic/next-rs-api.test.ts b/test/development/basic/next-rs-api.test.ts index cde9ab66700ec..ce1137a93153f 100644 --- a/test/development/basic/next-rs-api.test.ts +++ b/test/development/basic/next-rs-api.test.ts @@ -218,13 +218,6 @@ describe('next.rs api', () => { hasRewrites: false, middlewareMatchers: undefined, }), - buildId: 'development', - encryptionKey: '12345', - previewProps: { - previewModeId: 'development', - previewModeEncryptionKey: '12345', - previewModeSigningKey: '12345', - }, }) projectUpdateSubscription = filterMapAsyncIterator( project.updateInfoSubscribe(1000), diff --git a/test/e2e/middleware-general/test/index.test.ts b/test/e2e/middleware-general/test/index.test.ts index c8100fc95952b..e5c23b4fe51ca 100644 --- a/test/e2e/middleware-general/test/index.test.ts +++ b/test/e2e/middleware-general/test/index.test.ts @@ -170,9 +170,9 @@ describe('Middleware Runtime', () => { ...manifest.middleware['/'], } const envs = { - ...middlewareWithoutEnvs.env, + ...middlewareWithoutEnvs.environments, } - delete middlewareWithoutEnvs.env + delete middlewareWithoutEnvs.environments expect(middlewareWithoutEnvs).toEqual({ files: expect.arrayContaining([ 'server/edge-runtime-webpack.js', @@ -186,11 +186,9 @@ describe('Middleware Runtime', () => { regions: 'auto', }) expect(envs).toContainAllKeys([ - 'NEXT_SERVER_ACTIONS_ENCRYPTION_KEY', - '__NEXT_BUILD_ID', - '__NEXT_PREVIEW_MODE_ENCRYPTION_KEY', - '__NEXT_PREVIEW_MODE_ID', - '__NEXT_PREVIEW_MODE_SIGNING_KEY', + 'previewModeEncryptionKey', + 'previewModeId', + 'previewModeSigningKey', ]) }) diff --git a/test/e2e/middleware-trailing-slash/test/index.test.ts b/test/e2e/middleware-trailing-slash/test/index.test.ts index 1ee913f5a5578..e6f04754930f3 100644 --- a/test/e2e/middleware-trailing-slash/test/index.test.ts +++ b/test/e2e/middleware-trailing-slash/test/index.test.ts @@ -112,7 +112,7 @@ describe('Middleware Runtime trailing slash', () => { const middlewareWithoutEnvs = { ...manifest.middleware['/'], } - delete middlewareWithoutEnvs.env + delete middlewareWithoutEnvs.environments expect(middlewareWithoutEnvs).toEqual({ files: expect.arrayContaining([ 'prerender-manifest.js', diff --git a/test/production/deterministic-build/app/app-page/edge/page.js b/test/production/deterministic-build/app/app-page/edge/page.js deleted file mode 100644 index 4d6fdccfface9..0000000000000 --- a/test/production/deterministic-build/app/app-page/edge/page.js +++ /dev/null @@ -1,5 +0,0 @@ -export default function Page() { - return 'app-page (edge)' -} - -export const runtime = 'edge' diff --git a/test/production/deterministic-build/app/app-page/page.js b/test/production/deterministic-build/app/app-page/page.js deleted file mode 100644 index ad1654219282e..0000000000000 --- a/test/production/deterministic-build/app/app-page/page.js +++ /dev/null @@ -1,3 +0,0 @@ -export default function Page() { - return 'app-page (node)' -} diff --git a/test/production/deterministic-build/app/app-route/edge/route.js b/test/production/deterministic-build/app/app-route/edge/route.js deleted file mode 100644 index fc4a0205deaab..0000000000000 --- a/test/production/deterministic-build/app/app-route/edge/route.js +++ /dev/null @@ -1,5 +0,0 @@ -export function GET() { - return new Response('app-route (edge)') -} - -export const runtime = 'edge' diff --git a/test/production/deterministic-build/app/app-route/route.js b/test/production/deterministic-build/app/app-route/route.js deleted file mode 100644 index b01445578f89d..0000000000000 --- a/test/production/deterministic-build/app/app-route/route.js +++ /dev/null @@ -1,5 +0,0 @@ -export function GET() { - return new Response('app-route (node)') -} - -export const dynamic = 'force-dynamic' diff --git a/test/production/deterministic-build/app/layout.js b/test/production/deterministic-build/app/layout.js deleted file mode 100644 index ff05382bcc3f1..0000000000000 --- a/test/production/deterministic-build/app/layout.js +++ /dev/null @@ -1,9 +0,0 @@ -export default function Layout({ children }) { - return ( - - {children} - - ) -} - -export const dynamic = 'force-dynamic' diff --git a/test/production/deterministic-build/index.test.ts b/test/production/deterministic-build/index.test.ts deleted file mode 100644 index c9bea7107803f..0000000000000 --- a/test/production/deterministic-build/index.test.ts +++ /dev/null @@ -1,68 +0,0 @@ -import crypto from 'crypto' -import { NextInstance, nextTestSetup } from 'e2e-utils' - -function generateMD5(text: string) { - const hash = crypto.createHash('md5') - hash.update(text) - return hash.digest('hex') -} - -const nodeFilePaths = [ - 'app/app-page/page', - 'app/app-route/route', - 'pages/api/pages-api', - 'pages/pages-page', -] - -async function getEdgeRouteFilesFromManifest(next: NextInstance) { - const manifest: any = JSON.parse( - await next.readFile('.next/server/middleware-manifest.json') - ) - const routeKeys = Object.keys(manifest.functions) - const md5Map: Record = {} - for (const route of routeKeys) { - const files: string[] = manifest.functions[route].files - const filesMd5Promises = files.map(async (filePath: string) => { - const content = await next.readFile(`.next/${filePath}`) - return generateMD5(content) - }) - const md5s = await Promise.all(filesMd5Promises) - md5Map[route] = md5s - } - return md5Map -} - -describe('deterministic build', () => { - const { next } = nextTestSetup({ - files: __dirname, - skipStart: true, - }) - - // Edge - { [route]: [file md5s] } - const edgeBuildFileMd5Hashes: Record[] = [] - // Node - { [route]: page.js or route.js md5 } - const nodeBuildFileMd5Hashes: Record[] = [{}, {}] - - beforeAll(async () => { - // First build - await next.build() - edgeBuildFileMd5Hashes.push(await getEdgeRouteFilesFromManifest(next)) - for (const file of nodeFilePaths) { - const content = await next.readFile(`.next/server/${file}.js`) - nodeBuildFileMd5Hashes[0][file] = generateMD5(content) - } - - // Second build - await next.build() - edgeBuildFileMd5Hashes.push(await getEdgeRouteFilesFromManifest(next)) - for (const file of nodeFilePaths) { - const content = await next.readFile(`.next/server/${file}.js`) - nodeBuildFileMd5Hashes[1][file] = generateMD5(content) - } - }) - - it('should have same md5 file across build', async () => { - expect(edgeBuildFileMd5Hashes[0]).toEqual(edgeBuildFileMd5Hashes[1]) - expect(nodeBuildFileMd5Hashes[0]).toEqual(nodeBuildFileMd5Hashes[1]) - }) -}) diff --git a/test/production/deterministic-build/pages/api/pages-api/edge/index.js b/test/production/deterministic-build/pages/api/pages-api/edge/index.js deleted file mode 100644 index 670361403e996..0000000000000 --- a/test/production/deterministic-build/pages/api/pages-api/edge/index.js +++ /dev/null @@ -1,5 +0,0 @@ -export default function handler() { - return new Response('pages-api (edge)') -} - -export const runtime = 'experimental-edge' diff --git a/test/production/deterministic-build/pages/api/pages-api/index.js b/test/production/deterministic-build/pages/api/pages-api/index.js deleted file mode 100644 index 49c3323d5c1a5..0000000000000 --- a/test/production/deterministic-build/pages/api/pages-api/index.js +++ /dev/null @@ -1,3 +0,0 @@ -export default function handler(req, res) { - res.send('pages-api (node)') -} diff --git a/test/production/deterministic-build/pages/pages-page/edge/index.js b/test/production/deterministic-build/pages/pages-page/edge/index.js deleted file mode 100644 index a227e8796424f..0000000000000 --- a/test/production/deterministic-build/pages/pages-page/edge/index.js +++ /dev/null @@ -1,5 +0,0 @@ -export default function Page() { - return 'pages-page (edge)' -} - -export const runtime = 'experimental-edge' diff --git a/test/production/deterministic-build/pages/pages-page/index.js b/test/production/deterministic-build/pages/pages-page/index.js deleted file mode 100644 index fcfcc0a629ec7..0000000000000 --- a/test/production/deterministic-build/pages/pages-page/index.js +++ /dev/null @@ -1,8 +0,0 @@ -export default function Page() { - return 'pages-page (node)' -} - -// Use gssp to opt-in dynamic rendering -export function getServerSideProps() { - return { props: {} } -} diff --git a/test/turbopack-build-tests-manifest.json b/test/turbopack-build-tests-manifest.json index 51b938f6fd981..861b17c3df266 100644 --- a/test/turbopack-build-tests-manifest.json +++ b/test/turbopack-build-tests-manifest.json @@ -14427,13 +14427,6 @@ "flakey": [], "runtimeError": false }, - "test/production/deterministic-build/index.test.ts": { - "passed": [], - "failed": ["deterministic build should have same md5 file across build"], - "pending": [], - "flakey": [], - "runtimeError": false - }, "test/production/app-dir-edge-runtime-with-wasm/index.test.ts": { "passed": ["app-dir edge runtime with wasm should have built"], "failed": [],