Unverified Commit 756d6dc9 authored by Niklas Mischkulnig's avatar Niklas Mischkulnig Committed by GitHub

Move immutable static assets config option out of experimental (#95351)

Keep reading `config.experimental.supportsImmutableAssets` for now to
not break existing adapters until they are migrated.

Closes NAR-868
parent 094dccb2
......@@ -174,6 +174,7 @@ pub struct NextConfig {
typescript: TypeScriptConfig,
use_file_system_public_routes: bool,
cache_components: Option<bool>,
supports_immutable_assets: Option<bool>,
adapter_path: Option<RcStr>,
//
......@@ -1291,7 +1292,6 @@ pub struct ExperimentalConfig {
use_cache: Option<bool>,
durable_use_cache_entries: Option<bool>,
runtime_server_deployment_id: Option<bool>,
supports_immutable_assets: Option<bool>,
expose_testing_api_in_production_build: Option<bool>,
/// A salt to mix into chunk and asset content hashes. Empty string means
......@@ -2283,10 +2283,7 @@ impl NextConfig {
/// Returns the suffix to use for chunk loading.
#[turbo_tasks::function]
pub fn asset_suffix_path(&self) -> Vc<Option<RcStr>> {
let needs_dpl_id = self
.experimental
.supports_immutable_assets
.is_none_or(|f| !f);
let needs_dpl_id = self.supports_immutable_assets.is_none_or(|f| !f);
Vc::cell(
needs_dpl_id
......@@ -2300,19 +2297,17 @@ impl NextConfig {
/// .next/immutable-static-hashes.json manifest.
#[turbo_tasks::function]
pub fn enable_immutable_assets(&self) -> Vc<bool> {
Vc::cell(self.experimental.supports_immutable_assets == Some(true))
Vc::cell(self.supports_immutable_assets == Some(true))
}
#[turbo_tasks::function]
pub fn client_static_folder_name(&self) -> Vc<RcStr> {
Vc::cell(
if self.experimental.supports_immutable_assets == Some(true) {
// Ends up as `_next/static/immutable`
rcstr!("static/immutable")
} else {
rcstr!("static")
},
)
Vc::cell(if self.supports_immutable_assets == Some(true) {
// Ends up as `_next/static/immutable`
rcstr!("static/immutable")
} else {
rcstr!("static")
})
}
#[turbo_tasks::function]
......@@ -2379,10 +2374,7 @@ impl NextConfig {
#[turbo_tasks::function]
pub fn should_append_server_deployment_id_at_runtime(&self) -> Vc<bool> {
let needs_dpl_id = self
.experimental
.supports_immutable_assets
.is_none_or(|f| !f);
let needs_dpl_id = self.supports_immutable_assets.is_none_or(|f| !f);
Vc::cell(
needs_dpl_id
......
......@@ -553,8 +553,7 @@ export async function handleBuildComplete({
const staticFiles = await recursiveReadDir(path.join(distDir, 'static'))
const clientHashes: Record<string, string> | undefined =
bundler === Bundler.Turbopack &&
config.experimental.supportsImmutableAssets
bundler === Bundler.Turbopack && config.supportsImmutableAssets
? JSON.parse(
await fs.readFile(
path.join(distDir, 'immutable-static-hashes.json'),
......
......@@ -192,7 +192,7 @@ export function getDefineEnv({
'process.env.__NEXT_USE_NODE_STREAMS': isEdgeServer ? false : true,
'process.env.NEXT_SUPPORTS_IMMUTABLE_ASSETS':
config.experimental.supportsImmutableAssets || false,
config.supportsImmutableAssets || false,
...(config.experimental?.useSkewCookie || !config.deploymentId
? {
......
......@@ -866,7 +866,7 @@ export function createStaticWorker(
: undefined),
// worker.ts copies this value into globalThis.NEXT_CLIENT_ASSET_SUFFIX
__NEXT_PRERENDER_CLIENT_ASSET_SUFFIX:
config.experimental.supportsImmutableAssets || !config.deploymentId
config.supportsImmutableAssets || !config.deploymentId
? ''
: `?dpl=${config.deploymentId}`,
},
......@@ -2146,7 +2146,7 @@ export default async function build(
cacheLifeProfiles: config.cacheLife,
buildId,
deploymentId: config.deploymentId,
clientAssetToken: config.experimental.supportsImmutableAssets
clientAssetToken: config.supportsImmutableAssets
? ''
: config.deploymentId,
sriEnabled,
......@@ -2379,8 +2379,7 @@ export default async function build(
cacheLifeProfiles: config.cacheLife,
buildId,
deploymentId: config.deploymentId,
clientAssetToken: config.experimental
.supportsImmutableAssets
clientAssetToken: config.supportsImmutableAssets
? ''
: config.deploymentId,
sriEnabled,
......
......@@ -715,7 +715,7 @@ async function exportAppImpl(
worker.exportPages({
buildId,
deploymentId: nextConfig.deploymentId,
clientAssetToken: nextConfig.experimental.supportsImmutableAssets
clientAssetToken: nextConfig.supportsImmutableAssets
? ''
: nextConfig.deploymentId,
exportPaths: batch,
......
......@@ -496,7 +496,7 @@ export default abstract class Server<
process.env.NEXT_DEPLOYMENT_ID = id
}
;(globalThis as any).NEXT_CLIENT_ASSET_SUFFIX =
this.nextConfig.experimental.supportsImmutableAssets || !this.deploymentId
this.nextConfig.supportsImmutableAssets || !this.deploymentId
? ''
: `?dpl=${this.deploymentId}`
......
......@@ -594,6 +594,7 @@ export const configSchema: zod.ZodType<NextConfig> = z.lazy(() =>
.union([z.literal('anonymous'), z.literal('use-credentials')])
.optional(),
deploymentId: z.string().optional(),
supportsImmutableAssets: z.boolean().optional(),
devIndicators: z
.union([
z.object({
......
......@@ -1364,9 +1364,7 @@ export interface ExperimentalConfig {
runtimeServerDeploymentId?: boolean
/**
* Whether the deployment environment supports immutable assets (assets deployed to
* `_next/static/immutable` don't need a `?dpl` parameter and can be safely requested across
* deployments.)
* @deprecated Use the top-level `supportsImmutableAssets` option instead.
*/
supportsImmutableAssets?: boolean
......@@ -1692,6 +1690,13 @@ export interface NextConfig {
*/
deploymentId?: string
/**
* Whether the deployment environment supports immutable assets (assets deployed to
* `_next/static/immutable` don't need a `?dpl` parameter and can be safely requested across
* deployments.)
*/
supportsImmutableAssets?: boolean
/**
* Deploy a Next.js application under a sub-path of a domain
*
......@@ -2235,6 +2240,7 @@ export async function normalizeConfig(phase: string, config: any) {
export interface NextConfigRuntime {
// Can be undefined, particularly when experimental.runtimeServerDeploymentId is true
deploymentId?: NextConfigComplete['deploymentId']
supportsImmutableAssets?: NextConfigComplete['supportsImmutableAssets']
configFileName?: string
// Should only be included when using isExperimentalCompile
......@@ -2313,7 +2319,6 @@ export interface NextConfigRuntime {
| 'maxPostponedStateSize'
| 'cachedNavigations'
| 'exposeTestingApiInProductionBuild'
| 'supportsImmutableAssets'
| 'instantInsights'
| 'requestInsights'
> & {
......@@ -2383,7 +2388,6 @@ export function getNextConfigRuntime(
maxPostponedStateSize: ex.maxPostponedStateSize,
cachedNavigations: ex.cachedNavigations,
exposeTestingApiInProductionBuild: ex.exposeTestingApiInProductionBuild,
supportsImmutableAssets: ex.supportsImmutableAssets,
instantInsights: ex.instantInsights,
requestInsights: ex.requestInsights,
......@@ -2395,6 +2399,7 @@ export function getNextConfigRuntime(
deploymentId: config.experimental.runtimeServerDeploymentId
? ''
: config.deploymentId,
supportsImmutableAssets: config.supportsImmutableAssets,
configFileName: undefined,
env: undefined,
......
......@@ -1634,7 +1634,10 @@ function assignDefaultsAndValidate(
* consumers don't each need to know the current framework default (which may
* evolve over time).
*/
function finalizeConfig(config: NextConfigComplete): NextConfigComplete {
function finalizeConfig(
config: NextConfigComplete,
bundler: Bundler | undefined
): NextConfigComplete {
config.experimental.instantInsights = {
validationLevel:
config.experimental.instantInsights?.validationLevel ?? 'warning',
......@@ -1648,18 +1651,34 @@ function finalizeConfig(config: NextConfigComplete): NextConfigComplete {
config.deploymentId &&
process.env.__NEXT_SUPPORTS_IMMUTABLE_ASSETS
) {
config.experimental.supportsImmutableAssets = true
config.supportsImmutableAssets = true
}
// Normalize again for Adapters that only set config.experimental.supportsImmutableAssets for now.
if (
config.experimental.supportsImmutableAssets &&
config.supportsImmutableAssets !==
config.experimental?.supportsImmutableAssets
) {
config.supportsImmutableAssets =
config.supportsImmutableAssets ??
config.experimental?.supportsImmutableAssets
}
if (bundler !== undefined && bundler !== Bundler.Turbopack) {
// Silently ignore that flag for Webpack/Rspack since the server code assumes that all files
// in `static/chunks` are always immutable without checking the manifest.
config.supportsImmutableAssets = false
}
if (
config.supportsImmutableAssets &&
(config.output === 'export' || config.output === 'standalone')
) {
// supportsImmutableAssets is designed to work with adapters. Disable it for output=export and
// output=standalone, which are currently using a non-adapter codepath.
// Particularly output=export should just run through the adapter, with only static assets.
// TODO remove again once output=export (and output=standalone) are using adapters.
config.experimental.supportsImmutableAssets = false
config.supportsImmutableAssets = false
}
return config
......@@ -1888,7 +1907,8 @@ async function loadConfigImpl(
phase,
silent,
dir
)
),
bundler
)
// Cache the custom config result
......@@ -2000,6 +2020,14 @@ async function loadConfigImpl(
// Check deprecation warnings on the actual user config before merging with defaults
checkDeprecations(userConfig, configFileName, silent, dir)
if (
userConfig.supportsImmutableAssets === undefined &&
userConfig.experimental?.supportsImmutableAssets !== undefined
) {
userConfig.supportsImmutableAssets =
userConfig.experimental.supportsImmutableAssets
}
// Always validate the config against schema in non minimal mode
if (!process.env.NEXT_MINIMAL && !silent) {
await validateConfigSchema(
......@@ -2023,16 +2051,6 @@ async function loadConfigImpl(
)
}
if (
userConfig.experimental?.supportsImmutableAssets &&
bundler !== undefined &&
bundler !== Bundler.Turbopack
) {
// Silently ignore that flag for Webpack/Rspack since the server code assumes that all files
// in `static/chunks` are always immutable without checking the manifest.
userConfig.experimental.supportsImmutableAssets = undefined
}
if (reactProductionProfiling) {
userConfig.reactProductionProfiling = reactProductionProfiling
}
......@@ -2092,9 +2110,23 @@ async function loadConfigImpl(
)
const finalConfig = finalizeConfig(
await applyModifyConfig(completeConfig, phase, silent, dir)
await applyModifyConfig(completeConfig, phase, silent, dir),
bundler
)
// Normalize so that also Adapters can still see userConfig.experimental.supportsImmutableAssets for now
if (
userConfig.supportsImmutableAssets === undefined ||
userConfig.experimental?.supportsImmutableAssets === undefined
) {
const value =
userConfig.supportsImmutableAssets ??
userConfig.experimental?.supportsImmutableAssets
userConfig.supportsImmutableAssets = value
userConfig.experimental ??= {}
userConfig.experimental.supportsImmutableAssets = value
}
// Cache the final result
configCache.set(cacheKey, {
config: finalConfig,
......@@ -2151,7 +2183,8 @@ async function loadConfigImpl(
setHttpClientAndAgentOptions(completeConfig)
const finalConfig = finalizeConfig(
await applyModifyConfig(completeConfig, phase, silent, dir)
await applyModifyConfig(completeConfig, phase, silent, dir),
bundler
)
// Cache the default config result
......
......@@ -522,7 +522,7 @@ export function getResolveRoutes(
config.deploymentId
) {
let isImmutableFile =
config.experimental.supportsImmutableAssets &&
config.supportsImmutableAssets &&
clientHashes![`static${decodeURI(output.itemPath)}`]
// Service workers are served at a fixed, stable URL (so the browser can keep the
// same registration across builds), so they don't carry a `?dpl` token.
......
......@@ -655,8 +655,7 @@ export default class NextNodeServer extends BaseServer<
{
buildId: this.buildId,
deploymentId: this.deploymentId,
clientAssetToken: this.nextConfig.experimental
.supportsImmutableAssets
clientAssetToken: this.nextConfig.supportsImmutableAssets
? ''
: this.deploymentId,
}
......@@ -674,8 +673,7 @@ export default class NextNodeServer extends BaseServer<
{
buildId: this.buildId,
deploymentId: this.deploymentId,
clientAssetToken: this.nextConfig.experimental
.supportsImmutableAssets
clientAssetToken: this.nextConfig.supportsImmutableAssets
? undefined
: this.deploymentId,
customServer: this.serverOptions.customServer || undefined,
......@@ -1770,7 +1768,7 @@ export default class NextNodeServer extends BaseServer<
request: requestData,
useCache: true,
onWarning: params.onWarning,
clientAssetToken: this.nextConfig.experimental.supportsImmutableAssets
clientAssetToken: this.nextConfig.supportsImmutableAssets
? ''
: this.deploymentId,
})
......@@ -2092,7 +2090,7 @@ export default class NextNodeServer extends BaseServer<
params.req,
'serverComponentsHmrCache'
),
clientAssetToken: this.nextConfig.experimental.supportsImmutableAssets
clientAssetToken: this.nextConfig.supportsImmutableAssets
? ''
: this.deploymentId,
})
......
......@@ -308,7 +308,7 @@ export type PagesSharedContext = {
deploymentId: string | undefined
/**
* NextConfig.deploymentId if experimental.supportsImmutableAssets is not true, otherwise undefined.
* NextConfig.deploymentId if supportsImmutableAssets is not true, otherwise undefined.
*/
clientAssetToken: string | undefined
......
......@@ -1094,9 +1094,7 @@ export abstract class RouteModule<
nextConfig satisfies DeepReadonly<NextConfigRuntime> as NextConfigRuntime,
routerServerContext,
deploymentId,
clientAssetToken: nextConfig.experimental.supportsImmutableAssets
? ''
: deploymentId,
clientAssetToken: nextConfig.supportsImmutableAssets ? '' : deploymentId,
}
}
......
......@@ -6,6 +6,7 @@ import { Span } from 'next/dist/trace'
import stripAnsi from 'strip-ansi'
import { quote as shellQuote } from 'shell-quote'
import { shouldUseTurbopack } from 'next-test-utils'
import { RequiredServerFilesManifest } from 'next/dist/build'
export class NextStartInstance extends NextInstance {
private _buildId: string
......@@ -171,12 +172,11 @@ export class NextStartInstance extends NextInstance {
),
'utf8'
)
)
) as RequiredServerFilesManifest
this._deploymentId =
requiredServerFiles.config?.deploymentId || undefined
this._supportsImmutableAssets =
requiredServerFiles.config?.experimental?.supportsImmutableAssets ||
false
requiredServerFiles.config?.supportsImmutableAssets || false
} catch {}
}
......@@ -330,8 +330,7 @@ export class NextStartInstance extends NextInstance {
)
this._deploymentId = requiredServerFiles.config?.deploymentId || undefined
this._supportsImmutableAssets =
requiredServerFiles.config?.experimental?.supportsImmutableAssets ||
false
requiredServerFiles.config?.supportsImmutableAssets || false
} catch {}
return result
......
......@@ -39,6 +39,7 @@ import escapeRegex from 'escape-string-regexp'
import './add-redbox-matchers'
import { NextInstance } from 'e2e-utils'
import { ClientReferenceManifest } from 'next/dist/build/webpack/plugins/flight-manifest-plugin'
import { RequiredServerFilesManifest } from 'next/dist/build'
export { shouldUseTurbopack }
......@@ -2124,7 +2125,7 @@ export const getCacheHeader = (curRes: Response) =>
curRes.headers.get('x-nextjs-cache') || curRes.headers.get('x-vercel-cache')
export function getDeploymentId(appDir: string, isDev: boolean) {
let requiredServerFiles
let requiredServerFiles: RequiredServerFilesManifest | undefined
if (!isDev) {
// File isn't written in dev, but it might still exist because it was created by a prior
// production build.
......@@ -2142,7 +2143,7 @@ export function getDeploymentId(appDir: string, isDev: boolean) {
requiredServerFiles?.config?.deploymentId
const assetToken: string | undefined = requiredServerFiles?.config
?.experimental?.supportsImmutableAssets
?.supportsImmutableAssets
? undefined
: deploymentId
......
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