Files
Jiachi Liu 0a45e33848 [perf] cache load config results (#80570)
This PR is a perf improvement that we only need to load Next.js config once per process. For config schema validation we also only execute once. I noticed that config-schema is executed 3 times, in the 1st root process, and twice when the router-server load configs.

* For `loadConfig` it needs module loading, config parsing, save the expensive effort to only execute once
* Always validate config schema if possible and don't be silent, but only do it once is the parent process. We use `process.send` to check if it's in the root or forked process.
2025-07-25 10:43:18 +02:00

55 lines
1.7 KiB
TypeScript

import stripAnsi from 'strip-ansi'
import { nextTestSetup } from 'e2e-utils'
describe('config validation - validation only runs once', () => {
const { next } = nextTestSetup({
files: {
'pages/index.js': `
export default function Page() {
return <p>hello world</p>
}
`,
'next.config.js': `
module.exports = {
invalidOption: 'shouldTriggerValidation',
anotherBadKey: 'anotherBadValue'
}
`,
},
})
it('should validate config only once in root process', async () => {
await next.fetch('/')
const output = stripAnsi(next.cliOutput)
const validationHeaderMatches = output.match(
/Invalid next\.config\.js options detected:/g
)
const validationHeaderCount = validationHeaderMatches
? validationHeaderMatches.length
: 0
// Count occurrences of specific invalid option mentions
const invalidOptionMatches = output.match(/invalidOption/g)
const invalidOptionCount = invalidOptionMatches
? invalidOptionMatches.length
: 0
const anotherBadKeyMatches = output.match(/anotherBadKey/g)
const anotherBadKeyCount = anotherBadKeyMatches
? anotherBadKeyMatches.length
: 0
// Expect validation to have occurred
expect(output).toContain('Invalid next.config.js options detected')
expect(output).toContain('invalidOption')
expect(output).toContain('anotherBadKey')
// Expect validation header to appear only once (not multiple times from different processes)
expect(validationHeaderCount).toBe(1)
// Each invalid option should also appear only once in the validation output
expect(invalidOptionCount).toBe(1)
expect(anotherBadKeyCount).toBe(1)
})
})