Skip to content

Commit 5d92a52

Browse files
committed
[fix] only validate next config once
1 parent 64702a9 commit 5d92a52

File tree

5 files changed

+22
-6
lines changed

5 files changed

+22
-6
lines changed

packages/next/src/build/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -849,6 +849,7 @@ export default async function build(
849849
loadConfig(PHASE_PRODUCTION_BUILD, dir, {
850850
// Log for next.config loading process
851851
silent: false,
852+
validateConfig: true,
852853
reactProductionProfiling,
853854
}),
854855
turborepoAccessTraceResult

packages/next/src/cli/next-dev.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,10 @@ const nextDev = async (
219219
// some set-ups that rely on listening on other interfaces
220220
const host = options.hostname
221221

222-
config = await loadConfig(PHASE_DEVELOPMENT_SERVER, dir)
222+
config = await loadConfig(PHASE_DEVELOPMENT_SERVER, dir, {
223+
validateConfig: true,
224+
silent: false,
225+
})
223226

224227
if (
225228
options.experimentalUploadTrace &&

packages/next/src/server/config.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1156,12 +1156,14 @@ export default async function loadConfig(
11561156
customConfig,
11571157
rawConfig,
11581158
silent = true,
1159+
validateConfig,
11591160
onLoadUserConfig,
11601161
reactProductionProfiling,
11611162
}: {
11621163
customConfig?: object | null
11631164
rawConfig?: boolean
11641165
silent?: boolean
1166+
validateConfig?: boolean
11651167
onLoadUserConfig?: (conf: NextConfig) => void
11661168
reactProductionProfiling?: boolean
11671169
} = {}
@@ -1272,13 +1274,16 @@ export default async function loadConfig(
12721274
userConfigModule.default || userConfigModule
12731275
)) as NextConfig
12741276

1275-
if (!process.env.NEXT_MINIMAL) {
1277+
// Only validate the config against schema in:
1278+
// * non minimal mode
1279+
// * when `loadConfig` result is not silent
1280+
if (!process.env.NEXT_MINIMAL && validateConfig) {
12761281
// We only validate the config against schema in non minimal mode
12771282
const { configSchema } =
12781283
require('./config-schema') as typeof import('./config-schema')
12791284
const state = configSchema.safeParse(userConfig)
12801285

1281-
if (state.success === false) {
1286+
if (!state.success) {
12821287
// error message header
12831288
const messages = [`Invalid ${configFileName} options detected: `]
12841289

packages/next/src/server/lib/router-server.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,8 +99,7 @@ export async function initialize(opts: {
9999

100100
const config = await loadConfig(
101101
opts.dev ? PHASE_DEVELOPMENT_SERVER : PHASE_PRODUCTION_SERVER,
102-
opts.dir,
103-
{ silent: false }
102+
opts.dir
104103
)
105104

106105
let compress: ReturnType<typeof setupCompression> | undefined

test/e2e/app-dir/metadata-streaming/metadata-streaming-customized-rule.test.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { nextTestSetup } from 'e2e-utils'
22

33
describe('app-dir - metadata-streaming-customized-rule', () => {
4-
const { next } = nextTestSetup({
4+
const { next, isNextDev } = nextTestSetup({
55
files: __dirname,
66
overrideFiles: {
77
'next.config.js': `
@@ -39,4 +39,12 @@ describe('app-dir - metadata-streaming-customized-rule', () => {
3939
expect(await $('head title').length).toBe(0)
4040
expect(await $('body title').length).toBe(1)
4141
})
42+
43+
if (isNextDev) {
44+
it('should not have schema issue', () => {
45+
expect(next.cliOutput).not.toContain(
46+
'Invalid next.config.js options detected'
47+
)
48+
})
49+
}
4250
})

0 commit comments

Comments
 (0)