Unverified Commit 7498fd4b authored by JJ Kasper's avatar JJ Kasper Committed by GitHub

Restructure trace experiment manifest (#68131)

This restructures the experimental trace manifest to output the values
instead of just hashes for better debugging experience and removes
public env from the manifest for now.
parent ab8e395d
......@@ -3,7 +3,7 @@ import path from 'path'
import crypto from 'crypto'
import { getPageFromPath } from '../entries'
import { Sema } from 'next/dist/compiled/async-sema'
import { generateShuttleManifest } from './store-shuttle'
import { generateShuttleManifest, type ShuttleManifest } from './store-shuttle'
import type { NextConfigComplete } from '../../server/config-shared'
export interface DetectedEntriesResult {
......@@ -11,6 +11,32 @@ export interface DetectedEntriesResult {
pages: string[]
}
function deepEqual(obj1: any, obj2: any) {
if (obj1 === obj2) return true
if (
typeof obj1 !== 'object' ||
obj1 === null ||
typeof obj2 !== 'object' ||
obj2 === null
) {
return false
}
let keys1 = Object.keys(obj1)
let keys2 = Object.keys(obj2)
if (keys1.length !== keys2.length) return false
for (let key of keys1) {
if (!keys2.includes(key) || !deepEqual(obj1[key], obj2[key])) {
return false
}
}
return true
}
let _hasShuttle: undefined | boolean = undefined
export async function hasShuttle(
config: NextConfigComplete,
......@@ -19,44 +45,41 @@ export async function hasShuttle(
if (typeof _hasShuttle === 'boolean') {
return _hasShuttle
}
let foundShuttleManifest:
| ReturnType<typeof generateShuttleManifest>
| undefined
let foundShuttleManifest: ShuttleManifest
try {
foundShuttleManifest = JSON.parse(
await fs.promises
.readFile(path.join(shuttleDir, 'shuttle-manifest.json'), 'utf8')
.catch(() => '{}')
await fs.promises.readFile(
path.join(shuttleDir, 'shuttle-manifest.json'),
'utf8'
)
)
_hasShuttle = true
} catch (err: unknown) {
_hasShuttle = false
console.log(`Failed to read shuttle manifest`)
return _hasShuttle
}
const currentShuttleManifest = generateShuttleManifest(config)
const currentShuttleManifest = JSON.parse(generateShuttleManifest(config))
if (
currentShuttleManifest.nextVersion !== foundShuttleManifest?.nextVersion
) {
if (currentShuttleManifest.nextVersion !== foundShuttleManifest.nextVersion) {
// we don't allow using shuttle from differing Next.js version
// as it could have internal changes
console.log(
`shuttle has differing Next.js version ${foundShuttleManifest?.nextVersion} versus current ${currentShuttleManifest.nextVersion}, skipping.`
`shuttle has differing Next.js version ${foundShuttleManifest.nextVersion} versus current ${currentShuttleManifest.nextVersion}, skipping.`
)
_hasShuttle = false
}
if (
_hasShuttle !== false &&
currentShuttleManifest.globalHash !== foundShuttleManifest?.globalHash
) {
// if the global hash was invalidated we bypass the cache to be safe
if (!deepEqual(currentShuttleManifest.config, foundShuttleManifest.config)) {
_hasShuttle = false
console.log(
`shuttle has differing global hash ${foundShuttleManifest?.globalHash} versus current ${currentShuttleManifest.globalHash}, skipping.`
`Mismatching shuttle configs`,
currentShuttleManifest.config,
foundShuttleManifest.config
)
_hasShuttle = false
}
return _hasShuttle
}
......
import fs from 'fs'
import path from 'path'
import crypto from 'crypto'
import { recursiveCopy } from '../../lib/recursive-copy'
import { version as nextVersion } from 'next/package.json'
import type { NextConfigComplete } from '../../server/config-shared'
......@@ -12,58 +11,31 @@ import {
PAGES_MANIFEST,
ROUTES_MANIFEST,
} from '../../shared/lib/constants'
import { getNextPublicEnvironmentVariables } from '../webpack/plugins/define-env-plugin'
export function generateShuttleManifest(config: NextConfigComplete) {
// NEXT_PUBLIC_ changes for now since they are inlined
// and specific next config values that can impact the build
const globalHash = crypto.createHash('sha256')
const nextPublicEnv = getNextPublicEnvironmentVariables()
// sort for deterministic order
const publicEnvKeys = Object.keys(nextPublicEnv).sort()
for (const key of publicEnvKeys) {
globalHash.update(`${key}=${nextPublicEnv[key]}`)
}
// TODO: make this opt-out list instead?
// also ensure this list is complete this is minimal set
const configsToInvalidateOn = [
'basePath',
'env',
'i18n',
'images',
'productionBrowserSourceMaps',
'webpack',
'sassOptions',
'trailingSlash',
'experimental.flyingShuttle',
'experimental.ppr',
'experimental.reactCompiler',
].sort()
for (const key of configsToInvalidateOn) {
let value = config[key]
if (key.includes('.')) {
value = config
const keyParts = key.split('.')
for (let i = 0; i < keyParts.length; i++) {
value = value[keyParts[i]]
}
}
let serializedConfig =
typeof value === 'function' ? value.toString() : JSON.stringify(value)
globalHash.update(`${key}=${serializedConfig}`)
}
export interface ShuttleManifest {
nextVersion: string
config: Record<string, any>
}
return {
export function generateShuttleManifest(config: NextConfigComplete) {
return JSON.stringify({
nextVersion,
globalHash: globalHash.digest('hex'),
}
config: {
env: config.env,
i18n: config.i18n,
basePath: config.basePath,
sassOptions: config.sassOptions,
trailingSlash: config.trailingSlash,
productionBrowserSourceMaps: config.productionBrowserSourceMaps,
experimental: {
ppr: config.experimental.ppr,
reactCompiler: config.experimental.reactCompiler,
serverSourceMaps: config.experimental.serverSourceMaps,
serverMinification: config.experimental.serverMinification,
},
},
} satisfies ShuttleManifest)
}
// we can create a new shuttle with the outputs before env values have
......@@ -80,10 +52,9 @@ export async function storeShuttle({
await fs.promises.rm(shuttleDir, { force: true, recursive: true })
await fs.promises.mkdir(shuttleDir, { recursive: true })
const shuttleManifest = generateShuttleManifest(config)
await fs.promises.writeFile(
path.join(shuttleDir, 'shuttle-manifest.json'),
JSON.stringify(shuttleManifest)
generateShuttleManifest(config)
)
// copy all server entries
......
......@@ -23,13 +23,13 @@ import { nextTestSetup, isNextStart } from 'e2e-utils'
NEXT_PRIVATE_FLYING_SHUTTLE: 'true',
},
})
let initialGlobalHash: string = ''
let initialConfig: Record<string, any> = {}
beforeAll(async () => {
const manifest = await next.readJSON(
'.next/cache/shuttle/shuttle-manifest.json'
)
initialGlobalHash = manifest.globalHash
initialConfig = manifest.config
})
async function checkShuttleManifest() {
......@@ -39,7 +39,7 @@ import { nextTestSetup, isNextStart } from 'e2e-utils'
expect(manifest).toEqual({
nextVersion,
globalHash: initialGlobalHash,
config: initialConfig,
})
}
......@@ -93,17 +93,6 @@ import { nextTestSetup, isNextStart } from 'e2e-utils'
}
})
it('should have shuttle-manifest.json', async () => {
const manifest = await next.readJSON(
'.next/cache/shuttle/shuttle-manifest.json'
)
expect(manifest).toEqual({
nextVersion,
globalHash: expect.stringMatching(/[\w\d]{1,}/),
})
})
it('should hard navigate on chunk load failure', async () => {
let blockChunks = false
const browser = await next.browser('/dashboard', {
......
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