Unverified Commit 4d5840e3 authored by Niklas Mischkulnig's avatar Niklas Mischkulnig Committed by GitHub

Turbopack: experimental.immutableAssetToken (#88607)

This allows specifying a different deployment id for static immutable assets

- [x] rename `getImmutableAssetToken` to `getAssetToken` (because it can be non-immutable as well)
- [x] disable for webpack

Closes PACK-6534

For https://github.com/nextjs/adapter-vercel/pull/25

Part of PACK-6802
parent aa4c30a5
...@@ -1171,12 +1171,6 @@ impl Project { ...@@ -1171,12 +1171,6 @@ impl Project {
Vc::cell(self.is_persistent_caching_enabled) Vc::cell(self.is_persistent_caching_enabled)
} }
#[turbo_tasks::function]
pub(super) fn emit_client_hashes(&self) -> Vc<bool> {
// TODO
Vc::cell(false)
}
#[turbo_tasks::function] #[turbo_tasks::function]
pub(super) fn next_mode(&self) -> Vc<NextMode> { pub(super) fn next_mode(&self) -> Vc<NextMode> {
*self.mode *self.mode
......
...@@ -183,7 +183,7 @@ impl Asset for AssetHashesManifestAsset { ...@@ -183,7 +183,7 @@ impl Asset for AssetHashesManifestAsset {
pub async fn immutable_hashes_manifest_asset_if_enabled( pub async fn immutable_hashes_manifest_asset_if_enabled(
project: ResolvedVc<Project>, project: ResolvedVc<Project>,
) -> Result<Vc<OutputAssets>> { ) -> Result<Vc<OutputAssets>> {
if *project.emit_client_hashes().await? { if *project.next_config().enable_immutable_assets().await? {
let path = project let path = project
.node_root() .node_root()
.await? .await?
......
...@@ -1090,6 +1090,7 @@ pub struct ExperimentalConfig { ...@@ -1090,6 +1090,7 @@ pub struct ExperimentalConfig {
use_cache: Option<bool>, use_cache: Option<bool>,
root_params: Option<bool>, root_params: Option<bool>,
runtime_server_deployment_id: Option<bool>, runtime_server_deployment_id: Option<bool>,
immutable_asset_token: Option<RcStr>,
// --- // ---
// UNSUPPORTED // UNSUPPORTED
...@@ -1116,7 +1117,7 @@ pub struct ExperimentalConfig { ...@@ -1116,7 +1117,7 @@ pub struct ExperimentalConfig {
fully_specified: Option<bool>, fully_specified: Option<bool>,
gzip_size: Option<bool>, gzip_size: Option<bool>,
pub inline_css: Option<bool>, inline_css: Option<bool>,
instrumentation_hook: Option<bool>, instrumentation_hook: Option<bool>,
client_trace_metadata: Option<Vec<String>>, client_trace_metadata: Option<Vec<String>>,
large_page_data_bytes: Option<f64>, large_page_data_bytes: Option<f64>,
...@@ -1889,13 +1890,21 @@ impl NextConfig { ...@@ -1889,13 +1890,21 @@ impl NextConfig {
/// Returns the suffix to use for chunk loading. /// Returns the suffix to use for chunk loading.
#[turbo_tasks::function] #[turbo_tasks::function]
pub async fn asset_suffix_path(self: Vc<Self>) -> Result<Vc<Option<RcStr>>> { pub fn asset_suffix_path(&self) -> Vc<Option<RcStr>> {
let this = self.await?; let id = self
.experimental
.immutable_asset_token
.as_ref()
.or(self.deployment_id.as_ref());
match &this.deployment_id { Vc::cell(id.as_ref().map(|id| format!("?dpl={id}").into()))
Some(deployment_id) => Ok(Vc::cell(Some(format!("?dpl={deployment_id}").into()))), }
None => Ok(Vc::cell(None)),
} /// Whether to enable immutable assets, which uses a different asset suffix, and writes a
/// .next/immutable-static-hashes.json manifest.
#[turbo_tasks::function]
pub fn enable_immutable_assets(&self) -> Vc<bool> {
Vc::cell(self.experimental.immutable_asset_token.is_some())
} }
#[turbo_tasks::function] #[turbo_tasks::function]
......
...@@ -176,6 +176,9 @@ export function getDefineEnv({ ...@@ -176,6 +176,9 @@ export function getDefineEnv({
!!config.experimental.instantNavigationDevToolsToggle, !!config.experimental.instantNavigationDevToolsToggle,
'process.env.__NEXT_USE_CACHE': isUseCacheEnabled, 'process.env.__NEXT_USE_CACHE': isUseCacheEnabled,
'process.env.NEXT_IMMUTABLE_ASSET_TOKEN':
config.experimental.immutableAssetToken || '',
...(config.experimental?.useSkewCookie || !config.deploymentId ...(config.experimental?.useSkewCookie || !config.deploymentId
? { ? {
'process.env.NEXT_DEPLOYMENT_ID': false, 'process.env.NEXT_DEPLOYMENT_ID': false,
......
...@@ -854,9 +854,10 @@ export function createStaticWorker( ...@@ -854,9 +854,10 @@ export function createStaticWorker(
} }
: undefined), : undefined),
// worker.ts copies this value into globalThis.NEXT_CLIENT_ASSET_SUFFIX // worker.ts copies this value into globalThis.NEXT_CLIENT_ASSET_SUFFIX
__NEXT_PRERENDER_CLIENT_ASSET_SUFFIX: config.deploymentId __NEXT_PRERENDER_CLIENT_ASSET_SUFFIX:
? `?dpl=${config.deploymentId}` config.experimental.immutableAssetToken || config.deploymentId
: '', ? `?dpl=${config.experimental.immutableAssetToken || config.deploymentId}`
: '',
}, },
}, },
}) as StaticWorker }) as StaticWorker
...@@ -2060,7 +2061,8 @@ export default async function build( ...@@ -2060,7 +2061,8 @@ export default async function build(
pprConfig: config.experimental.ppr, pprConfig: config.experimental.ppr,
cacheLifeProfiles: config.cacheLife, cacheLifeProfiles: config.cacheLife,
buildId, buildId,
deploymentId: config.deploymentId, clientAssetToken:
config.experimental.immutableAssetToken || config.deploymentId,
sriEnabled, sriEnabled,
cacheMaxMemorySize: config.cacheMaxMemorySize, cacheMaxMemorySize: config.cacheMaxMemorySize,
}) })
...@@ -2286,7 +2288,9 @@ export default async function build( ...@@ -2286,7 +2288,9 @@ export default async function build(
pprConfig: config.experimental.ppr, pprConfig: config.experimental.ppr,
cacheLifeProfiles: config.cacheLife, cacheLifeProfiles: config.cacheLife,
buildId, buildId,
deploymentId: config.deploymentId, clientAssetToken:
config.experimental.immutableAssetToken ||
config.deploymentId,
sriEnabled, sriEnabled,
}) })
} }
......
...@@ -184,6 +184,7 @@ export async function handler( ...@@ -184,6 +184,7 @@ export async function handler(
parsedUrl, parsedUrl,
interceptionRoutePatterns, interceptionRoutePatterns,
deploymentId, deploymentId,
clientAssetToken,
} = prepareResult } = prepareResult
const normalizedSrcPage = normalizeAppPath(srcPage) const normalizedSrcPage = normalizeAppPath(srcPage)
...@@ -643,6 +644,7 @@ export async function handler( ...@@ -643,6 +644,7 @@ export async function handler(
sharedContext: { sharedContext: {
buildId, buildId,
deploymentId, deploymentId,
clientAssetToken,
}, },
serverComponentsHmrCache: getRequestMeta( serverComponentsHmrCache: getRequestMeta(
req, req,
......
...@@ -85,6 +85,7 @@ async function requestHandler( ...@@ -85,6 +85,7 @@ async function requestHandler(
interceptionRoutePatterns, interceptionRoutePatterns,
routerServerContext, routerServerContext,
deploymentId, deploymentId,
clientAssetToken,
} = prepareResult } = prepareResult
// Initialize the cache handlers interface. // Initialize the cache handlers interface.
...@@ -107,6 +108,7 @@ async function requestHandler( ...@@ -107,6 +108,7 @@ async function requestHandler(
sharedContext: { sharedContext: {
buildId, buildId,
deploymentId, deploymentId,
clientAssetToken,
}, },
fallbackRouteParams: null, fallbackRouteParams: null,
......
...@@ -117,6 +117,7 @@ async function requestHandler( ...@@ -117,6 +117,7 @@ async function requestHandler(
reactLoadableManifest, reactLoadableManifest,
subresourceIntegrityManifest, subresourceIntegrityManifest,
dynamicCssManifest, dynamicCssManifest,
clientAssetToken,
} = prepareResult } = prepareResult
initializeCacheHandlers(nextConfig.cacheMaxMemorySize) initializeCacheHandlers(nextConfig.cacheMaxMemorySize)
...@@ -129,6 +130,7 @@ async function requestHandler( ...@@ -129,6 +130,7 @@ async function requestHandler(
sharedContext: { sharedContext: {
buildId, buildId,
deploymentId, deploymentId,
clientAssetToken,
customServer: undefined, customServer: undefined,
}, },
......
...@@ -757,7 +757,7 @@ export async function isPageStatic({ ...@@ -757,7 +757,7 @@ export async function isPageStatic({
cacheLifeProfiles, cacheLifeProfiles,
pprConfig, pprConfig,
buildId, buildId,
deploymentId, clientAssetToken,
sriEnabled, sriEnabled,
}: { }: {
dir: string dir: string
...@@ -784,7 +784,7 @@ export async function isPageStatic({ ...@@ -784,7 +784,7 @@ export async function isPageStatic({
nextConfigOutput: 'standalone' | 'export' | undefined nextConfigOutput: 'standalone' | 'export' | undefined
pprConfig: ExperimentalPPRConfig | undefined pprConfig: ExperimentalPPRConfig | undefined
buildId: string buildId: string
deploymentId: string clientAssetToken: string
sriEnabled: boolean sriEnabled: boolean
}): Promise<PageIsStaticResult> { }): Promise<PageIsStaticResult> {
// Skip page data collection for synthetic _global-error routes // Skip page data collection for synthetic _global-error routes
...@@ -838,7 +838,7 @@ export async function isPageStatic({ ...@@ -838,7 +838,7 @@ export async function isPageStatic({
name: edgeInfo.name, name: edgeInfo.name,
useCache: true, useCache: true,
distDir, distDir,
deploymentId, clientAssetToken,
}) })
const mod = ( const mod = (
await runtime.context._ENTRIES[`middleware_${edgeInfo.name}`] await runtime.context._ENTRIES[`middleware_${edgeInfo.name}`]
......
...@@ -19,7 +19,7 @@ import { getProxiedPluginState } from '../../build-context' ...@@ -19,7 +19,7 @@ import { getProxiedPluginState } from '../../build-context'
import { WEBPACK_LAYERS } from '../../../lib/constants' import { WEBPACK_LAYERS } from '../../../lib/constants'
import { normalizePagePath } from '../../../shared/lib/page-path/normalize-page-path' import { normalizePagePath } from '../../../shared/lib/page-path/normalize-page-path'
import { CLIENT_STATIC_FILES_RUNTIME_MAIN_APP } from '../../../shared/lib/constants' import { CLIENT_STATIC_FILES_RUNTIME_MAIN_APP } from '../../../shared/lib/constants'
import { getDeploymentIdQueryOrEmptyString } from '../../../shared/lib/deployment-id' import { getAssetTokenQuery } from '../../../shared/lib/deployment-id'
import { import {
formatBarrelOptimizedResource, formatBarrelOptimizedResource,
getModuleReferencesInOrder, getModuleReferencesInOrder,
...@@ -119,7 +119,7 @@ function getAppPathRequiredChunks( ...@@ -119,7 +119,7 @@ function getAppPathRequiredChunks(
chunkGroup: webpack.ChunkGroup, chunkGroup: webpack.ChunkGroup,
excludedFiles: Set<string> excludedFiles: Set<string>
) { ) {
const deploymentIdChunkQuery = getDeploymentIdQueryOrEmptyString() const assetTokenQuery = getAssetTokenQuery()
const chunks: Array<string> = [] const chunks: Array<string> = []
chunkGroup.chunks.forEach((chunk) => { chunkGroup.chunks.forEach((chunk) => {
...@@ -142,10 +142,7 @@ function getAppPathRequiredChunks( ...@@ -142,10 +142,7 @@ function getAppPathRequiredChunks(
// previously done for dynamic chunks by patching the webpack runtime but we want // previously done for dynamic chunks by patching the webpack runtime but we want
// these filenames to be managed by React's Flight runtime instead and so we need // these filenames to be managed by React's Flight runtime instead and so we need
// to implement any special handling of the file name here. // to implement any special handling of the file name here.
return chunks.push( return chunks.push(chunkId, encodeURIPath(file) + assetTokenQuery)
chunkId,
encodeURIPath(file) + deploymentIdChunkQuery
)
}) })
} }
}) })
......
...@@ -2,18 +2,15 @@ ...@@ -2,18 +2,15 @@
// https://github.com/webpack/webpack/blob/2738eebc7880835d88c727d364ad37f3ec557593/lib/RuntimeGlobals.js#L204 // https://github.com/webpack/webpack/blob/2738eebc7880835d88c727d364ad37f3ec557593/lib/RuntimeGlobals.js#L204
import './register-deployment-id-global' import './register-deployment-id-global'
import { import { getAssetToken, getAssetTokenQuery } from '../shared/lib/deployment-id'
getDeploymentId,
getDeploymentIdQueryOrEmptyString,
} from '../shared/lib/deployment-id'
import { encodeURIPath } from '../shared/lib/encode-uri-path' import { encodeURIPath } from '../shared/lib/encode-uri-path'
declare const __webpack_require__: any declare const __webpack_require__: any
// If we have a deployment ID, we need to append it to the webpack chunk names // If we have a deployment ID, we need to append it to the webpack chunk names
// I am keeping the process check explicit so this can be statically optimized // I am keeping the process check explicit so this can be statically optimized
if (getDeploymentId()) { if (getAssetToken()) {
const suffix = getDeploymentIdQueryOrEmptyString() const suffix = getAssetTokenQuery()
const getChunkScriptFilename = __webpack_require__.u const getChunkScriptFilename = __webpack_require__.u
__webpack_require__.u = (...args: any[]) => __webpack_require__.u = (...args: any[]) =>
// We encode the chunk filename because our static server matches against and encoded // We encode the chunk filename because our static server matches against and encoded
......
...@@ -50,7 +50,7 @@ import RootErrorBoundary from './errors/root-error-boundary' ...@@ -50,7 +50,7 @@ import RootErrorBoundary from './errors/root-error-boundary'
import DefaultGlobalError from './builtin/global-error' import DefaultGlobalError from './builtin/global-error'
import { RootLayoutBoundary } from '../../lib/framework/boundary-components' import { RootLayoutBoundary } from '../../lib/framework/boundary-components'
import type { StaticIndicatorState } from '../dev/hot-reloader/app/hot-reloader-app' import type { StaticIndicatorState } from '../dev/hot-reloader/app/hot-reloader-app'
import { getDeploymentIdQueryOrEmptyString } from '../../shared/lib/deployment-id' import { getAssetTokenQuery } from '../../shared/lib/deployment-id'
const globalMutable: { const globalMutable: {
pendingMpaPath?: string pendingMpaPath?: string
...@@ -633,12 +633,12 @@ function RuntimeStylesForWebpack() { ...@@ -633,12 +633,12 @@ function RuntimeStylesForWebpack() {
} }
}, [renderedStylesSize, forceUpdate]) }, [renderedStylesSize, forceUpdate])
const dplId = getDeploymentIdQueryOrEmptyString() const query = getAssetTokenQuery()
return [...(runtimeStyles || [])].map((href, i) => ( return [...(runtimeStyles || [])].map((href, i) => (
<link <link
key={i} key={i}
rel="stylesheet" rel="stylesheet"
href={`${href}${dplId}`} href={`${href}${query}`}
// @ts-ignore // @ts-ignore
precedence="next" precedence="next"
// TODO figure out crossOrigin and nonce // TODO figure out crossOrigin and nonce
......
...@@ -4,7 +4,7 @@ import type { RequiredServerFilesManifest } from '../build' ...@@ -4,7 +4,7 @@ import type { RequiredServerFilesManifest } from '../build'
import getAssetPathFromRoute from '../shared/lib/router/utils/get-asset-path-from-route' import getAssetPathFromRoute from '../shared/lib/router/utils/get-asset-path-from-route'
import { __unsafeCreateTrustedScriptURL } from './trusted-types' import { __unsafeCreateTrustedScriptURL } from './trusted-types'
import { requestIdleCallback } from './request-idle-callback' import { requestIdleCallback } from './request-idle-callback'
import { getDeploymentIdQueryOrEmptyString } from '../shared/lib/deployment-id' import { getAssetTokenQuery } from '../shared/lib/deployment-id'
import { encodeURIPath } from '../shared/lib/encode-uri-path' import { encodeURIPath } from '../shared/lib/encode-uri-path'
import { resolvePromiseWithTimeout } from './lib/promise' import { resolvePromiseWithTimeout } from './lib/promise'
...@@ -114,10 +114,6 @@ function hasPrefetch(link?: HTMLLinkElement): boolean { ...@@ -114,10 +114,6 @@ function hasPrefetch(link?: HTMLLinkElement): boolean {
const canPrefetch: boolean = hasPrefetch() const canPrefetch: boolean = hasPrefetch()
const getAssetQueryString = () => {
return getDeploymentIdQueryOrEmptyString()
}
function prefetchViaDom( function prefetchViaDom(
href: string, href: string,
as: string, as: string,
...@@ -218,7 +214,7 @@ function getFilesForRoute( ...@@ -218,7 +214,7 @@ function getFilesForRoute(
assetPrefix + assetPrefix +
'/_next/static/chunks/pages' + '/_next/static/chunks/pages' +
encodeURIPath(getAssetPathFromRoute(route, '.js')) + encodeURIPath(getAssetPathFromRoute(route, '.js')) +
getAssetQueryString() getAssetTokenQuery()
return Promise.resolve({ return Promise.resolve({
scripts: [__unsafeCreateTrustedScriptURL(scriptUrl)], scripts: [__unsafeCreateTrustedScriptURL(scriptUrl)],
// Styles are handled by `style-loader` in development: // Styles are handled by `style-loader` in development:
...@@ -235,10 +231,10 @@ function getFilesForRoute( ...@@ -235,10 +231,10 @@ function getFilesForRoute(
return { return {
scripts: allFiles scripts: allFiles
.filter((v) => v.endsWith('.js')) .filter((v) => v.endsWith('.js'))
.map((v) => __unsafeCreateTrustedScriptURL(v) + getAssetQueryString()), .map((v) => __unsafeCreateTrustedScriptURL(v) + getAssetTokenQuery()),
css: allFiles css: allFiles
.filter((v) => v.endsWith('.css')) .filter((v) => v.endsWith('.css'))
.map((v) => v + getAssetQueryString()), .map((v) => v + getAssetTokenQuery()),
} }
}) })
} }
......
declare const __webpack_require__: any declare const __webpack_require__: any
declare let __webpack_public_path__: string declare let __webpack_public_path__: string
import { import { getAssetToken, getAssetTokenQuery } from '../shared/lib/deployment-id'
getDeploymentId,
getDeploymentIdQueryOrEmptyString,
} from '../shared/lib/deployment-id'
// If we have a deployment ID, we need to append it to the webpack chunk names // If we have a deployment ID query string, we need to append it to the webpack chunk names
// I am keeping the process check explicit so this can be statically optimized // I am keeping the process check explicit so this can be statically optimized
if (getDeploymentId()) { if (getAssetToken()) {
const suffix = getDeploymentIdQueryOrEmptyString() const suffix = getAssetTokenQuery()
const getChunkScriptFilename = __webpack_require__.u const getChunkScriptFilename = __webpack_require__.u
__webpack_require__.u = (...args: any[]) => __webpack_require__.u = (...args: any[]) =>
// We enode the chunk filename because our static server matches against and encoded // We enode the chunk filename because our static server matches against and encoded
......
...@@ -704,6 +704,9 @@ async function exportAppImpl( ...@@ -704,6 +704,9 @@ async function exportAppImpl(
worker.exportPages({ worker.exportPages({
buildId, buildId,
deploymentId: nextConfig.deploymentId, deploymentId: nextConfig.deploymentId,
clientAssetToken:
nextConfig.experimental.immutableAssetToken ||
nextConfig.deploymentId,
exportPaths: batch, exportPaths: batch,
parentSpanId: span.getId(), parentSpanId: span.getId(),
pagesDataDir, pagesDataDir,
......
...@@ -24,6 +24,7 @@ export type ExportPathEntry = ExportPathMap[keyof ExportPathMap] & { ...@@ -24,6 +24,7 @@ export type ExportPathEntry = ExportPathMap[keyof ExportPathMap] & {
export interface ExportPagesInput { export interface ExportPagesInput {
buildId: string buildId: string
deploymentId: string deploymentId: string
clientAssetToken: string
exportPaths: ExportPathEntry[] exportPaths: ExportPathEntry[]
parentSpanId: number parentSpanId: number
dir: string dir: string
...@@ -43,6 +44,7 @@ export interface ExportPagesInput { ...@@ -43,6 +44,7 @@ export interface ExportPagesInput {
export interface ExportPageInput { export interface ExportPageInput {
buildId: string buildId: string
deploymentId: string deploymentId: string
clientAssetToken: string
exportPath: ExportPathEntry exportPath: ExportPathEntry
distDir: string distDir: string
outDir: string outDir: string
......
...@@ -86,6 +86,7 @@ async function exportPageImpl( ...@@ -86,6 +86,7 @@ async function exportPageImpl(
outDir: commonOutDir, outDir: commonOutDir,
buildId, buildId,
deploymentId, deploymentId,
clientAssetToken,
renderResumeDataCache, renderResumeDataCache,
} = input } = input
...@@ -275,7 +276,11 @@ async function exportPageImpl( ...@@ -275,7 +276,11 @@ async function exportPageImpl(
// Handle App Pages // Handle App Pages
if (isAppDir) { if (isAppDir) {
const sharedContext: AppSharedContext = { buildId, deploymentId } const sharedContext: AppSharedContext = {
buildId,
deploymentId,
clientAssetToken,
}
return exportAppPage( return exportAppPage(
req, req,
...@@ -296,6 +301,7 @@ async function exportPageImpl( ...@@ -296,6 +301,7 @@ async function exportPageImpl(
const sharedContext: PagesSharedContext = { const sharedContext: PagesSharedContext = {
buildId, buildId,
deploymentId, deploymentId,
clientAssetToken,
customServer: undefined, customServer: undefined,
} }
...@@ -418,6 +424,7 @@ export async function exportPages( ...@@ -418,6 +424,7 @@ export async function exportPages(
sriEnabled: Boolean(nextConfig.experimental.sri?.algorithm), sriEnabled: Boolean(nextConfig.experimental.sri?.algorithm),
buildId: input.buildId, buildId: input.buildId,
deploymentId: input.deploymentId, deploymentId: input.deploymentId,
clientAssetToken: input.clientAssetToken,
renderResumeDataCache, renderResumeDataCache,
}), }),
hasDebuggerAttached hasDebuggerAttached
......
...@@ -138,6 +138,7 @@ function getScripts( ...@@ -138,6 +138,7 @@ function getScripts(
buildManifest, buildManifest,
isDevelopment, isDevelopment,
assetQueryString, assetQueryString,
mutableAssetQueryString,
disableOptimizedLoading, disableOptimizedLoading,
crossOrigin, crossOrigin,
} = context } = context
...@@ -148,10 +149,15 @@ function getScripts( ...@@ -148,10 +149,15 @@ function getScripts(
) )
return [...normalScripts, ...lowPriorityScripts].map((file) => { return [...normalScripts, ...lowPriorityScripts].map((file) => {
// static/chunks/51e975e7b637a580.js should use the immutable id, while
// static/Yj152X97rfGgF7NPcJEZs/_ssgManifest.js should use the deployment id
const query = file.startsWith('static/chunks')
? assetQueryString
: mutableAssetQueryString
return ( return (
<script <script
key={file} key={file}
src={`${assetPrefix}/_next/${encodeURIPath(file)}${assetQueryString}`} src={`${assetPrefix}/_next/${encodeURIPath(file)}${query}`}
nonce={props.nonce} nonce={props.nonce}
async={!isDevelopment && disableOptimizedLoading} async={!isDevelopment && disableOptimizedLoading}
defer={!disableOptimizedLoading} defer={!disableOptimizedLoading}
......
...@@ -267,6 +267,7 @@ export type GenerateFlight = typeof generateDynamicFlightRenderResult ...@@ -267,6 +267,7 @@ export type GenerateFlight = typeof generateDynamicFlightRenderResult
export type AppSharedContext = { export type AppSharedContext = {
buildId: string buildId: string
deploymentId: string deploymentId: string
clientAssetToken: string
} }
export type AppRenderContext = { export type AppRenderContext = {
......
...@@ -18,8 +18,8 @@ export function getAssetQueryString( ...@@ -18,8 +18,8 @@ export function getAssetQueryString(
qs += `?v=${ctx.requestTimestamp}` qs += `?v=${ctx.requestTimestamp}`
} }
if (ctx.sharedContext.deploymentId) { if (ctx.sharedContext.clientAssetToken) {
qs += `${shouldAddVersion ? '&' : '?'}dpl=${ctx.sharedContext.deploymentId}` qs += `${shouldAddVersion ? '&' : '?'}dpl=${ctx.sharedContext.clientAssetToken}`
} }
return qs return qs
} }
...@@ -467,9 +467,6 @@ export default abstract class Server< ...@@ -467,9 +467,6 @@ export default abstract class Server<
) )
} }
this.deploymentId = process.env.NEXT_DEPLOYMENT_ID this.deploymentId = process.env.NEXT_DEPLOYMENT_ID
;(globalThis as any).NEXT_CLIENT_ASSET_SUFFIX = this.deploymentId
? `?dpl=${this.deploymentId}`
: ''
} else { } else {
let id = this.nextConfig.experimental.useSkewCookie let id = this.nextConfig.experimental.useSkewCookie
? '' ? ''
...@@ -477,8 +474,11 @@ export default abstract class Server< ...@@ -477,8 +474,11 @@ export default abstract class Server<
this.deploymentId = id this.deploymentId = id
process.env.NEXT_DEPLOYMENT_ID = id process.env.NEXT_DEPLOYMENT_ID = id
;(globalThis as any).NEXT_CLIENT_ASSET_SUFFIX = id ? `?dpl=${id}` : ''
} }
;(globalThis as any).NEXT_CLIENT_ASSET_SUFFIX =
this.nextConfig.experimental.immutableAssetToken || this.deploymentId
? `?dpl=${this.nextConfig.experimental.immutableAssetToken || this.deploymentId}`
: ''
this.hostname = hostname this.hostname = hostname
if (this.hostname) { if (this.hostname) {
......
...@@ -980,6 +980,18 @@ function assignDefaultsAndValidate( ...@@ -980,6 +980,18 @@ function assignDefaultsAndValidate(
result.deploymentId = process.env.NEXT_DEPLOYMENT_ID result.deploymentId = process.env.NEXT_DEPLOYMENT_ID
} }
// Only read process.env.__NEXT_IMMUTABLE_ASSET_TOKEN to make our testing setup easier. This is
// actually done by the adapter's modifyConfig
if (
process.env.__NEXT_TEST_MODE &&
process.env.IS_TURBOPACK_TEST &&
result.deploymentId &&
process.env.__NEXT_IMMUTABLE_ASSET_TOKEN
) {
result.experimental.immutableAssetToken =
process.env.__NEXT_IMMUTABLE_ASSET_TOKEN
}
const tracingRoot = result?.outputFileTracingRoot const tracingRoot = result?.outputFileTracingRoot
const turbopackRoot = result?.turbopack?.root const turbopackRoot = result?.turbopack?.root
...@@ -1730,8 +1742,8 @@ export default async function loadConfig( ...@@ -1730,8 +1742,8 @@ export default async function loadConfig(
} }
if ( if (
// TODO enable once everything is merged phase === PHASE_PRODUCTION_BUILD &&
(true || bundler !== Bundler.Turbopack) && bundler !== Bundler.Turbopack &&
userConfig.experimental?.immutableAssetToken userConfig.experimental?.immutableAssetToken
) { ) {
// Silently ignore that flag for Webpack/Rspack since the server code assumes that all files // Silently ignore that flag for Webpack/Rspack since the server code assumes that all files
......
...@@ -57,6 +57,18 @@ export function getResolveRoutes( ...@@ -57,6 +57,18 @@ export function getResolveRoutes(
renderServerOpts: Parameters<RenderServer['initialize']>[0], renderServerOpts: Parameters<RenderServer['initialize']>[0],
ensureMiddleware?: (url?: string) => Promise<void> ensureMiddleware?: (url?: string) => Promise<void>
) { ) {
let clientHashes: Record<string, string> | undefined = undefined
if (process.env.__NEXT_TEST_MODE && process.env.IS_TURBOPACK_TEST) {
try {
clientHashes = JSON.parse(
(require('fs') as typeof import('fs')).readFileSync(
path.join(opts.dir, config.distDir, 'immutable-static-hashes.json'),
'utf8'
)
)
} catch {}
}
type Route = { type Route = {
/** /**
* The path matcher to check if this route applies to this request. * The path matcher to check if this route applies to this request.
...@@ -481,7 +493,13 @@ export function getResolveRoutes( ...@@ -481,7 +493,13 @@ export function getResolveRoutes(
output.type === 'nextStaticFolder' && output.type === 'nextStaticFolder' &&
config.deploymentId config.deploymentId
) { ) {
const expectedToken = config.deploymentId let useImmutableToken =
config.experimental.immutableAssetToken &&
clientHashes![`static${decodeURI(output.itemPath)}`]
const expectedToken = useImmutableToken
? config.experimental.immutableAssetToken
: config.deploymentId
if (parsedUrl.query.dpl !== expectedToken) { if (parsedUrl.query.dpl !== expectedToken) {
console.error( console.error(
`Invalid dpl query param: ${req.url}, expected: ${expectedToken}` `Invalid dpl query param: ${req.url}, expected: ${expectedToken}`
......
...@@ -274,6 +274,8 @@ export default class NextNodeServer extends BaseServer< ...@@ -274,6 +274,8 @@ export default class NextNodeServer extends BaseServer<
// when using compile mode static env isn't inlined so we // when using compile mode static env isn't inlined so we
// need to populate in normal runtime env // need to populate in normal runtime env
if (this.renderOpts.isExperimentalCompile) { if (this.renderOpts.isExperimentalCompile) {
// immutableAssetToken only works with Turbopack, and `isExperimentalCompile` isn't supported
// with that anyway, so we can assign immutableAssetToken to deploymentId here
populateStaticEnv(this.nextConfig, this.deploymentId || '') populateStaticEnv(this.nextConfig, this.deploymentId || '')
} }
...@@ -634,6 +636,9 @@ export default class NextNodeServer extends BaseServer< ...@@ -634,6 +636,9 @@ export default class NextNodeServer extends BaseServer<
{ {
buildId: this.buildId, buildId: this.buildId,
deploymentId: this.deploymentId, deploymentId: this.deploymentId,
clientAssetToken:
this.nextConfig.experimental.immutableAssetToken ??
this.deploymentId,
} }
) )
} else { } else {
...@@ -649,6 +654,9 @@ export default class NextNodeServer extends BaseServer< ...@@ -649,6 +654,9 @@ export default class NextNodeServer extends BaseServer<
{ {
buildId: this.buildId, buildId: this.buildId,
deploymentId: this.deploymentId, deploymentId: this.deploymentId,
clientAssetToken:
this.nextConfig.experimental.immutableAssetToken ??
this.deploymentId,
customServer: this.serverOptions.customServer || undefined, customServer: this.serverOptions.customServer || undefined,
}, },
{ {
...@@ -1734,7 +1742,8 @@ export default class NextNodeServer extends BaseServer< ...@@ -1734,7 +1742,8 @@ export default class NextNodeServer extends BaseServer<
request: requestData, request: requestData,
useCache: true, useCache: true,
onWarning: params.onWarning, onWarning: params.onWarning,
deploymentId: this.deploymentId, clientAssetToken:
this.nextConfig.experimental.immutableAssetToken || this.deploymentId,
}) })
} }
...@@ -2032,7 +2041,8 @@ export default class NextNodeServer extends BaseServer< ...@@ -2032,7 +2041,8 @@ export default class NextNodeServer extends BaseServer<
params.req, params.req,
'serverComponentsHmrCache' 'serverComponentsHmrCache'
), ),
deploymentId: this.deploymentId, clientAssetToken:
this.nextConfig.experimental.immutableAssetToken || this.deploymentId,
}) })
if (result.fetchMetrics) { if (result.fetchMetrics) {
......
...@@ -60,6 +60,7 @@ export type PagesRenderResultMetadata = { ...@@ -60,6 +60,7 @@ export type PagesRenderResultMetadata = {
pageData?: any pageData?: any
cacheControl?: CacheControl cacheControl?: CacheControl
assetQueryString?: string assetQueryString?: string
mutableAssetQueryString?: string
isNotFound?: boolean isNotFound?: boolean
isRedirect?: boolean isRedirect?: boolean
} }
......
...@@ -303,9 +303,14 @@ export type PagesSharedContext = { ...@@ -303,9 +303,14 @@ export type PagesSharedContext = {
buildId: string buildId: string
/** /**
* The deployment ID if the user is deploying to a platform that provides one. * See NextConfig.deploymentId
*/ */
deploymentId: string deploymentId: string | undefined
/**
* Either NextConfig.experimental.immutableAssetToken or NextConfig.deploymentId
*/
clientAssetToken: string | undefined
/** /**
* True if the user is using a custom server. * True if the user is using a custom server.
...@@ -453,12 +458,10 @@ export async function renderToHTMLImpl( ...@@ -453,12 +458,10 @@ export async function renderToHTMLImpl(
// Adds support for reading `cookies` in `getServerSideProps` when SSR. // Adds support for reading `cookies` in `getServerSideProps` when SSR.
setLazyProp({ req: req as any }, 'cookies', getCookieParser(req.headers)) setLazyProp({ req: req as any }, 'cookies', getCookieParser(req.headers))
const metadata: PagesRenderResultMetadata = {} let baseAssetQueryString =
metadata.assetQueryString =
(process.env.__NEXT_DEV_SERVER && renderOpts.assetQueryString) || '' (process.env.__NEXT_DEV_SERVER && renderOpts.assetQueryString) || ''
if (process.env.__NEXT_DEV_SERVER && !metadata.assetQueryString) { if (process.env.__NEXT_DEV_SERVER && !baseAssetQueryString) {
const userAgent = (req.headers['user-agent'] || '').toLowerCase() const userAgent = (req.headers['user-agent'] || '').toLowerCase()
if (userAgent.includes('safari') && !userAgent.includes('chrome')) { if (userAgent.includes('safari') && !userAgent.includes('chrome')) {
// In dev we invalidate the cache by appending a timestamp to the resource URL. // In dev we invalidate the cache by appending a timestamp to the resource URL.
...@@ -466,15 +469,23 @@ export async function renderToHTMLImpl( ...@@ -466,15 +469,23 @@ export async function renderToHTMLImpl(
// TODO: remove this workaround when https://bugs.webkit.org/show_bug.cgi?id=187726 is fixed. // TODO: remove this workaround when https://bugs.webkit.org/show_bug.cgi?id=187726 is fixed.
// Note: The workaround breaks breakpoints on reload since the script url always changes, // Note: The workaround breaks breakpoints on reload since the script url always changes,
// so we only apply it to Safari. // so we only apply it to Safari.
metadata.assetQueryString = `?ts=${Date.now()}` baseAssetQueryString = `?ts=${Date.now()}`
} }
} }
// if deploymentId is provided we append it to all asset requests const mutableAssetQueryString =
if (sharedContext.deploymentId) { baseAssetQueryString +
metadata.assetQueryString += `${metadata.assetQueryString ? '&' : '?'}dpl=${ (sharedContext.deploymentId
sharedContext.deploymentId ? `${baseAssetQueryString ? '&' : '?'}dpl=${sharedContext.deploymentId}`
}` : '')
const assetQueryString =
baseAssetQueryString +
(sharedContext.clientAssetToken
? `${baseAssetQueryString ? '&' : '?'}dpl=${sharedContext.clientAssetToken}`
: '')
const metadata: PagesRenderResultMetadata = {
assetQueryString,
mutableAssetQueryString,
} }
// don't modify original query object // don't modify original query object
...@@ -500,8 +511,6 @@ export async function renderToHTMLImpl( ...@@ -500,8 +511,6 @@ export async function renderToHTMLImpl(
} = renderOpts } = renderOpts
const { App } = extra const { App } = extra
const assetQueryString = metadata.assetQueryString
let Document = extra.Document let Document = extra.Document
let Component: React.ComponentType<{}> | ((props: any) => JSX.Element) = let Component: React.ComponentType<{}> | ((props: any) => JSX.Element) =
...@@ -1517,7 +1526,8 @@ export async function renderToHTMLImpl( ...@@ -1517,7 +1526,8 @@ export async function renderToHTMLImpl(
? pageConfig.unstable_runtimeJS ? pageConfig.unstable_runtimeJS
: undefined, : undefined,
unstable_JsPreload: pageConfig.unstable_JsPreload, unstable_JsPreload: pageConfig.unstable_JsPreload,
assetQueryString, assetQueryString: assetQueryString || '',
mutableAssetQueryString: mutableAssetQueryString || '',
scriptLoader, scriptLoader,
locale, locale,
disableOptimizedLoading, disableOptimizedLoading,
......
...@@ -48,7 +48,6 @@ import type { ...@@ -48,7 +48,6 @@ import type {
GetStaticPaths, GetStaticPaths,
GetStaticProps, GetStaticProps,
} from '../../../types' } from '../../../types'
import { getDeploymentId } from '../../../shared/lib/deployment-id'
export const getHandler = ({ export const getHandler = ({
srcPage: originalSrcPage, srcPage: originalSrcPage,
...@@ -146,6 +145,8 @@ export const getHandler = ({ ...@@ -146,6 +145,8 @@ export const getHandler = ({
nextConfig, nextConfig,
resolvedPathname, resolvedPathname,
encodedResolvedPathname, encodedResolvedPathname,
deploymentId,
clientAssetToken,
} = prepareResult } = prepareResult
const isExperimentalCompile = const isExperimentalCompile =
...@@ -266,7 +267,8 @@ export const getHandler = ({ ...@@ -266,7 +267,8 @@ export const getHandler = ({
buildId, buildId,
customServer: customServer:
Boolean(routerServerContext?.isCustomServer) || undefined, Boolean(routerServerContext?.isCustomServer) || undefined,
deploymentId: getDeploymentId() || '', deploymentId,
clientAssetToken,
}, },
renderOpts: { renderOpts: {
params, params,
...@@ -634,7 +636,6 @@ export const getHandler = ({ ...@@ -634,7 +636,6 @@ export const getHandler = ({
res.statusCode = 404 res.statusCode = 404
if (isNextDataRequest) { if (isNextDataRequest) {
const deploymentId = getDeploymentId()
if (deploymentId) { if (deploymentId) {
res.setHeader(NEXT_NAV_DEPLOYMENT_ID_HEADER, deploymentId) res.setHeader(NEXT_NAV_DEPLOYMENT_ID_HEADER, deploymentId)
} }
...@@ -646,7 +647,6 @@ export const getHandler = ({ ...@@ -646,7 +647,6 @@ export const getHandler = ({
if (result.value.kind === CachedRouteKind.REDIRECT) { if (result.value.kind === CachedRouteKind.REDIRECT) {
if (isNextDataRequest) { if (isNextDataRequest) {
const deploymentId = getDeploymentId()
if (deploymentId) { if (deploymentId) {
res.setHeader(NEXT_NAV_DEPLOYMENT_ID_HEADER, deploymentId) res.setHeader(NEXT_NAV_DEPLOYMENT_ID_HEADER, deploymentId)
} }
...@@ -724,7 +724,6 @@ export const getHandler = ({ ...@@ -724,7 +724,6 @@ export const getHandler = ({
// Add deployment ID header for data requests // Add deployment ID header for data requests
if (isNextDataRequest && !isErrorPage && !is500Page) { if (isNextDataRequest && !isErrorPage && !is500Page) {
const deploymentId = getDeploymentId()
if (deploymentId) { if (deploymentId) {
res.setHeader(NEXT_NAV_DEPLOYMENT_ID_HEADER, deploymentId) res.setHeader(NEXT_NAV_DEPLOYMENT_ID_HEADER, deploymentId)
} }
......
...@@ -569,6 +569,7 @@ export abstract class RouteModule< ...@@ -569,6 +569,7 @@ export abstract class RouteModule<
| { | {
buildId: string buildId: string
deploymentId: string deploymentId: string
clientAssetToken: string
locale?: string locale?: string
locales?: readonly string[] locales?: readonly string[]
defaultLocale?: string defaultLocale?: string
...@@ -1026,6 +1027,8 @@ export abstract class RouteModule< ...@@ -1026,6 +1027,8 @@ export abstract class RouteModule<
nextConfig satisfies DeepReadonly<NextConfigRuntime> as NextConfigRuntime, nextConfig satisfies DeepReadonly<NextConfigRuntime> as NextConfigRuntime,
routerServerContext, routerServerContext,
deploymentId, deploymentId,
clientAssetToken:
nextConfig.experimental.immutableAssetToken || deploymentId,
} }
} }
......
...@@ -37,7 +37,7 @@ interface RunnerFnParams { ...@@ -37,7 +37,7 @@ interface RunnerFnParams {
distDir: string distDir: string
incrementalCache?: any incrementalCache?: any
serverComponentsHmrCache?: ServerComponentsHmrCache serverComponentsHmrCache?: ServerComponentsHmrCache
deploymentId: string clientAssetToken: string
} }
type RunnerFn = (params: RunnerFnParams) => Promise<FetchEventResult> type RunnerFn = (params: RunnerFnParams) => Promise<FetchEventResult>
...@@ -97,10 +97,9 @@ export async function getRuntimeContext( ...@@ -97,10 +97,9 @@ export async function getRuntimeContext(
params.serverComponentsHmrCache params.serverComponentsHmrCache
} }
if (params.deploymentId) { if (params.clientAssetToken) {
runtime.context.globalThis.NEXT_CLIENT_ASSET_SUFFIX = params.deploymentId runtime.context.globalThis.NEXT_CLIENT_ASSET_SUFFIX =
? `?dpl=${params.deploymentId}` params.clientAssetToken ? `?dpl=${params.clientAssetToken}` : ''
: ''
} }
for (const paramPath of params.paths) { for (const paramPath of params.paths) {
......
...@@ -15,9 +15,24 @@ export function getDeploymentId(): string | undefined { ...@@ -15,9 +15,24 @@ export function getDeploymentId(): string | undefined {
return deploymentId return deploymentId
} }
export function getDeploymentIdQueryOrEmptyString(): string { export function getDeploymentIdQuery(ampersand = false): string {
if (deploymentId) { let id = getDeploymentId()
return `?dpl=${deploymentId}` if (id) {
return `${ampersand ? '&' : '?'}dpl=${id}`
}
return ''
}
export function getAssetToken(): string | undefined {
return (
process.env.NEXT_IMMUTABLE_ASSET_TOKEN || process.env.NEXT_DEPLOYMENT_ID
)
}
export function getAssetTokenQuery(ampersand = false): string {
let id = getAssetToken()
if (id) {
return `${ampersand ? '&' : '?'}dpl=${id}`
} }
return '' return ''
} }
...@@ -30,6 +30,7 @@ export type HtmlProps = { ...@@ -30,6 +30,7 @@ export type HtmlProps = {
unstable_runtimeJS?: false unstable_runtimeJS?: false
unstable_JsPreload?: false unstable_JsPreload?: false
assetQueryString: string assetQueryString: string
mutableAssetQueryString: string
scriptLoader: { scriptLoader: {
afterInteractive?: string[] afterInteractive?: string[]
beforeInteractive?: any[] beforeInteractive?: any[]
......
...@@ -26,7 +26,9 @@ function defaultLoader({ ...@@ -26,7 +26,9 @@ function defaultLoader({
} }
} }
// Extract dpl parameter early so validation uses the clean URL // Extract dpl parameter early so validation uses the clean URL.
// If a immutable asset token should be used, it was already added as a query parameter and will
// be extracted and reused here.
let deploymentId = getDeploymentId() let deploymentId = getDeploymentId()
if (src.startsWith('/') && !src.startsWith('//')) { if (src.startsWith('/') && !src.startsWith('//')) {
// We unfortunately can't easily use `new URL()` here, because it normalizes the URL which causes // We unfortunately can't easily use `new URL()` here, because it normalizes the URL which causes
......
...@@ -4,7 +4,7 @@ import { preload } from 'react-dom' ...@@ -4,7 +4,7 @@ import { preload } from 'react-dom'
import { workAsyncStorage } from '../../../server/app-render/work-async-storage.external' import { workAsyncStorage } from '../../../server/app-render/work-async-storage.external'
import { encodeURIPath } from '../encode-uri-path' import { encodeURIPath } from '../encode-uri-path'
import { getDeploymentIdQueryOrEmptyString } from '../deployment-id' import { getAssetTokenQuery } from '../deployment-id'
export function PreloadChunks({ export function PreloadChunks({
moduleIds, moduleIds,
...@@ -38,12 +38,12 @@ export function PreloadChunks({ ...@@ -38,12 +38,12 @@ export function PreloadChunks({
return null return null
} }
const dplId = getDeploymentIdQueryOrEmptyString() const query = getAssetTokenQuery()
return ( return (
<> <>
{allFiles.map((chunk) => { {allFiles.map((chunk) => {
const href = `${workStore.assetPrefix}/_next/${encodeURIPath(chunk)}${dplId}` const href = `${workStore.assetPrefix}/_next/${encodeURIPath(chunk)}${query}`
const isCss = chunk.endsWith('.css') const isCss = chunk.endsWith('.css')
// If it's stylesheet we use `precedence` o help hoist with React Float. // If it's stylesheet we use `precedence` o help hoist with React Float.
// For stylesheets we actually need to render the CSS because nothing else is going to do it so it needs to be part of the component tree. // For stylesheets we actually need to render the CSS because nothing else is going to do it so it needs to be part of the component tree.
......
...@@ -16,7 +16,7 @@ describe(`Handle new URL asset references`, () => { ...@@ -16,7 +16,7 @@ describe(`Handle new URL asset references`, () => {
env: { env: {
// rely on skew protection when deployed // rely on skew protection when deployed
NEXT_DEPLOYMENT_ID: isNextStart ? 'test-deployment-id' : undefined, NEXT_DEPLOYMENT_ID: isNextStart ? 'test-deployment-id' : undefined,
VERCEL_IMMUTABLE_DEPLOYMENT_ID: isNextStart __NEXT_IMMUTABLE_ASSET_TOKEN: isNextStart
? 'test-immutable-tkn-7890' ? 'test-immutable-tkn-7890'
: undefined, : undefined,
}, },
......
...@@ -285,7 +285,7 @@ export class NextInstance { ...@@ -285,7 +285,7 @@ export class NextInstance {
? // since we can't get the build id as a build artifact, ? // since we can't get the build id as a build artifact,
// add it in build logs // add it in build logs
{ {
'post-build': `node -e 'console.log("BUILD" + "_ID: " + fs.readFileSync("${this.distDir}/BUILD_ID") + "\\nDEPLOYMENT" + "_ID: " + process.env.NEXT_DEPLOYMENT_ID)'`, 'post-build': `node -e 'console.log("BUILD" + "_ID: " + fs.readFileSync("${this.distDir}/BUILD_ID") + "\\nDEPLOYMENT" + "_ID: " + process.env.NEXT_DEPLOYMENT_ID + "\\nIMMUTABLE_ASSET" + "_TOKEN: " + process.env.VERCEL_IMMUTABLE_ASSET_TOKEN)'`,
} }
: {}), : {}),
...pkgScripts, ...pkgScripts,
...@@ -647,8 +647,12 @@ export class NextInstance { ...@@ -647,8 +647,12 @@ export class NextInstance {
return this.deploymentId ? `${prefix}dpl=${this.deploymentId}` : '' return this.deploymentId ? `${prefix}dpl=${this.deploymentId}` : ''
} }
public get immutableAssetToken(): string | undefined {
return undefined
}
public get assetToken(): string | undefined { public get assetToken(): string | undefined {
return this.deploymentId return this.immutableAssetToken || this.deploymentId
} }
public getAssetQuery(ampersand: boolean = false): string | undefined { public getAssetQuery(ampersand: boolean = false): string | undefined {
......
...@@ -10,6 +10,7 @@ export class NextDeployInstance extends NextInstance { ...@@ -10,6 +10,7 @@ export class NextDeployInstance extends NextInstance {
private _cliOutput: string private _cliOutput: string
private _buildId: string private _buildId: string
private _deploymentId: string | undefined private _deploymentId: string | undefined
private _immutableAssetToken: string | undefined
private _writtenHostsLine: string | null = null private _writtenHostsLine: string | null = null
protected throwIfUnavailable(): void | never { protected throwIfUnavailable(): void | never {
...@@ -38,6 +39,10 @@ export class NextDeployInstance extends NextInstance { ...@@ -38,6 +39,10 @@ export class NextDeployInstance extends NextInstance {
return this._deploymentId return this._deploymentId
} }
public get immutableAssetToken() {
return process.env.IS_TURBOPACK_TEST ? this._immutableAssetToken : undefined
}
private async deployUsingCustomScript(): Promise<{ url: string }> { private async deployUsingCustomScript(): Promise<{ url: string }> {
const deployScriptPath = process.env.NEXT_TEST_DEPLOY_SCRIPT_PATH! const deployScriptPath = process.env.NEXT_TEST_DEPLOY_SCRIPT_PATH!
...@@ -131,9 +136,18 @@ export class NextDeployInstance extends NextInstance { ...@@ -131,9 +136,18 @@ export class NextDeployInstance extends NextInstance {
throw new Error(`Failed to get deploymentId from logs ${this._cliOutput}`) throw new Error(`Failed to get deploymentId from logs ${this._cliOutput}`)
} }
this._deploymentId = deploymentId this._deploymentId = deploymentId
const immutableAssetToken = this._cliOutput
.match(/IMMUTABLE_ASSET_TOKEN: (.+)/)?.[1]
?.trim()
if (!immutableAssetToken) {
throw new Error(
`Failed to get immutableAssetToken from logs ${this._cliOutput}`
)
}
this._immutableAssetToken = immutableAssetToken
require('console').log( require('console').log(
`Got buildId: ${this._buildId}, deploymentId: ${this._deploymentId}` `Got buildId: ${this._buildId}, deploymentId: ${this._deploymentId}, immutableAssetToken: ${this._immutableAssetToken}`
) )
} }
......
...@@ -10,6 +10,7 @@ import { shouldUseTurbopack } from 'next-test-utils' ...@@ -10,6 +10,7 @@ import { shouldUseTurbopack } from 'next-test-utils'
export class NextStartInstance extends NextInstance { export class NextStartInstance extends NextInstance {
private _buildId: string private _buildId: string
private _deploymentId: string | undefined private _deploymentId: string | undefined
private _immutableAssetToken: string | undefined
private _cliOutput: string = '' private _cliOutput: string = ''
private _prerenderFinishedTimeMS: number | null = null private _prerenderFinishedTimeMS: number | null = null
...@@ -19,6 +20,7 @@ export class NextStartInstance extends NextInstance { ...@@ -19,6 +20,7 @@ export class NextStartInstance extends NextInstance {
if (!opts.disableAutoSkewProtection && shouldUseTurbopack()) { if (!opts.disableAutoSkewProtection && shouldUseTurbopack()) {
this.env.NEXT_DEPLOYMENT_ID = 'test-dpl-id-1234' this.env.NEXT_DEPLOYMENT_ID = 'test-dpl-id-1234'
this.env.__NEXT_IMMUTABLE_ASSET_TOKEN = 'test-immutable-tkn-7890'
} }
} }
...@@ -30,6 +32,10 @@ export class NextStartInstance extends NextInstance { ...@@ -30,6 +32,10 @@ export class NextStartInstance extends NextInstance {
return this._deploymentId return this._deploymentId
} }
public get immutableAssetToken() {
return process.env.IS_TURBOPACK_TEST ? this._immutableAssetToken : undefined
}
public get cliOutput() { public get cliOutput() {
return this._cliOutput return this._cliOutput
} }
...@@ -141,6 +147,9 @@ export class NextStartInstance extends NextInstance { ...@@ -141,6 +147,9 @@ export class NextStartInstance extends NextInstance {
) )
this._deploymentId = this._deploymentId =
requiredServerFiles.config?.deploymentId || undefined requiredServerFiles.config?.deploymentId || undefined
this._immutableAssetToken =
requiredServerFiles.config?.experimental.immutableAssetToken ||
undefined
} catch {} } catch {}
} }
...@@ -293,6 +302,9 @@ export class NextStartInstance extends NextInstance { ...@@ -293,6 +302,9 @@ export class NextStartInstance extends NextInstance {
) )
) )
this._deploymentId = requiredServerFiles.config?.deploymentId || undefined this._deploymentId = requiredServerFiles.config?.deploymentId || undefined
this._immutableAssetToken =
requiredServerFiles.config?.experimental.immutableAssetToken ||
undefined
} catch {} } catch {}
return result return result
......
...@@ -522,6 +522,7 @@ export function nextBuild( ...@@ -522,6 +522,7 @@ export function nextBuild(
if (!opts.disableAutoSkewProtection && shouldUseTurbopack() && !opts.env) { if (!opts.disableAutoSkewProtection && shouldUseTurbopack() && !opts.env) {
opts.env ??= {} opts.env ??= {}
opts.env.NEXT_DEPLOYMENT_ID = 'test-dpl-id-1234' opts.env.NEXT_DEPLOYMENT_ID = 'test-dpl-id-1234'
opts.env.__NEXT_IMMUTABLE_ASSET_TOKEN = 'test-immutable-tkn-7890'
} }
return runNextCommand(['build', dir, ...args], opts) return runNextCommand(['build', dir, ...args], opts)
...@@ -549,6 +550,7 @@ export function nextStart( ...@@ -549,6 +550,7 @@ export function nextStart(
if (!opts.disableAutoSkewProtection && shouldUseTurbopack() && !opts.env) { if (!opts.disableAutoSkewProtection && shouldUseTurbopack() && !opts.env) {
opts.env ??= {} opts.env ??= {}
opts.env.NEXT_DEPLOYMENT_ID = 'test-dpl-id-1234' opts.env.NEXT_DEPLOYMENT_ID = 'test-dpl-id-1234'
opts.env.__NEXT_IMMUTABLE_ASSET_TOKEN = 'test-immutable-tkn-7890'
} }
return runNextCommandDev( return runNextCommandDev(
......
...@@ -7,7 +7,7 @@ describe('css-url-deployment-id', () => { ...@@ -7,7 +7,7 @@ describe('css-url-deployment-id', () => {
dependencies: { sass: '1.54.0' }, dependencies: { sass: '1.54.0' },
env: { env: {
NEXT_DEPLOYMENT_ID: isNextStart ? 'test-deployment-id' : undefined, NEXT_DEPLOYMENT_ID: isNextStart ? 'test-deployment-id' : undefined,
VERCEL_IMMUTABLE_DEPLOYMENT_ID: isNextStart __NEXT_IMMUTABLE_ASSET_TOKEN: isNextStart
? 'imm-deployment-id' ? 'imm-deployment-id'
: undefined, : undefined,
}, },
......
/** @type {import('next').NextConfig} */ /** @type {import('next').NextConfig} */
module.exports = { module.exports = {
deploymentId: process.env.CUSTOM_DEPLOYMENT_ID, deploymentId:
process.env.IMMUTABLE_ASSET_TOKEN ?? process.env.CUSTOM_DEPLOYMENT_ID,
experimental: { experimental: {
useSkewCookie: Boolean(process.env.COOKIE_SKEW), useSkewCookie: Boolean(process.env.COOKIE_SKEW),
runtimeServerDeploymentId: !!process.env.RUNTIME_SERVER_DEPLOYMENT_ID, runtimeServerDeploymentId: !!process.env.RUNTIME_SERVER_DEPLOYMENT_ID,
immutableAssetToken: process.env.IMMUTABLE_ASSET_TOKEN
? `imm-${process.env.IMMUTABLE_ASSET_TOKEN}`
: undefined,
}, },
} }
...@@ -6,10 +6,19 @@ describe.each([ ...@@ -6,10 +6,19 @@ describe.each([
['NEXT_DEPLOYMENT_ID', ''], ['NEXT_DEPLOYMENT_ID', ''],
['CUSTOM_DEPLOYMENT_ID', ''], ['CUSTOM_DEPLOYMENT_ID', ''],
['NEXT_DEPLOYMENT_ID', ' and runtimeServerDeploymentId'], ['NEXT_DEPLOYMENT_ID', ' and runtimeServerDeploymentId'],
['IMMUTABLE_ASSET_TOKEN', ''],
])( ])(
'deployment-id-handling enabled with %s%s', 'deployment-id-handling enabled with %s%s',
(envKey, runtimeServerDeploymentId) => { (envKey, runtimeServerDeploymentId) => {
if (envKey === 'IMMUTABLE_ASSET_TOKEN' && !process.env.IS_TURBOPACK_TEST) {
it.skip('skip for webpack', () => {})
return
}
const deploymentId = Date.now() + '' const deploymentId = Date.now() + ''
const immutableAssetToken =
envKey === 'IMMUTABLE_ASSET_TOKEN' ? `imm-${deploymentId}` : deploymentId
const { next } = nextTestSetup({ const { next } = nextTestSetup({
files: join(__dirname, 'app'), files: join(__dirname, 'app'),
env: { env: {
...@@ -21,6 +30,18 @@ describe.each([ ...@@ -21,6 +30,18 @@ describe.each([
disableAutoSkewProtection: true, disableAutoSkewProtection: true,
}) })
const tokenForRequest = (url) => {
return url.includes('_next/static/chunks') ||
url.includes('_next/static/media')
? // Turbopack-emitted chunks
immutableAssetToken
: // e.g. _next/static/build-id/_ssgManifest.js
deploymentId
}
const validateTokenForRequest = (url) => {
expect(url).toContain('dpl=' + tokenForRequest(url))
}
it.each([ it.each([
{ urlPath: '/' }, { urlPath: '/' },
{ urlPath: '/pages-edge' }, { urlPath: '/pages-edge' },
...@@ -39,7 +60,7 @@ describe.each([ ...@@ -39,7 +60,7 @@ describe.each([
for (const script of scripts) { for (const script of scripts) {
if (script.attribs.src) { if (script.attribs.src) {
expect(script.attribs.src).toContain('dpl=' + deploymentId) validateTokenForRequest(script.attribs.src)
} }
} }
...@@ -48,7 +69,7 @@ describe.each([ ...@@ -48,7 +69,7 @@ describe.each([
for (const link of links) { for (const link of links) {
if (link.attribs.href && link.attribs.rel !== 'expect') { if (link.attribs.href && link.attribs.rel !== 'expect') {
expect(link.attribs.href).toContain('dpl=' + deploymentId) validateTokenForRequest(link.attribs.href)
} }
} }
...@@ -59,6 +80,7 @@ describe.each([ ...@@ -59,6 +80,7 @@ describe.each([
const browser = await next.browser(urlPath, { const browser = await next.browser(urlPath, {
beforePageLoad(page) { beforePageLoad(page) {
page.on('request', async (req) => { page.on('request', async (req) => {
// TODO this currently exclude _next/image
if (req.url().includes('/_next/static')) { if (req.url().includes('/_next/static')) {
clientRequests.push(req.url()) clientRequests.push(req.url())
} }
...@@ -76,11 +98,9 @@ describe.each([ ...@@ -76,11 +98,9 @@ describe.each([
await retry(() => expect(dynamicImportRequests).not.toBeEmpty()) await retry(() => expect(dynamicImportRequests).not.toBeEmpty())
try { try {
expect( expect(dynamicImportRequests).toSatisfyAll((item) =>
dynamicImportRequests.every((item) => item.includes('dpl=' + tokenForRequest(item))
item.includes('dpl=' + deploymentId) )
)
).toBe(true)
} finally { } finally {
require('console').error( require('console').error(
'dynamicImportRequests', 'dynamicImportRequests',
...@@ -89,9 +109,9 @@ describe.each([ ...@@ -89,9 +109,9 @@ describe.each([
} }
try { try {
expect( expect(clientRequests).toSatisfyAll((item) =>
clientRequests.every((item) => item.includes('dpl=' + deploymentId)) item.includes('dpl=' + tokenForRequest(item))
).toBe(true) )
} finally { } finally {
require('console').error('clientRequests', clientRequests) require('console').error('clientRequests', clientRequests)
} }
...@@ -114,7 +134,7 @@ describe.each([ ...@@ -114,7 +134,7 @@ describe.each([
const browser = await next.browser('/', { const browser = await next.browser('/', {
beforePageLoad(page) { beforePageLoad(page) {
page.on('request', async (req) => { page.on('request', async (req) => {
const headers = await req.allHeaders() const headers = req.headers()
if (headers['x-nextjs-data']) { if (headers['x-nextjs-data']) {
dataHeaders.push(headers) dataHeaders.push(headers)
} }
...@@ -142,7 +162,7 @@ describe.each([ ...@@ -142,7 +162,7 @@ describe.each([
const browser = await next.browser('/from-app', { const browser = await next.browser('/from-app', {
beforePageLoad(page) { beforePageLoad(page) {
page.on('request', async (req) => { page.on('request', async (req) => {
const headers = await req.allHeaders() const headers = req.headers()
if (headers['rsc']) { if (headers['rsc']) {
rscHeaders.push(headers) rscHeaders.push(headers)
} }
...@@ -158,11 +178,9 @@ describe.each([ ...@@ -158,11 +178,9 @@ describe.each([
expect(rscHeaders.length).toBeGreaterThan(0) expect(rscHeaders.length).toBeGreaterThan(0)
}) })
expect( expect(rscHeaders).toSatisfyAll(
rscHeaders.every( (headers) => headers['x-deployment-id'] === deploymentId
(headers) => headers['x-deployment-id'] === deploymentId )
)
).toBe(true)
}) })
} }
) )
...@@ -215,9 +233,9 @@ describe('deployment-id-handling disabled', () => { ...@@ -215,9 +233,9 @@ describe('deployment-id-handling disabled', () => {
await retry(() => expect(requests).not.toBeEmpty()) await retry(() => expect(requests).not.toBeEmpty())
try { try {
expect( expect(requests).toSatisfyAll(
requests.every((item) => !item.includes('dpl=' + deploymentId)) (item) => !item.includes('dpl=' + deploymentId)
).toBe(true) )
} finally { } finally {
require('console').error('requests', requests) require('console').error('requests', requests)
} }
......
...@@ -8,6 +8,19 @@ jest.mock('next/dist/shared/lib/deployment-id.js', () => { ...@@ -8,6 +8,19 @@ jest.mock('next/dist/shared/lib/deployment-id.js', () => {
getDeploymentId() { getDeploymentId() {
return deploymentId return deploymentId
}, },
getDeploymentIdQuery(ampersand = false): string {
let id = deploymentId
if (id) {
return `${ampersand ? '&' : '?'}dpl=${id}`
}
return ''
},
getAssetToken() {
throw new Error("getAssetToken shouldn't be called ")
},
getAssetTokenQuery(ampersand = false) {
throw new Error("getAssetTokenQuery shouldn't be called ")
},
} }
}) })
......
...@@ -11,6 +11,19 @@ jest.mock('next/dist/shared/lib/deployment-id.js', () => { ...@@ -11,6 +11,19 @@ jest.mock('next/dist/shared/lib/deployment-id.js', () => {
getDeploymentId() { getDeploymentId() {
return deploymentId return deploymentId
}, },
getDeploymentIdQuery(ampersand = false): string {
let id = deploymentId
if (id) {
return `${ampersand ? '&' : '?'}dpl=${id}`
}
return ''
},
getAssetToken() {
throw new Error("getAssetToken shouldn't be called ")
},
getAssetTokenQuery(ampersand = false) {
throw new Error("getAssetTokenQuery shouldn't be called ")
},
} }
}) })
......
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