Unverified Commit a1ba3d4b authored by Niklas Mischkulnig's avatar Niklas Mischkulnig Committed by GitHub

Turbopack: don't use build id for pages router client-side manifests (#88641)

Goes from
```
/_next/static/THE_BUILD_ID/_buildManifest.js?dpl=123
/_next/static/THE_BUILD_ID/_ssgManifest.js?dpl=123
/_next/static/THE_BUILD_ID/_clientMiddlewareManifest.js?dpl=123
```
to
```
/_next/static/_buildManifest.js?dpl=123
/_next/static/_ssgManifest.js?dpl=123
/_next/static/_clientMiddlewareManifest.js?dpl=123
```
if skew protection is enabled. This makes the build-manifest.json not depend on the build id

Closes PACK-6535
parent a6f714cb
...@@ -977,5 +977,6 @@ ...@@ -977,5 +977,6 @@
"976": "Decompressed resume data cache exceeded %s byte limit", "976": "Decompressed resume data cache exceeded %s byte limit",
"977": "maxPostponedStateSize must be a valid number (bytes) or filesize format string (e.g., \"5mb\")", "977": "maxPostponedStateSize must be a valid number (bytes) or filesize format string (e.g., \"5mb\")",
"978": "Next.js has blocked a javascript: URL as a security precaution.", "978": "Next.js has blocked a javascript: URL as a security precaution.",
"979": "invariant: expected %s bytes of postponed state but only received %s bytes" "979": "invariant: expected %s bytes of postponed state but only received %s bytes",
"980": "Failed to load client middleware manifest"
} }
...@@ -553,10 +553,12 @@ async function writeClientSsgManifest( ...@@ -553,10 +553,12 @@ async function writeClientSsgManifest(
buildId, buildId,
distDir, distDir,
locales, locales,
deploymentId,
}: { }: {
buildId: string buildId: string
distDir: string distDir: string
locales: readonly string[] | undefined locales: readonly string[] | undefined
deploymentId: string
} }
) { ) {
const ssgPages = new Set<string>( const ssgPages = new Set<string>(
...@@ -573,8 +575,14 @@ async function writeClientSsgManifest( ...@@ -573,8 +575,14 @@ async function writeClientSsgManifest(
ssgPages ssgPages
)};self.__SSG_MANIFEST_CB&&self.__SSG_MANIFEST_CB()` )};self.__SSG_MANIFEST_CB&&self.__SSG_MANIFEST_CB()`
// When skew protection is enabled, we instead just rely on the deployment id query string to
// load the correct manifests, to avoid the build id.
let ssgManifestPath = deploymentId
? path.join(CLIENT_STATIC_FILES_PATH, '_ssgManifest.js')
: path.join(CLIENT_STATIC_FILES_PATH, buildId, '_ssgManifest.js')
await writeFileUtf8( await writeFileUtf8(
path.join(distDir, CLIENT_STATIC_FILES_PATH, buildId, '_ssgManifest.js'), path.join(distDir, ssgManifestPath),
clientSsgManifestContent clientSsgManifestContent
) )
} }
...@@ -1712,6 +1720,8 @@ export default async function build( ...@@ -1712,6 +1720,8 @@ export default async function build(
) )
} }
// #region Compile
Log.info('Creating an optimized production build ...') Log.info('Creating an optimized production build ...')
traceMemoryUsage('Starting build', nextBuildSpan) traceMemoryUsage('Starting build', nextBuildSpan)
...@@ -1881,6 +1891,8 @@ export default async function build( ...@@ -1881,6 +1891,8 @@ export default async function build(
}) })
} }
// #endregion
// For app directory, we run type checking after build. // For app directory, we run type checking after build.
if (appDir && !isCompileMode && !isGenerateMode) { if (appDir && !isCompileMode && !isGenerateMode) {
await updateBuildDiagnostics({ await updateBuildDiagnostics({
...@@ -1890,6 +1902,7 @@ export default async function build( ...@@ -1890,6 +1902,7 @@ export default async function build(
traceMemoryUsage('Finished type checking', nextBuildSpan) traceMemoryUsage('Finished type checking', nextBuildSpan)
} }
// #region required-server-files
const requiredServerFilesManifest = await nextBuildSpan const requiredServerFilesManifest = await nextBuildSpan
.traceChild('generate-required-server-files') .traceChild('generate-required-server-files')
.traceAsyncFn(async () => { .traceAsyncFn(async () => {
...@@ -2070,6 +2083,9 @@ export default async function build( ...@@ -2070,6 +2083,9 @@ export default async function build(
requiredServerFilesManifest requiredServerFilesManifest
) )
// #endregion
// #region Collect data
const numberOfWorkers = getNumberOfWorkers(config) const numberOfWorkers = getNumberOfWorkers(config)
const collectingPageDataStart = process.hrtime() const collectingPageDataStart = process.hrtime()
const postCompileSpinner = createSpinner( const postCompileSpinner = createSpinner(
...@@ -2697,14 +2713,26 @@ export default async function build( ...@@ -2697,14 +2713,26 @@ export default async function build(
} }
if (bundler === Bundler.Turbopack) { if (bundler === Bundler.Turbopack) {
await writeManifest( const clientMiddlewareManifestJs = `self.__MIDDLEWARE_MATCHERS = ${JSON.stringify(
path.join( functionsConfigManifest.functions['/_middleware'].matchers || [],
distDir, null,
'static', 2
)};self.__MIDDLEWARE_MATCHERS_CB && self.__MIDDLEWARE_MATCHERS_CB()`
let clientMiddlewareManifestPath = config.deploymentId
? path.join(
CLIENT_STATIC_FILES_PATH,
TURBOPACK_CLIENT_MIDDLEWARE_MANIFEST
)
: path.join(
CLIENT_STATIC_FILES_PATH,
buildId, buildId,
TURBOPACK_CLIENT_MIDDLEWARE_MANIFEST TURBOPACK_CLIENT_MIDDLEWARE_MANIFEST
), )
functionsConfigManifest.functions['/_middleware'].matchers || []
await writeFileUtf8(
path.join(distDir, clientMiddlewareManifestPath),
clientMiddlewareManifestJs
) )
} }
} }
...@@ -2712,6 +2740,9 @@ export default async function build( ...@@ -2712,6 +2740,9 @@ export default async function build(
await writeFunctionsConfigManifest(distDir, functionsConfigManifest) await writeFunctionsConfigManifest(distDir, functionsConfigManifest)
// #endregion
// #region NFT
if ( if (
bundler !== Bundler.Turbopack && bundler !== Bundler.Turbopack &&
!isGenerateMode && !isGenerateMode &&
...@@ -2869,6 +2900,9 @@ export default async function build( ...@@ -2869,6 +2900,9 @@ export default async function build(
const hasGSPAndRevalidateZero = new Set<string>() const hasGSPAndRevalidateZero = new Set<string>()
// #endregion
// #region SSG
// we need to trigger automatic exporting when we have // we need to trigger automatic exporting when we have
// - static 404/500 // - static 404/500
// - getStaticProps paths // - getStaticProps paths
...@@ -4045,6 +4079,7 @@ export default async function build( ...@@ -4045,6 +4079,7 @@ export default async function build(
distDir, distDir,
buildId, buildId,
locales: config.i18n?.locales, locales: config.i18n?.locales,
deploymentId: config.deploymentId,
}) })
} else { } else {
await writePrerenderManifest(distDir, { await writePrerenderManifest(distDir, {
...@@ -4056,6 +4091,8 @@ export default async function build( ...@@ -4056,6 +4091,8 @@ export default async function build(
}) })
} }
// #endregion
await writeImagesManifest(distDir, config) await writeImagesManifest(distDir, config)
await writeManifest(path.join(distDir, EXPORT_MARKER), { await writeManifest(path.join(distDir, EXPORT_MARKER), {
version: 1, version: 1,
......
...@@ -100,9 +100,11 @@ export async function turbopackBuild(): Promise<{ ...@@ -100,9 +100,11 @@ export async function turbopackBuild(): Promise<{
await fs.writeFile(path.join(distDir, 'turbopack'), '') await fs.writeFile(path.join(distDir, 'turbopack'), '')
await fs.mkdir(path.join(distDir, 'server'), { recursive: true }) await fs.mkdir(path.join(distDir, 'server'), { recursive: true })
if (!config.deploymentId) {
await fs.mkdir(path.join(distDir, 'static', buildId), { await fs.mkdir(path.join(distDir, 'static', buildId), {
recursive: true, recursive: true,
}) })
}
await fs.writeFile( await fs.writeFile(
path.join(distDir, 'package.json'), path.join(distDir, 'package.json'),
'{"type": "commonjs"}' '{"type": "commonjs"}'
...@@ -134,6 +136,8 @@ export async function turbopackBuild(): Promise<{ ...@@ -134,6 +136,8 @@ export async function turbopackBuild(): Promise<{
buildId, buildId,
distDir, distDir,
encryptionKey, encryptionKey,
dev: false,
deploymentId: config.deploymentId,
}) })
const currentEntrypoints = await rawEntrypointsToEntrypoints( const currentEntrypoints = await rawEntrypointsToEntrypoints(
......
...@@ -2115,6 +2115,8 @@ export default async function getBaseWebpackConfig( ...@@ -2115,6 +2115,8 @@ export default async function getBaseWebpackConfig(
isClient && isClient &&
new BuildManifestPlugin({ new BuildManifestPlugin({
buildId, buildId,
dev,
deploymentId: config.deploymentId,
rewrites, rewrites,
isDevFallback, isDevFallback,
appDirEnabled: hasAppDir, appDirEnabled: hasAppDir,
......
...@@ -50,32 +50,9 @@ export function normalizeRewritesForBuildManifest( ...@@ -50,32 +50,9 @@ export function normalizeRewritesForBuildManifest(
} }
export function createEdgeRuntimeManifest( export function createEdgeRuntimeManifest(
originAssetMap: Partial<BuildManifest> assetMap: Partial<BuildManifest>
): string { ): string {
const manifestFilenames = ['_buildManifest.js', '_ssgManifest.js']
const assetMap: Partial<BuildManifest> = {
...originAssetMap,
lowPriorityFiles: [],
}
// we use globalThis here because middleware can be node // we use globalThis here because middleware can be node
// which doesn't have "self" // which doesn't have "self"
const manifestDefCode = `globalThis.__BUILD_MANIFEST = ${JSON.stringify( return `globalThis.__BUILD_MANIFEST = ${JSON.stringify(assetMap, null, 2)};\n`
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 =
`globalThis.__BUILD_MANIFEST.lowPriorityFiles = [\n` +
manifestFilenames
.map((filename) => {
return `"/static/" + process.env.__NEXT_BUILD_ID + "/${filename}"`
})
.join(',\n') +
`\n];`
return manifestDefCode + lowPriorityFilesCode
} }
...@@ -28,8 +28,19 @@ import { ...@@ -28,8 +28,19 @@ import {
type DeepMutable<T> = { -readonly [P in keyof T]: DeepMutable<T[P]> } type DeepMutable<T> = { -readonly [P in keyof T]: DeepMutable<T[P]> }
// nodejs: '/static/<build id>/low-priority.js' // nodejs: '/static/<build id>/low-priority.js'
function buildNodejsLowPriorityPath(filename: string, buildId: string) { // or with skew protection: '/static/low-priority.js'
function buildNodejsLowPriorityPath(
filename: string,
buildId: string,
dev: boolean,
deploymentId: string
) {
if (!dev && deploymentId) {
// Leverage skew protection
return `${CLIENT_STATIC_FILES_PATH}/${filename}`
} else {
return `${CLIENT_STATIC_FILES_PATH}/${buildId}/${filename}` return `${CLIENT_STATIC_FILES_PATH}/${buildId}/${filename}`
}
} }
// This function takes the asset map generated in BuildManifestPlugin and creates a // This function takes the asset map generated in BuildManifestPlugin and creates a
...@@ -102,6 +113,8 @@ export function getEntrypointFiles(entrypoint: any): string[] { ...@@ -102,6 +113,8 @@ export function getEntrypointFiles(entrypoint: any): string[] {
// It has a mapping of "entry" filename to real filename. Because the real filename can be hashed in production // It has a mapping of "entry" filename to real filename. Because the real filename can be hashed in production
export default class BuildManifestPlugin { export default class BuildManifestPlugin {
private buildId: string private buildId: string
private dev: boolean
private deploymentId: string
private rewrites: CustomRoutes['rewrites'] private rewrites: CustomRoutes['rewrites']
private isDevFallback: boolean private isDevFallback: boolean
private appDirEnabled: boolean private appDirEnabled: boolean
...@@ -109,12 +122,16 @@ export default class BuildManifestPlugin { ...@@ -109,12 +122,16 @@ export default class BuildManifestPlugin {
constructor(options: { constructor(options: {
buildId: string buildId: string
dev: boolean
deploymentId: string
rewrites: CustomRoutes['rewrites'] rewrites: CustomRoutes['rewrites']
isDevFallback?: boolean isDevFallback?: boolean
appDirEnabled: boolean appDirEnabled: boolean
clientRouterFilters?: Parameters<typeof generateClientManifest>[2] clientRouterFilters?: Parameters<typeof generateClientManifest>[2]
}) { }) {
this.buildId = options.buildId this.buildId = options.buildId
this.dev = options.dev
this.deploymentId = options.deploymentId
this.isDevFallback = !!options.isDevFallback this.isDevFallback = !!options.isDevFallback
this.rewrites = { this.rewrites = {
beforeFiles: [], beforeFiles: [],
...@@ -206,11 +223,15 @@ export default class BuildManifestPlugin { ...@@ -206,11 +223,15 @@ export default class BuildManifestPlugin {
// downloaded by the client. // downloaded by the client.
const buildManifestPath = buildNodejsLowPriorityPath( const buildManifestPath = buildNodejsLowPriorityPath(
'_buildManifest.js', '_buildManifest.js',
this.buildId this.buildId,
this.dev,
this.deploymentId
) )
const ssgManifestPath = buildNodejsLowPriorityPath( const ssgManifestPath = buildNodejsLowPriorityPath(
'_ssgManifest.js', '_ssgManifest.js',
this.buildId this.buildId,
this.dev,
this.deploymentId
) )
assetMap.lowPriorityFiles.push(buildManifestPath, ssgManifestPath) assetMap.lowPriorityFiles.push(buildManifestPath, ssgManifestPath)
compilation.emitAsset( compilation.emitAsset(
...@@ -244,8 +265,14 @@ export default class BuildManifestPlugin { ...@@ -244,8 +265,14 @@ export default class BuildManifestPlugin {
) )
if (!this.isDevFallback) { if (!this.isDevFallback) {
const buildManifestPath = buildNodejsLowPriorityPath(
'_buildManifest.js',
this.buildId,
this.dev,
this.deploymentId
)
compilation.emitAsset( compilation.emitAsset(
`${CLIENT_STATIC_FILES_PATH}/${this.buildId}/_buildManifest.js`, buildManifestPath,
new sources.RawSource( new sources.RawSource(
`self.__BUILD_MANIFEST = ${generateClientManifest( `self.__BUILD_MANIFEST = ${generateClientManifest(
assetMap, assetMap,
......
import { requestIdleCallback } from '../request-idle-callback'
// 3.8s was arbitrarily chosen as it's what https://web.dev/interactive
// considers as "Good" time-to-interactive. We must assume something went
// wrong beyond this point, and then fall-back to a full page transition to
// show the user something of value.
const MS_MAX_IDLE_DELAY = 3800
/** Resolve a promise that times out after given amount of milliseconds. */
export function resolvePromiseWithTimeout<T>(
p: Promise<T>,
err: Error,
devPromise: Promise<void> | undefined
): Promise<T> {
return new Promise((resolve, reject) => {
let cancelled = false
p.then((r) => {
// Resolved, cancel the timeout
cancelled = true
resolve(r)
}).catch(reject)
// We wrap these checks separately for better dead-code elimination in
// production bundles.
if (process.env.NODE_ENV === 'development') {
;(devPromise || Promise.resolve()).then(() => {
requestIdleCallback(() =>
setTimeout(() => {
if (!cancelled) {
reject(err)
}
}, MS_MAX_IDLE_DELAY)
)
})
}
if (process.env.NODE_ENV !== 'development') {
requestIdleCallback(() =>
setTimeout(() => {
if (!cancelled) {
reject(err)
}
}, MS_MAX_IDLE_DELAY)
)
}
})
}
...@@ -8,12 +8,16 @@ import { addLocale } from './add-locale' ...@@ -8,12 +8,16 @@ import { addLocale } from './add-locale'
import { isDynamicRoute } from '../shared/lib/router/utils/is-dynamic' import { isDynamicRoute } from '../shared/lib/router/utils/is-dynamic'
import { parseRelativeUrl } from '../shared/lib/router/utils/parse-relative-url' import { parseRelativeUrl } from '../shared/lib/router/utils/parse-relative-url'
import { removeTrailingSlash } from '../shared/lib/router/utils/remove-trailing-slash' import { removeTrailingSlash } from '../shared/lib/router/utils/remove-trailing-slash'
import { createRouteLoader, getClientBuildManifest } from './route-loader' import {
createRouteLoader,
getClientBuildManifest,
markAssetError,
} from './route-loader'
import { import {
DEV_CLIENT_PAGES_MANIFEST, DEV_CLIENT_PAGES_MANIFEST,
DEV_CLIENT_MIDDLEWARE_MANIFEST, DEV_CLIENT_MIDDLEWARE_MANIFEST,
TURBOPACK_CLIENT_MIDDLEWARE_MANIFEST,
} from '../shared/lib/constants' } from '../shared/lib/constants'
import { resolvePromiseWithTimeout } from './lib/promise'
declare global { declare global {
interface Window { interface Window {
...@@ -101,24 +105,24 @@ export default class PageLoader { ...@@ -101,24 +105,24 @@ export default class PageLoader {
if (window.__MIDDLEWARE_MATCHERS) { if (window.__MIDDLEWARE_MATCHERS) {
return window.__MIDDLEWARE_MATCHERS return window.__MIDDLEWARE_MATCHERS
} else { } else {
if (!this.promisedMiddlewareMatchers) { const onClientMiddlewareManifest = new Promise<ProxyMatcher[]>(
// TODO: Decide what should happen when fetching fails instead of asserting (resolve) => {
// @ts-ignore // Mandatory because this is not concurrent safe:
this.promisedMiddlewareMatchers = fetch( const cb = self.__MIDDLEWARE_MATCHERS_CB
`${this.assetPrefix}/_next/static/${this.buildId}/${TURBOPACK_CLIENT_MIDDLEWARE_MANIFEST}`, self.__MIDDLEWARE_MATCHERS_CB = () => {
{ credentials: 'same-origin' } resolve(self.__MIDDLEWARE_MATCHERS!)
) cb && cb()
.then((res) => res.json())
.then((matchers: ProxyMatcher[]) => {
window.__MIDDLEWARE_MATCHERS = matchers
return matchers
})
.catch((err) => {
console.log(`Failed to fetch _devMiddlewareManifest`, err)
})
} }
// TODO Remove this assertion as this could be undefined }
return this.promisedMiddlewareMatchers! )
return resolvePromiseWithTimeout(
onClientMiddlewareManifest,
markAssetError(
new Error('Failed to load client middleware manifest')
),
undefined
)
} }
// Development both Turbopack and Webpack // Development both Turbopack and Webpack
} else { } else {
......
...@@ -6,12 +6,7 @@ import { __unsafeCreateTrustedScriptURL } from './trusted-types' ...@@ -6,12 +6,7 @@ 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 { getDeploymentIdQueryOrEmptyString } 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'
// 3.8s was arbitrarily chosen as it's what https://web.dev/interactive
// considers as "Good" time-to-interactive. We must assume something went
// wrong beyond this point, and then fall-back to a full page transition to
// show the user something of value.
const MS_MAX_IDLE_DELAY = 3800
declare global { declare global {
interface Window { interface Window {
...@@ -19,7 +14,7 @@ declare global { ...@@ -19,7 +14,7 @@ declare global {
__BUILD_MANIFEST_CB?: Function __BUILD_MANIFEST_CB?: Function
__SERVER_FILES_MANIFEST?: RequiredServerFilesManifest __SERVER_FILES_MANIFEST?: RequiredServerFilesManifest
__MIDDLEWARE_MATCHERS?: ProxyMatcher[] __MIDDLEWARE_MATCHERS?: ProxyMatcher[]
__MIDDLEWARE_MANIFEST_CB?: Function __MIDDLEWARE_MATCHERS_CB?: Function
__REACT_LOADABLE_MANIFEST?: any __REACT_LOADABLE_MANIFEST?: any
__DYNAMIC_CSS_MANIFEST?: any __DYNAMIC_CSS_MANIFEST?: any
__RSC_MANIFEST?: any __RSC_MANIFEST?: any
...@@ -183,47 +178,6 @@ function appendScript( ...@@ -183,47 +178,6 @@ function appendScript(
// timeout to prevent an un-necessary hard navigation in development. // timeout to prevent an un-necessary hard navigation in development.
let devBuildPromise: Promise<void> | undefined let devBuildPromise: Promise<void> | undefined
// Resolve a promise that times out after given amount of milliseconds.
function resolvePromiseWithTimeout<T>(
p: Promise<T>,
ms: number,
err: Error
): Promise<T> {
return new Promise((resolve, reject) => {
let cancelled = false
p.then((r) => {
// Resolved, cancel the timeout
cancelled = true
resolve(r)
}).catch(reject)
// We wrap these checks separately for better dead-code elimination in
// production bundles.
if (process.env.NODE_ENV === 'development') {
;(devBuildPromise || Promise.resolve()).then(() => {
requestIdleCallback(() =>
setTimeout(() => {
if (!cancelled) {
reject(err)
}
}, ms)
)
})
}
if (process.env.NODE_ENV !== 'development') {
requestIdleCallback(() =>
setTimeout(() => {
if (!cancelled) {
reject(err)
}
}, ms)
)
}
})
}
// TODO: stop exporting or cache the failure // TODO: stop exporting or cache the failure
// It'd be best to stop exporting this. It's an implementation detail. We're // It'd be best to stop exporting this. It's an implementation detail. We're
// only exporting it for backwards compatibility with the `page-loader`. // only exporting it for backwards compatibility with the `page-loader`.
...@@ -246,8 +200,8 @@ export function getClientBuildManifest() { ...@@ -246,8 +200,8 @@ export function getClientBuildManifest() {
return resolvePromiseWithTimeout( return resolvePromiseWithTimeout(
onBuildManifest, onBuildManifest,
MS_MAX_IDLE_DELAY, markAssetError(new Error('Failed to load client build manifest')),
markAssetError(new Error('Failed to load client build manifest')) devBuildPromise
) )
} }
...@@ -405,8 +359,8 @@ export function createRouteLoader(assetPrefix: string): RouteLoader { ...@@ -405,8 +359,8 @@ export function createRouteLoader(assetPrefix: string): RouteLoader {
styles: res[1], styles: res[1],
})) }))
}), }),
MS_MAX_IDLE_DELAY, markAssetError(new Error(`Route did not complete loading: ${route}`)),
markAssetError(new Error(`Route did not complete loading: ${route}`)) devBuildPromise
) )
.then(({ entrypoint, styles }) => { .then(({ entrypoint, styles }) => {
const res: RouteLoaderEntry = Object.assign< const res: RouteLoaderEntry = Object.assign<
......
...@@ -323,6 +323,8 @@ export async function createHotReloaderTurbopack( ...@@ -323,6 +323,8 @@ export async function createHotReloaderTurbopack(
buildId, buildId,
distDir, distDir,
encryptionKey, encryptionKey,
dev: true,
deploymentId: nextConfig.deploymentId,
}) })
// Dev specific // Dev specific
......
...@@ -101,7 +101,7 @@ export const SERVER_FILES_MANIFEST = 'required-server-files' ...@@ -101,7 +101,7 @@ export const SERVER_FILES_MANIFEST = 'required-server-files'
export const DEV_CLIENT_PAGES_MANIFEST = '_devPagesManifest.json' export const DEV_CLIENT_PAGES_MANIFEST = '_devPagesManifest.json'
export const MIDDLEWARE_MANIFEST = 'middleware-manifest.json' export const MIDDLEWARE_MANIFEST = 'middleware-manifest.json'
export const TURBOPACK_CLIENT_MIDDLEWARE_MANIFEST = export const TURBOPACK_CLIENT_MIDDLEWARE_MANIFEST =
'_clientMiddlewareManifest.json' '_clientMiddlewareManifest.js'
export const TURBOPACK_CLIENT_BUILD_MANIFEST = 'client-build-manifest.json' export const TURBOPACK_CLIENT_BUILD_MANIFEST = 'client-build-manifest.json'
export const DEV_CLIENT_MIDDLEWARE_MANIFEST = '_devMiddlewareManifest.json' export const DEV_CLIENT_MIDDLEWARE_MANIFEST = '_devMiddlewareManifest.json'
export const REACT_LOADABLE_MANIFEST = 'react-loadable-manifest.json' export const REACT_LOADABLE_MANIFEST = 'react-loadable-manifest.json'
......
...@@ -17,6 +17,7 @@ import type { REACT_LOADABLE_MANIFEST } from '../constants' ...@@ -17,6 +17,7 @@ import type { REACT_LOADABLE_MANIFEST } from '../constants'
import { import {
APP_PATHS_MANIFEST, APP_PATHS_MANIFEST,
BUILD_MANIFEST, BUILD_MANIFEST,
CLIENT_STATIC_FILES_PATH,
INTERCEPTION_ROUTE_REWRITE_MANIFEST, INTERCEPTION_ROUTE_REWRITE_MANIFEST,
MIDDLEWARE_BUILD_MANIFEST, MIDDLEWARE_BUILD_MANIFEST,
MIDDLEWARE_MANIFEST, MIDDLEWARE_MANIFEST,
...@@ -201,19 +202,27 @@ export class TurbopackManifestLoader { ...@@ -201,19 +202,27 @@ export class TurbopackManifestLoader {
private readonly distDir: string private readonly distDir: string
private readonly buildId: string private readonly buildId: string
private readonly deploymentId: string
private readonly dev: boolean
constructor({ constructor({
distDir, distDir,
buildId, buildId,
encryptionKey, encryptionKey,
dev,
deploymentId,
}: { }: {
buildId: string buildId: string
distDir: string distDir: string
encryptionKey: string encryptionKey: string
dev: boolean
deploymentId: string
}) { }) {
this.distDir = distDir this.distDir = distDir
this.buildId = buildId this.buildId = buildId
this.encryptionKey = encryptionKey this.encryptionKey = encryptionKey
this.dev = dev
this.deploymentId = deploymentId
} }
delete(key: EntryKey) { delete(key: EntryKey) {
...@@ -438,7 +447,10 @@ export class TurbopackManifestLoader { ...@@ -438,7 +447,10 @@ export class TurbopackManifestLoader {
} }
} }
private mergeBuildManifests(manifests: Iterable<BuildManifest>) { private mergeBuildManifests(
manifests: Iterable<BuildManifest>,
lowPriorityFiles: string[]
) {
const manifest: Partial<BuildManifest> & Pick<BuildManifest, 'pages'> = { const manifest: Partial<BuildManifest> & Pick<BuildManifest, 'pages'> = {
pages: { pages: {
'/_app': [], '/_app': [],
...@@ -446,10 +458,7 @@ export class TurbopackManifestLoader { ...@@ -446,10 +458,7 @@ export class TurbopackManifestLoader {
// Something in next.js depends on these to exist even for app dir rendering // Something in next.js depends on these to exist even for app dir rendering
devFiles: [], devFiles: [],
polyfillFiles: [], polyfillFiles: [],
lowPriorityFiles: [ lowPriorityFiles,
`static/${this.buildId}/_ssgManifest.js`,
`static/${this.buildId}/_buildManifest.js`,
],
rootMainFiles: [], rootMainFiles: [],
} }
for (const m of manifests) { for (const m of manifests) {
...@@ -512,11 +521,14 @@ export class TurbopackManifestLoader { ...@@ -512,11 +521,14 @@ export class TurbopackManifestLoader {
) )
} }
private writeBuildManifest(): void { private writeBuildManifest(lowPriorityFiles: string[]): void {
if (!this.buildManifests.takeChanged()) { if (!this.buildManifests.takeChanged()) {
return return
} }
const buildManifest = this.mergeBuildManifests(this.buildManifests.values()) const buildManifest = this.mergeBuildManifests(
this.buildManifests.values(),
lowPriorityFiles
)
const buildManifestPath = join(this.distDir, BUILD_MANIFEST) const buildManifestPath = join(this.distDir, BUILD_MANIFEST)
const middlewareBuildManifestPath = join( const middlewareBuildManifestPath = join(
...@@ -530,8 +542,6 @@ export class TurbopackManifestLoader { ...@@ -530,8 +542,6 @@ export class TurbopackManifestLoader {
writeFileAtomic(buildManifestPath, JSON.stringify(buildManifest, null, 2)) writeFileAtomic(buildManifestPath, JSON.stringify(buildManifest, null, 2))
writeFileAtomic( writeFileAtomic(
middlewareBuildManifestPath, middlewareBuildManifestPath,
// we use globalThis here because middleware can be node
// which doesn't have "self"
createEdgeRuntimeManifest(buildManifest) createEdgeRuntimeManifest(buildManifest)
) )
...@@ -540,7 +550,8 @@ export class TurbopackManifestLoader { ...@@ -540,7 +550,8 @@ export class TurbopackManifestLoader {
[ [
this.buildManifests.get(getEntryKey('pages', 'server', '_app')), this.buildManifests.get(getEntryKey('pages', 'server', '_app')),
this.buildManifests.get(getEntryKey('pages', 'server', '_error')), this.buildManifests.get(getEntryKey('pages', 'server', '_error')),
].filter(Boolean) as BuildManifest[] ].filter(Boolean) as BuildManifest[],
lowPriorityFiles
) )
const fallbackBuildManifestPath = join( const fallbackBuildManifestPath = join(
this.distDir, this.distDir,
...@@ -557,7 +568,7 @@ export class TurbopackManifestLoader { ...@@ -557,7 +568,7 @@ export class TurbopackManifestLoader {
entrypoints: Entrypoints, entrypoints: Entrypoints,
devRewrites: SetupOpts['fsChecker']['rewrites'] | undefined, devRewrites: SetupOpts['fsChecker']['rewrites'] | undefined,
productionRewrites: CustomRoutes['rewrites'] | undefined productionRewrites: CustomRoutes['rewrites'] | undefined
): void { ): string[] {
const rewrites = normalizeRewritesForBuildManifest( const rewrites = normalizeRewritesForBuildManifest(
productionRewrites ?? { productionRewrites ?? {
...devRewrites, ...devRewrites,
...@@ -577,9 +588,33 @@ export class TurbopackManifestLoader { ...@@ -577,9 +588,33 @@ export class TurbopackManifestLoader {
const sortedPageKeys = getSortedRoutes(pagesKeys) const sortedPageKeys = getSortedRoutes(pagesKeys)
if (!this.clientBuildManifests.takeChanged({ rewrites, sortedPageKeys })) { let buildManifestPath
return let ssgManifestPath
if (this.deploymentId && !this.dev) {
// When skew protection is enabled, we instead just rely on the deployment id query string to
// load the correct manifests, to avoid the build id.
buildManifestPath = join(CLIENT_STATIC_FILES_PATH, '_buildManifest.js')
ssgManifestPath = join(CLIENT_STATIC_FILES_PATH, '_ssgManifest.js')
} else {
buildManifestPath = join(
CLIENT_STATIC_FILES_PATH,
this.buildId,
'_buildManifest.js'
)
ssgManifestPath = join(
CLIENT_STATIC_FILES_PATH,
this.buildId,
'_ssgManifest.js'
)
}
if (
this.dev &&
!this.clientBuildManifests.takeChanged({ rewrites, sortedPageKeys })
) {
return [buildManifestPath, ssgManifestPath]
} }
const clientBuildManifest = this.mergeClientBuildManifests( const clientBuildManifest = this.mergeClientBuildManifests(
this.clientBuildManifests.values(), this.clientBuildManifests.values(),
rewrites, rewrites,
...@@ -590,14 +625,16 @@ export class TurbopackManifestLoader { ...@@ -590,14 +625,16 @@ export class TurbopackManifestLoader {
null, null,
2 2
)};self.__BUILD_MANIFEST_CB && self.__BUILD_MANIFEST_CB()` )};self.__BUILD_MANIFEST_CB && self.__BUILD_MANIFEST_CB()`
writeFileAtomic( writeFileAtomic(
join(this.distDir, 'static', this.buildId, '_buildManifest.js'), join(this.distDir, buildManifestPath),
clientBuildManifestJs clientBuildManifestJs
) )
writeFileAtomic( // This is just an empty placeholder, the actual manifest is written after prerendering in
join(this.distDir, 'static', this.buildId, '_ssgManifest.js'), // packages/next/src/build/index.ts
srcEmptySsgManifest writeFileAtomic(join(this.distDir, ssgManifestPath), srcEmptySsgManifest)
)
return [buildManifestPath, ssgManifestPath]
} }
loadFontManifest(pageName: string, type: 'app' | 'pages' = 'pages'): void { loadFontManifest(pageName: string, type: 'app' | 'pages' = 'pages'): void {
...@@ -757,9 +794,22 @@ export class TurbopackManifestLoader { ...@@ -757,9 +794,22 @@ export class TurbopackManifestLoader {
return manifest return manifest
} }
private writeMiddlewareManifest(): void { private writeMiddlewareManifest(): {
if (!this.middlewareManifests.takeChanged()) { clientMiddlewareManifestPath: string
return } {
let clientMiddlewareManifestPath =
this.deploymentId && !this.dev
? join(CLIENT_STATIC_FILES_PATH, TURBOPACK_CLIENT_MIDDLEWARE_MANIFEST)
: join(
CLIENT_STATIC_FILES_PATH,
this.buildId,
TURBOPACK_CLIENT_MIDDLEWARE_MANIFEST
)
if (this.dev && !this.middlewareManifests.takeChanged()) {
return {
clientMiddlewareManifestPath,
}
} }
const middlewareManifest = this.mergeMiddlewareManifests( const middlewareManifest = this.mergeMiddlewareManifests(
this.middlewareManifests.values() this.middlewareManifests.values()
...@@ -791,20 +841,25 @@ export class TurbopackManifestLoader { ...@@ -791,20 +841,25 @@ export class TurbopackManifestLoader {
JSON.stringify(middlewareManifest, null, 2) JSON.stringify(middlewareManifest, null, 2)
) )
// Client middleware manifest // Client middleware manifest This is only used in dev though, packages/next/src/build/index.ts
// writes the mainfest again for builds.
const matchers = middlewareManifest?.middleware['/']?.matchers || [] const matchers = middlewareManifest?.middleware['/']?.matchers || []
const clientMiddlewareManifestPath = join( const clientMiddlewareManifestJs = `self.__MIDDLEWARE_MATCHERS = ${JSON.stringify(
this.distDir, matchers,
'static', null,
this.buildId, 2
`${TURBOPACK_CLIENT_MIDDLEWARE_MANIFEST}` )};self.__MIDDLEWARE_MATCHERS_CB && self.__MIDDLEWARE_MATCHERS_CB()`
)
deleteCache(clientMiddlewareManifestPath) deleteCache(clientMiddlewareManifestPath)
writeFileAtomic( writeFileAtomic(
clientMiddlewareManifestPath, join(this.distDir, clientMiddlewareManifestPath),
JSON.stringify(matchers, null, 2) clientMiddlewareManifestJs
) )
return {
clientMiddlewareManifestPath,
}
} }
loadPagesManifest(pageName: string): void { loadPagesManifest(pageName: string): void {
...@@ -843,10 +898,14 @@ export class TurbopackManifestLoader { ...@@ -843,10 +898,14 @@ export class TurbopackManifestLoader {
}): void { }): void {
this.writeActionManifest() this.writeActionManifest()
this.writeAppPathsManifest() this.writeAppPathsManifest()
this.writeBuildManifest() const lowPriorityFiles = this.writeClientBuildManifest(
entrypoints,
devRewrites,
productionRewrites
)
const { clientMiddlewareManifestPath } = this.writeMiddlewareManifest()
this.writeBuildManifest([...lowPriorityFiles, clientMiddlewareManifestPath])
this.writeInterceptionRouteRewriteManifest(devRewrites, productionRewrites) this.writeInterceptionRouteRewriteManifest(devRewrites, productionRewrites)
this.writeClientBuildManifest(entrypoints, devRewrites, productionRewrites)
this.writeMiddlewareManifest()
this.writeNextFontManifest() this.writeNextFontManifest()
this.writePagesManifest() this.writePagesManifest()
......
...@@ -35,7 +35,7 @@ export const expectedWhenTrailingSlashTrue = [ ...@@ -35,7 +35,7 @@ export const expectedWhenTrailingSlashTrue = [
expect.stringMatching(/_next\/static\/media\/test\.[0-9a-f]+\.png/), expect.stringMatching(/_next\/static\/media\/test\.[0-9a-f]+\.png/),
'_next/static/test-build-id/_buildManifest.js', '_next/static/test-build-id/_buildManifest.js',
...(process.env.IS_TURBOPACK_TEST ...(process.env.IS_TURBOPACK_TEST
? ['_next/static/test-build-id/_clientMiddlewareManifest.json'] ? ['_next/static/test-build-id/_clientMiddlewareManifest.js']
: []), : []),
'_next/static/test-build-id/_ssgManifest.js', '_next/static/test-build-id/_ssgManifest.js',
'_not-found/__next._full.txt', '_not-found/__next._full.txt',
...@@ -110,7 +110,7 @@ const expectedWhenTrailingSlashFalse = [ ...@@ -110,7 +110,7 @@ const expectedWhenTrailingSlashFalse = [
expect.stringMatching(/_next\/static\/media\/test\.[0-9a-f]+\.png/), expect.stringMatching(/_next\/static\/media\/test\.[0-9a-f]+\.png/),
'_next/static/test-build-id/_buildManifest.js', '_next/static/test-build-id/_buildManifest.js',
...(process.env.IS_TURBOPACK_TEST ...(process.env.IS_TURBOPACK_TEST
? ['_next/static/test-build-id/_clientMiddlewareManifest.json'] ? ['_next/static/test-build-id/_clientMiddlewareManifest.js']
: []), : []),
'_next/static/test-build-id/_ssgManifest.js', '_next/static/test-build-id/_ssgManifest.js',
'_not-found.html', '_not-found.html',
......
...@@ -7,7 +7,7 @@ import { diff } from 'jest-diff' ...@@ -7,7 +7,7 @@ import { diff } from 'jest-diff'
const glob = promisify(globOrig) const glob = promisify(globOrig)
// These are cosmetic files which aren't deployed. // These are cosmetic files which aren't deployed.
const IGNORE = /trace|trace-build/ const IGNORE = /^trace$|^trace-build$/
async function readFiles(next: NextInstance) { async function readFiles(next: NextInstance) {
const files = ( const files = (
...@@ -27,21 +27,33 @@ async function readFiles(next: NextInstance) { ...@@ -27,21 +27,33 @@ async function readFiles(next: NextInstance) {
) )
} }
// TODO we need to fix these case // TODO static/* browser chunks are content hashed and have the deployment id inlined
// - static/chunks client chunks are content hashed and have the deployment id inlined
const IGNORE_NAME = /^static\/chunks\// const IGNORE_NAME = /^static\/chunks\//
const IGNORE_CONTENT = new RegExp( const IGNORE_CONTENT = new RegExp(
[ [
// These contain content-hashed browser or edge chunk urls (including the deployment id query param) // TODO These contain content-hashed browser chunk urls (and/or the deployment id query param)
'page_client-reference-manifest\\.js',
'build-manifest\\.json',
// TODO This contains
// - content-hashed browser chunk urls (and/or the deployment id query param)
// - browser chunk urls inside of a folder named after the build id
'middleware-build-manifest\\.js',
// TODO this contains "env": { "__NEXT_BUILD_ID": "taBOOu8Znzobe4G7wEG_i",
'middleware-manifest\\.json',
// TODO this contains the build id
'BUILD_ID',
// TODO this contains the build id: "/pages-static-gsp": { "dataRoute": "/_next/data/V7oVUAlS1LiV5CqrtpkAL/pages-static-gsp.json",
'prerender-manifest\\.json',
// TODO These contain (but are not deployed to the serverless function itself)
// - content-hashed browser chunk urls
// - the build id
'.*\\.html', '.*\\.html',
'.*\\.rsc', '.*\\.rsc',
'page_client-reference-manifest\\.js', // These are not critical, as they aren't deployed to the serverless function itself
// These contain the content-hashed browser chunk names (but they might not actually be deployed in the serverless function)
'_buildManifest\\.js', '_buildManifest\\.js',
'build-manifest\\.json',
'client-build-manifest\\.json', 'client-build-manifest\\.json',
'fallback-build-manifest\\.json', 'fallback-build-manifest\\.json',
'middleware-build-manifest\\.js', 'routes-manifest\\.json',
] ]
.map((v) => '(?:\\/|^)' + v + '$') .map((v) => '(?:\\/|^)' + v + '$')
.join('|') .join('|')
...@@ -55,15 +67,18 @@ const IGNORE_CONTENT = new RegExp( ...@@ -55,15 +67,18 @@ const IGNORE_CONTENT = new RegExp(
files: { files: {
app: new FileRef(path.join(__dirname, 'app')), app: new FileRef(path.join(__dirname, 'app')),
pages: new FileRef(path.join(__dirname, 'pages')), pages: new FileRef(path.join(__dirname, 'pages')),
// TODO constant generateBuildId isn't entirely representative of the real world public: new FileRef(path.join(__dirname, 'public')),
'instrumentation.ts': new FileRef(
path.join(__dirname, 'instrumentation.ts')
),
'middleware.ts': new FileRef(path.join(__dirname, 'middleware.ts')),
'next.config.js': `module.exports = { 'next.config.js': `module.exports = {
generateBuildId: async () => 'default-build-id', experimental: {
// Enable these when debugging to get readable diffs // Enable these when debugging to get readable diffs
// experimental: {
// turbopackMinify: false, // turbopackMinify: false,
// turbopackModuleIds: 'named', // turbopackModuleIds: 'named',
// turbopackScopeHoisting: false, // turbopackScopeHoisting: false,
// }, },
}`, }`,
}, },
env: { env: {
...@@ -104,7 +119,15 @@ const IGNORE_CONTENT = new RegExp( ...@@ -104,7 +119,15 @@ const IGNORE_CONTENT = new RegExp(
if (content1 !== content2) { if (content1 !== content2) {
errors.push( errors.push(
`File content mismatch for ${fileName}\n\n` + `File content mismatch for ${fileName}\n\n` +
diff(content1, content2) diff(content1 ?? '', content2 ?? '')
)
}
}
for (const [fileName, content2] of run2Map) {
if (!run1Map.has(fileName)) {
errors.push(
`File content mismatch for ${fileName}\n\n` +
diff('', content2 ?? '')
) )
} }
} }
......
import { type Instrumentation } from 'next'
export const onRequestError: Instrumentation.onRequestError = (
err,
request,
context
) => {
console.error('onRequestError instrumentation called:', err)
}
import { NextRequest, NextResponse } from 'next/server'
const url = new URL('./public/vercel.png', import.meta.url).toString()
export async function middleware(req: NextRequest) {
if (req.nextUrl.toString().endsWith('/middleware')) {
return Response.json({ url })
}
return NextResponse.next()
}
...@@ -30,7 +30,8 @@ const nodeFilePaths = [ ...@@ -30,7 +30,8 @@ const nodeFilePaths = [
'app/force-dynamic/app-page/page', 'app/force-dynamic/app-page/page',
'app/force-dynamic/app-route/route', 'app/force-dynamic/app-route/route',
'pages/api/pages-api', 'pages/api/pages-api',
'pages/dynamic/pages-page', 'pages/pages-dynamic',
'pages/pages-static-gsp',
] ]
function getEdgeRouteFileHashes(next: NextInstance): RouteFileHashRecords { function getEdgeRouteFileHashes(next: NextInstance): RouteFileHashRecords {
...@@ -57,7 +58,8 @@ interface Runs { ...@@ -57,7 +58,8 @@ interface Runs {
run2: RouteFileHashRecords run2: RouteFileHashRecords
} }
describe('deterministic build - no-change build', () => { // TODO reenable later. This test was only ever correctly validating edge functions
describe.skip('deterministic build - no-change build', () => {
const { next } = nextTestSetup({ const { next } = nextTestSetup({
files: __dirname, files: __dirname,
skipStart: true, skipStart: true,
......
export default function Page() {
return 'pages-static-props'
}
export async function getStaticProps() {
return { props: { foo: 123 } }
}
export default function Page() {
return 'pages-static'
}
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