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 @@
"976": "Decompressed resume data cache exceeded %s byte limit",
"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.",
"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(
buildId,
distDir,
locales,
deploymentId,
}: {
buildId: string
distDir: string
locales: readonly string[] | undefined
deploymentId: string
}
) {
const ssgPages = new Set<string>(
......@@ -573,8 +575,14 @@ async function writeClientSsgManifest(
ssgPages
)};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(
path.join(distDir, CLIENT_STATIC_FILES_PATH, buildId, '_ssgManifest.js'),
path.join(distDir, ssgManifestPath),
clientSsgManifestContent
)
}
......@@ -1712,6 +1720,8 @@ export default async function build(
)
}
// #region Compile
Log.info('Creating an optimized production build ...')
traceMemoryUsage('Starting build', nextBuildSpan)
......@@ -1881,6 +1891,8 @@ export default async function build(
})
}
// #endregion
// For app directory, we run type checking after build.
if (appDir && !isCompileMode && !isGenerateMode) {
await updateBuildDiagnostics({
......@@ -1890,6 +1902,7 @@ export default async function build(
traceMemoryUsage('Finished type checking', nextBuildSpan)
}
// #region required-server-files
const requiredServerFilesManifest = await nextBuildSpan
.traceChild('generate-required-server-files')
.traceAsyncFn(async () => {
......@@ -2070,6 +2083,9 @@ export default async function build(
requiredServerFilesManifest
)
// #endregion
// #region Collect data
const numberOfWorkers = getNumberOfWorkers(config)
const collectingPageDataStart = process.hrtime()
const postCompileSpinner = createSpinner(
......@@ -2697,14 +2713,26 @@ export default async function build(
}
if (bundler === Bundler.Turbopack) {
await writeManifest(
path.join(
distDir,
'static',
buildId,
TURBOPACK_CLIENT_MIDDLEWARE_MANIFEST
),
functionsConfigManifest.functions['/_middleware'].matchers || []
const clientMiddlewareManifestJs = `self.__MIDDLEWARE_MATCHERS = ${JSON.stringify(
functionsConfigManifest.functions['/_middleware'].matchers || [],
null,
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,
TURBOPACK_CLIENT_MIDDLEWARE_MANIFEST
)
await writeFileUtf8(
path.join(distDir, clientMiddlewareManifestPath),
clientMiddlewareManifestJs
)
}
}
......@@ -2712,6 +2740,9 @@ export default async function build(
await writeFunctionsConfigManifest(distDir, functionsConfigManifest)
// #endregion
// #region NFT
if (
bundler !== Bundler.Turbopack &&
!isGenerateMode &&
......@@ -2869,6 +2900,9 @@ export default async function build(
const hasGSPAndRevalidateZero = new Set<string>()
// #endregion
// #region SSG
// we need to trigger automatic exporting when we have
// - static 404/500
// - getStaticProps paths
......@@ -4045,6 +4079,7 @@ export default async function build(
distDir,
buildId,
locales: config.i18n?.locales,
deploymentId: config.deploymentId,
})
} else {
await writePrerenderManifest(distDir, {
......@@ -4056,6 +4091,8 @@ export default async function build(
})
}
// #endregion
await writeImagesManifest(distDir, config)
await writeManifest(path.join(distDir, EXPORT_MARKER), {
version: 1,
......
......@@ -100,9 +100,11 @@ export async function turbopackBuild(): Promise<{
await fs.writeFile(path.join(distDir, 'turbopack'), '')
await fs.mkdir(path.join(distDir, 'server'), { recursive: true })
await fs.mkdir(path.join(distDir, 'static', buildId), {
recursive: true,
})
if (!config.deploymentId) {
await fs.mkdir(path.join(distDir, 'static', buildId), {
recursive: true,
})
}
await fs.writeFile(
path.join(distDir, 'package.json'),
'{"type": "commonjs"}'
......@@ -134,6 +136,8 @@ export async function turbopackBuild(): Promise<{
buildId,
distDir,
encryptionKey,
dev: false,
deploymentId: config.deploymentId,
})
const currentEntrypoints = await rawEntrypointsToEntrypoints(
......
......@@ -2115,6 +2115,8 @@ export default async function getBaseWebpackConfig(
isClient &&
new BuildManifestPlugin({
buildId,
dev,
deploymentId: config.deploymentId,
rewrites,
isDevFallback,
appDirEnabled: hasAppDir,
......
......@@ -50,32 +50,9 @@ export function normalizeRewritesForBuildManifest(
}
export function createEdgeRuntimeManifest(
originAssetMap: Partial<BuildManifest>
assetMap: Partial<BuildManifest>
): string {
const manifestFilenames = ['_buildManifest.js', '_ssgManifest.js']
const assetMap: Partial<BuildManifest> = {
...originAssetMap,
lowPriorityFiles: [],
}
// we use globalThis here because middleware can be node
// which doesn't have "self"
const manifestDefCode = `globalThis.__BUILD_MANIFEST = ${JSON.stringify(
assetMap,
null,
2
)};\n`
// edge lowPriorityFiles item: '"/static/" + process.env.__NEXT_BUILD_ID + "/low-priority.js"'.
// Since lowPriorityFiles is not fixed and relying on `process.env.__NEXT_BUILD_ID`, we'll produce code creating it dynamically.
const lowPriorityFilesCode =
`globalThis.__BUILD_MANIFEST.lowPriorityFiles = [\n` +
manifestFilenames
.map((filename) => {
return `"/static/" + process.env.__NEXT_BUILD_ID + "/${filename}"`
})
.join(',\n') +
`\n];`
return manifestDefCode + lowPriorityFilesCode
return `globalThis.__BUILD_MANIFEST = ${JSON.stringify(assetMap, null, 2)};\n`
}
......@@ -28,8 +28,19 @@ import {
type DeepMutable<T> = { -readonly [P in keyof T]: DeepMutable<T[P]> }
// nodejs: '/static/<build id>/low-priority.js'
function buildNodejsLowPriorityPath(filename: string, buildId: string) {
return `${CLIENT_STATIC_FILES_PATH}/${buildId}/${filename}`
// 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}`
}
}
// This function takes the asset map generated in BuildManifestPlugin and creates a
......@@ -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
export default class BuildManifestPlugin {
private buildId: string
private dev: boolean
private deploymentId: string
private rewrites: CustomRoutes['rewrites']
private isDevFallback: boolean
private appDirEnabled: boolean
......@@ -109,12 +122,16 @@ export default class BuildManifestPlugin {
constructor(options: {
buildId: string
dev: boolean
deploymentId: string
rewrites: CustomRoutes['rewrites']
isDevFallback?: boolean
appDirEnabled: boolean
clientRouterFilters?: Parameters<typeof generateClientManifest>[2]
}) {
this.buildId = options.buildId
this.dev = options.dev
this.deploymentId = options.deploymentId
this.isDevFallback = !!options.isDevFallback
this.rewrites = {
beforeFiles: [],
......@@ -206,11 +223,15 @@ export default class BuildManifestPlugin {
// downloaded by the client.
const buildManifestPath = buildNodejsLowPriorityPath(
'_buildManifest.js',
this.buildId
this.buildId,
this.dev,
this.deploymentId
)
const ssgManifestPath = buildNodejsLowPriorityPath(
'_ssgManifest.js',
this.buildId
this.buildId,
this.dev,
this.deploymentId
)
assetMap.lowPriorityFiles.push(buildManifestPath, ssgManifestPath)
compilation.emitAsset(
......@@ -244,8 +265,14 @@ export default class BuildManifestPlugin {
)
if (!this.isDevFallback) {
const buildManifestPath = buildNodejsLowPriorityPath(
'_buildManifest.js',
this.buildId,
this.dev,
this.deploymentId
)
compilation.emitAsset(
`${CLIENT_STATIC_FILES_PATH}/${this.buildId}/_buildManifest.js`,
buildManifestPath,
new sources.RawSource(
`self.__BUILD_MANIFEST = ${generateClientManifest(
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'
import { isDynamicRoute } from '../shared/lib/router/utils/is-dynamic'
import { parseRelativeUrl } from '../shared/lib/router/utils/parse-relative-url'
import { removeTrailingSlash } from '../shared/lib/router/utils/remove-trailing-slash'
import { createRouteLoader, getClientBuildManifest } from './route-loader'
import {
createRouteLoader,
getClientBuildManifest,
markAssetError,
} from './route-loader'
import {
DEV_CLIENT_PAGES_MANIFEST,
DEV_CLIENT_MIDDLEWARE_MANIFEST,
TURBOPACK_CLIENT_MIDDLEWARE_MANIFEST,
} from '../shared/lib/constants'
import { resolvePromiseWithTimeout } from './lib/promise'
declare global {
interface Window {
......@@ -101,24 +105,24 @@ export default class PageLoader {
if (window.__MIDDLEWARE_MATCHERS) {
return window.__MIDDLEWARE_MATCHERS
} else {
if (!this.promisedMiddlewareMatchers) {
// TODO: Decide what should happen when fetching fails instead of asserting
// @ts-ignore
this.promisedMiddlewareMatchers = fetch(
`${this.assetPrefix}/_next/static/${this.buildId}/${TURBOPACK_CLIENT_MIDDLEWARE_MANIFEST}`,
{ credentials: 'same-origin' }
)
.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!
const onClientMiddlewareManifest = new Promise<ProxyMatcher[]>(
(resolve) => {
// Mandatory because this is not concurrent safe:
const cb = self.__MIDDLEWARE_MATCHERS_CB
self.__MIDDLEWARE_MATCHERS_CB = () => {
resolve(self.__MIDDLEWARE_MATCHERS!)
cb && cb()
}
}
)
return resolvePromiseWithTimeout(
onClientMiddlewareManifest,
markAssetError(
new Error('Failed to load client middleware manifest')
),
undefined
)
}
// Development both Turbopack and Webpack
} else {
......
......@@ -6,12 +6,7 @@ import { __unsafeCreateTrustedScriptURL } from './trusted-types'
import { requestIdleCallback } from './request-idle-callback'
import { getDeploymentIdQueryOrEmptyString } from '../shared/lib/deployment-id'
import { encodeURIPath } from '../shared/lib/encode-uri-path'
// 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
import { resolvePromiseWithTimeout } from './lib/promise'
declare global {
interface Window {
......@@ -19,7 +14,7 @@ declare global {
__BUILD_MANIFEST_CB?: Function
__SERVER_FILES_MANIFEST?: RequiredServerFilesManifest
__MIDDLEWARE_MATCHERS?: ProxyMatcher[]
__MIDDLEWARE_MANIFEST_CB?: Function
__MIDDLEWARE_MATCHERS_CB?: Function
__REACT_LOADABLE_MANIFEST?: any
__DYNAMIC_CSS_MANIFEST?: any
__RSC_MANIFEST?: any
......@@ -183,47 +178,6 @@ function appendScript(
// timeout to prevent an un-necessary hard navigation in development.
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
// 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`.
......@@ -246,8 +200,8 @@ export function getClientBuildManifest() {
return resolvePromiseWithTimeout(
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 {
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 }) => {
const res: RouteLoaderEntry = Object.assign<
......
......@@ -323,6 +323,8 @@ export async function createHotReloaderTurbopack(
buildId,
distDir,
encryptionKey,
dev: true,
deploymentId: nextConfig.deploymentId,
})
// Dev specific
......
......@@ -101,7 +101,7 @@ export const SERVER_FILES_MANIFEST = 'required-server-files'
export const DEV_CLIENT_PAGES_MANIFEST = '_devPagesManifest.json'
export const MIDDLEWARE_MANIFEST = 'middleware-manifest.json'
export const TURBOPACK_CLIENT_MIDDLEWARE_MANIFEST =
'_clientMiddlewareManifest.json'
'_clientMiddlewareManifest.js'
export const TURBOPACK_CLIENT_BUILD_MANIFEST = 'client-build-manifest.json'
export const DEV_CLIENT_MIDDLEWARE_MANIFEST = '_devMiddlewareManifest.json'
export const REACT_LOADABLE_MANIFEST = 'react-loadable-manifest.json'
......
......@@ -17,6 +17,7 @@ import type { REACT_LOADABLE_MANIFEST } from '../constants'
import {
APP_PATHS_MANIFEST,
BUILD_MANIFEST,
CLIENT_STATIC_FILES_PATH,
INTERCEPTION_ROUTE_REWRITE_MANIFEST,
MIDDLEWARE_BUILD_MANIFEST,
MIDDLEWARE_MANIFEST,
......@@ -201,19 +202,27 @@ export class TurbopackManifestLoader {
private readonly distDir: string
private readonly buildId: string
private readonly deploymentId: string
private readonly dev: boolean
constructor({
distDir,
buildId,
encryptionKey,
dev,
deploymentId,
}: {
buildId: string
distDir: string
encryptionKey: string
dev: boolean
deploymentId: string
}) {
this.distDir = distDir
this.buildId = buildId
this.encryptionKey = encryptionKey
this.dev = dev
this.deploymentId = deploymentId
}
delete(key: EntryKey) {
......@@ -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'> = {
pages: {
'/_app': [],
......@@ -446,10 +458,7 @@ export class TurbopackManifestLoader {
// Something in next.js depends on these to exist even for app dir rendering
devFiles: [],
polyfillFiles: [],
lowPriorityFiles: [
`static/${this.buildId}/_ssgManifest.js`,
`static/${this.buildId}/_buildManifest.js`,
],
lowPriorityFiles,
rootMainFiles: [],
}
for (const m of manifests) {
......@@ -512,11 +521,14 @@ export class TurbopackManifestLoader {
)
}
private writeBuildManifest(): void {
private writeBuildManifest(lowPriorityFiles: string[]): void {
if (!this.buildManifests.takeChanged()) {
return
}
const buildManifest = this.mergeBuildManifests(this.buildManifests.values())
const buildManifest = this.mergeBuildManifests(
this.buildManifests.values(),
lowPriorityFiles
)
const buildManifestPath = join(this.distDir, BUILD_MANIFEST)
const middlewareBuildManifestPath = join(
......@@ -530,8 +542,6 @@ export class TurbopackManifestLoader {
writeFileAtomic(buildManifestPath, JSON.stringify(buildManifest, null, 2))
writeFileAtomic(
middlewareBuildManifestPath,
// we use globalThis here because middleware can be node
// which doesn't have "self"
createEdgeRuntimeManifest(buildManifest)
)
......@@ -540,7 +550,8 @@ export class TurbopackManifestLoader {
[
this.buildManifests.get(getEntryKey('pages', 'server', '_app')),
this.buildManifests.get(getEntryKey('pages', 'server', '_error')),
].filter(Boolean) as BuildManifest[]
].filter(Boolean) as BuildManifest[],
lowPriorityFiles
)
const fallbackBuildManifestPath = join(
this.distDir,
......@@ -557,7 +568,7 @@ export class TurbopackManifestLoader {
entrypoints: Entrypoints,
devRewrites: SetupOpts['fsChecker']['rewrites'] | undefined,
productionRewrites: CustomRoutes['rewrites'] | undefined
): void {
): string[] {
const rewrites = normalizeRewritesForBuildManifest(
productionRewrites ?? {
...devRewrites,
......@@ -577,9 +588,33 @@ export class TurbopackManifestLoader {
const sortedPageKeys = getSortedRoutes(pagesKeys)
if (!this.clientBuildManifests.takeChanged({ rewrites, sortedPageKeys })) {
return
let buildManifestPath
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(
this.clientBuildManifests.values(),
rewrites,
......@@ -590,14 +625,16 @@ export class TurbopackManifestLoader {
null,
2
)};self.__BUILD_MANIFEST_CB && self.__BUILD_MANIFEST_CB()`
writeFileAtomic(
join(this.distDir, 'static', this.buildId, '_buildManifest.js'),
join(this.distDir, buildManifestPath),
clientBuildManifestJs
)
writeFileAtomic(
join(this.distDir, 'static', this.buildId, '_ssgManifest.js'),
srcEmptySsgManifest
)
// This is just an empty placeholder, the actual manifest is written after prerendering in
// packages/next/src/build/index.ts
writeFileAtomic(join(this.distDir, ssgManifestPath), srcEmptySsgManifest)
return [buildManifestPath, ssgManifestPath]
}
loadFontManifest(pageName: string, type: 'app' | 'pages' = 'pages'): void {
......@@ -757,9 +794,22 @@ export class TurbopackManifestLoader {
return manifest
}
private writeMiddlewareManifest(): void {
if (!this.middlewareManifests.takeChanged()) {
return
private writeMiddlewareManifest(): {
clientMiddlewareManifestPath: string
} {
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(
this.middlewareManifests.values()
......@@ -791,20 +841,25 @@ export class TurbopackManifestLoader {
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 clientMiddlewareManifestPath = join(
this.distDir,
'static',
this.buildId,
`${TURBOPACK_CLIENT_MIDDLEWARE_MANIFEST}`
)
const clientMiddlewareManifestJs = `self.__MIDDLEWARE_MATCHERS = ${JSON.stringify(
matchers,
null,
2
)};self.__MIDDLEWARE_MATCHERS_CB && self.__MIDDLEWARE_MATCHERS_CB()`
deleteCache(clientMiddlewareManifestPath)
writeFileAtomic(
clientMiddlewareManifestPath,
JSON.stringify(matchers, null, 2)
join(this.distDir, clientMiddlewareManifestPath),
clientMiddlewareManifestJs
)
return {
clientMiddlewareManifestPath,
}
}
loadPagesManifest(pageName: string): void {
......@@ -843,10 +898,14 @@ export class TurbopackManifestLoader {
}): void {
this.writeActionManifest()
this.writeAppPathsManifest()
this.writeBuildManifest()
const lowPriorityFiles = this.writeClientBuildManifest(
entrypoints,
devRewrites,
productionRewrites
)
const { clientMiddlewareManifestPath } = this.writeMiddlewareManifest()
this.writeBuildManifest([...lowPriorityFiles, clientMiddlewareManifestPath])
this.writeInterceptionRouteRewriteManifest(devRewrites, productionRewrites)
this.writeClientBuildManifest(entrypoints, devRewrites, productionRewrites)
this.writeMiddlewareManifest()
this.writeNextFontManifest()
this.writePagesManifest()
......
......@@ -35,7 +35,7 @@ export const expectedWhenTrailingSlashTrue = [
expect.stringMatching(/_next\/static\/media\/test\.[0-9a-f]+\.png/),
'_next/static/test-build-id/_buildManifest.js',
...(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',
'_not-found/__next._full.txt',
......@@ -110,7 +110,7 @@ const expectedWhenTrailingSlashFalse = [
expect.stringMatching(/_next\/static\/media\/test\.[0-9a-f]+\.png/),
'_next/static/test-build-id/_buildManifest.js',
...(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',
'_not-found.html',
......
......@@ -7,7 +7,7 @@ import { diff } from 'jest-diff'
const glob = promisify(globOrig)
// These are cosmetic files which aren't deployed.
const IGNORE = /trace|trace-build/
const IGNORE = /^trace$|^trace-build$/
async function readFiles(next: NextInstance) {
const files = (
......@@ -27,21 +27,33 @@ async function readFiles(next: NextInstance) {
)
}
// TODO we need to fix these case
// - static/chunks client chunks are content hashed and have the deployment id inlined
// TODO static/* browser chunks are content hashed and have the deployment id inlined
const IGNORE_NAME = /^static\/chunks\//
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',
'.*\\.rsc',
'page_client-reference-manifest\\.js',
// These contain the content-hashed browser chunk names (but they might not actually be deployed in the serverless function)
// These are not critical, as they aren't deployed to the serverless function itself
'_buildManifest\\.js',
'build-manifest\\.json',
'client-build-manifest\\.json',
'fallback-build-manifest\\.json',
'middleware-build-manifest\\.js',
'routes-manifest\\.json',
]
.map((v) => '(?:\\/|^)' + v + '$')
.join('|')
......@@ -55,16 +67,19 @@ const IGNORE_CONTENT = new RegExp(
files: {
app: new FileRef(path.join(__dirname, 'app')),
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 = {
generateBuildId: async () => 'default-build-id',
// Enable these when debugging to get readable diffs
// experimental: {
// turbopackMinify: false,
// turbopackModuleIds: 'named',
// turbopackScopeHoisting: false,
// },
}`,
experimental: {
// Enable these when debugging to get readable diffs
// turbopackMinify: false,
// turbopackModuleIds: 'named',
// turbopackScopeHoisting: false,
},
}`,
},
env: {
NOW_BUILDER: '1',
......@@ -104,7 +119,15 @@ const IGNORE_CONTENT = new RegExp(
if (content1 !== content2) {
errors.push(
`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 = [
'app/force-dynamic/app-page/page',
'app/force-dynamic/app-route/route',
'pages/api/pages-api',
'pages/dynamic/pages-page',
'pages/pages-dynamic',
'pages/pages-static-gsp',
]
function getEdgeRouteFileHashes(next: NextInstance): RouteFileHashRecords {
......@@ -57,7 +58,8 @@ interface Runs {
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({
files: __dirname,
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