Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in
Toggle navigation
N
next.js
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Alex
next.js
Commits
df5bc544
Unverified
Commit
df5bc544
authored
Aug 20, 2025
by
Jiachi Liu
Committed by
GitHub
Aug 20, 2025
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
[perf] only load next config once when start next dev server (#82654)
parent
ff399348
Hide whitespace changes
Inline
Side-by-side
Showing
8 changed files
with
128 additions
and
64 deletions
+128
-64
next-dev.ts
packages/next/src/cli/next-dev.ts
+21
-19
find-root.ts
packages/next/src/lib/find-root.ts
+17
-6
config.ts
packages/next/src/server/config.ts
+55
-39
app-info-log.ts
packages/next/src/server/lib/app-info-log.ts
+1
-0
layout.js
test/development/load-config-freq/app/layout.js
+7
-0
page.js
test/development/load-config-freq/app/page.js
+3
-0
load-config-freq.test.ts
test/development/load-config-freq/load-config-freq.test.ts
+16
-0
next.config.js
test/development/load-config-freq/next.config.js
+8
-0
No files found.
packages/next/src/cli/next-dev.ts
View file @
df5bc544
...
@@ -17,7 +17,7 @@ import { getProjectDir } from '../lib/get-project-dir'
...
@@ -17,7 +17,7 @@ import { getProjectDir } from '../lib/get-project-dir'
import
{
PHASE_DEVELOPMENT_SERVER
}
from
'../shared/lib/constants'
import
{
PHASE_DEVELOPMENT_SERVER
}
from
'../shared/lib/constants'
import
path
from
'path'
import
path
from
'path'
import
type
{
NextConfigComplete
}
from
'../server/config-shared'
import
type
{
NextConfigComplete
}
from
'../server/config-shared'
import
{
setGlobal
,
traceGlobals
}
from
'../trace/shared'
import
{
traceGlobals
}
from
'../trace/shared'
import
{
Telemetry
}
from
'../telemetry/storage'
import
{
Telemetry
}
from
'../telemetry/storage'
import
loadConfig
from
'../server/config'
import
loadConfig
from
'../server/config'
import
{
findPagesDir
}
from
'../lib/find-pages-dir'
import
{
findPagesDir
}
from
'../lib/find-pages-dir'
...
@@ -56,6 +56,7 @@ type PortSource = 'cli' | 'default' | 'env'
...
@@ -56,6 +56,7 @@ type PortSource = 'cli' | 'default' | 'env'
let
dir
:
string
let
dir
:
string
let
child
:
undefined
|
ChildProcess
let
child
:
undefined
|
ChildProcess
// The config in next-dev is only used to access config.distDir for telemetry and trace.
let
config
:
NextConfigComplete
let
config
:
NextConfigComplete
let
isTurboSession
=
false
let
isTurboSession
=
false
let
traceUploadUrl
:
string
let
traceUploadUrl
:
string
...
@@ -95,16 +96,6 @@ const handleSessionStop = async (signal: NodeJS.Signals | number | null) => {
...
@@ -95,16 +96,6 @@ const handleSessionStop = async (signal: NodeJS.Signals | number | null) => {
const
{
eventCliSessionStopped
}
=
const
{
eventCliSessionStopped
}
=
require
(
'../telemetry/events/session-stopped'
)
as
typeof
import
(
'../telemetry/events/session-stopped'
)
require
(
'../telemetry/events/session-stopped'
)
as
typeof
import
(
'../telemetry/events/session-stopped'
)
config
=
config
||
(
await
loadConfig
(
PHASE_DEVELOPMENT_SERVER
,
dir
))
let
telemetry
=
(
traceGlobals
.
get
(
'telemetry'
)
as
InstanceType
<
typeof
import
(
'../telemetry/storage'
).
Telemetry
>
)
||
new
Telemetry
({
distDir
:
path
.
join
(
dir
,
config
.
distDir
),
})
let
pagesDir
:
boolean
=
!!
traceGlobals
.
get
(
'pagesDir'
)
let
pagesDir
:
boolean
=
!!
traceGlobals
.
get
(
'pagesDir'
)
let
appDir
:
boolean
=
!!
traceGlobals
.
get
(
'appDir'
)
let
appDir
:
boolean
=
!!
traceGlobals
.
get
(
'appDir'
)
...
@@ -117,6 +108,18 @@ const handleSessionStop = async (signal: NodeJS.Signals | number | null) => {
...
@@ -117,6 +108,18 @@ const handleSessionStop = async (signal: NodeJS.Signals | number | null) => {
pagesDir
=
!!
pagesResult
.
pagesDir
pagesDir
=
!!
pagesResult
.
pagesDir
}
}
config
=
config
||
(
await
loadConfig
(
PHASE_DEVELOPMENT_SERVER
,
dir
,
{
silent
:
true
}))
let
telemetry
=
(
traceGlobals
.
get
(
'telemetry'
)
as
InstanceType
<
typeof
import
(
'../telemetry/storage'
).
Telemetry
>
)
||
new
Telemetry
({
distDir
:
path
.
join
(
dir
,
config
.
distDir
),
})
telemetry
.
record
(
telemetry
.
record
(
eventCliSessionStopped
({
eventCliSessionStopped
({
cliCommand
:
'dev'
,
cliCommand
:
'dev'
,
...
@@ -228,10 +231,6 @@ const nextDev = async (
...
@@ -228,10 +231,6 @@ const nextDev = async (
// some set-ups that rely on listening on other interfaces
// some set-ups that rely on listening on other interfaces
const
host
=
options
.
hostname
const
host
=
options
.
hostname
config
=
await
loadConfig
(
PHASE_DEVELOPMENT_SERVER
,
dir
,
{
silent
:
false
,
})
if
(
if
(
options
.
experimentalUploadTrace
&&
options
.
experimentalUploadTrace
&&
!
process
.
env
.
NEXT_TRACE_UPLOAD_DISABLED
!
process
.
env
.
NEXT_TRACE_UPLOAD_DISABLED
...
@@ -247,10 +246,6 @@ const nextDev = async (
...
@@ -247,10 +246,6 @@ const nextDev = async (
hostname
:
host
,
hostname
:
host
,
}
}
const
distDir
=
path
.
join
(
dir
,
config
.
distDir
??
'.next'
)
setGlobal
(
'phase'
,
PHASE_DEVELOPMENT_SERVER
)
setGlobal
(
'distDir'
,
distDir
)
const
startServerPath
=
require
.
resolve
(
'../server/lib/start-server'
)
const
startServerPath
=
require
.
resolve
(
'../server/lib/start-server'
)
async
function
startServer
(
startServerOptions
:
StartServerOptions
)
{
async
function
startServer
(
startServerOptions
:
StartServerOptions
)
{
...
@@ -330,6 +325,13 @@ const nextDev = async (
...
@@ -330,6 +325,13 @@ const nextDev = async (
// must upload the existing contents before restarting the server to
// must upload the existing contents before restarting the server to
// preserve the metrics.
// preserve the metrics.
if
(
traceUploadUrl
)
{
if
(
traceUploadUrl
)
{
// Postpone loading next config when we need to get
// config.distDir for upload trace.
config
=
config
||
(
await
loadConfig
(
PHASE_DEVELOPMENT_SERVER
,
dir
,
{
silent
:
true
,
}))
uploadTrace
({
uploadTrace
({
traceUploadUrl
,
traceUploadUrl
,
mode
:
'dev'
,
mode
:
'dev'
,
...
...
packages/next/src/lib/find-root.ts
View file @
df5bc544
...
@@ -17,9 +17,16 @@ export function findRootLockFile(cwd: string) {
...
@@ -17,9 +17,16 @@ export function findRootLockFile(cwd: string) {
)
)
}
}
export
function
findRootDir
(
cwd
:
string
):
string
{
export
function
findRootDirAndLockFiles
(
cwd
:
string
):
{
lockFiles
:
string
[]
rootDir
:
string
}
{
const
lockFile
=
findRootLockFile
(
cwd
)
const
lockFile
=
findRootLockFile
(
cwd
)
if
(
!
lockFile
)
return
cwd
if
(
!
lockFile
)
return
{
lockFiles
:
[],
rootDir
:
cwd
,
}
const
lockFiles
=
[
lockFile
]
const
lockFiles
=
[
lockFile
]
while
(
true
)
{
while
(
true
)
{
...
@@ -37,8 +44,14 @@ export function findRootDir(cwd: string): string {
...
@@ -37,8 +44,14 @@ export function findRootDir(cwd: string): string {
lockFiles
.
push
(
newLockFile
)
lockFiles
.
push
(
newLockFile
)
}
}
// Only warn if not in a build worker to avoid duplicate warnings
return
{
if
(
typeof
process
.
send
!==
'function'
&&
lockFiles
.
length
>
1
)
{
lockFiles
,
rootDir
:
dirname
(
lockFiles
[
lockFiles
.
length
-
1
]),
}
}
export
function
warnDuplicatedLockFiles
(
lockFiles
:
string
[])
{
if
(
lockFiles
.
length
>
1
)
{
const
additionalLockFiles
=
lockFiles
const
additionalLockFiles
=
lockFiles
.
slice
(
0
,
-
1
)
.
slice
(
0
,
-
1
)
.
map
((
str
)
=>
`\n *
${
str
}
`
)
.
map
((
str
)
=>
`\n *
${
str
}
`
)
...
@@ -64,6 +77,4 @@ export function findRootDir(cwd: string): string {
...
@@ -64,6 +77,4 @@ export function findRootDir(cwd: string): string {
)
)
}
}
}
}
return
dirname
(
lockFiles
[
lockFiles
.
length
-
1
])
}
}
packages/next/src/server/config.ts
View file @
df5bc544
...
@@ -25,7 +25,10 @@ import { imageConfigDefault } from '../shared/lib/image-config'
...
@@ -25,7 +25,10 @@ import { imageConfigDefault } from '../shared/lib/image-config'
import
type
{
ImageConfig
}
from
'../shared/lib/image-config'
import
type
{
ImageConfig
}
from
'../shared/lib/image-config'
import
{
loadEnvConfig
,
updateInitialEnv
}
from
'@next/env'
import
{
loadEnvConfig
,
updateInitialEnv
}
from
'@next/env'
import
{
flushAndExit
}
from
'../telemetry/flush-and-exit'
import
{
flushAndExit
}
from
'../telemetry/flush-and-exit'
import
{
findRootDir
}
from
'../lib/find-root'
import
{
findRootDirAndLockFiles
,
warnDuplicatedLockFiles
,
}
from
'../lib/find-root'
import
{
setHttpClientAndAgentOptions
}
from
'./setup-http-agent-env'
import
{
setHttpClientAndAgentOptions
}
from
'./setup-http-agent-env'
import
{
pathHasPrefix
}
from
'../shared/lib/router/utils/path-has-prefix'
import
{
pathHasPrefix
}
from
'../shared/lib/router/utils/path-has-prefix'
import
{
matchRemotePattern
}
from
'../shared/lib/match-remote-pattern'
import
{
matchRemotePattern
}
from
'../shared/lib/match-remote-pattern'
...
@@ -764,7 +767,14 @@ function assignDefaults(
...
@@ -764,7 +767,14 @@ function assignDefaults(
)
)
}
}
const
rootDir
=
tracingRoot
||
turbopackRoot
||
findRootDir
(
dir
)
let
rootDir
=
tracingRoot
||
turbopackRoot
if
(
!
rootDir
)
{
const
{
rootDir
:
foundRootDir
,
lockFiles
}
=
findRootDirAndLockFiles
(
dir
)
rootDir
=
foundRootDir
if
(
!
silent
)
{
warnDuplicatedLockFiles
(
lockFiles
)
}
}
if
(
!
rootDir
)
{
if
(
!
rootDir
)
{
throw
new
Error
(
throw
new
Error
(
...
@@ -1432,43 +1442,9 @@ export default async function loadConfig(
...
@@ -1432,43 +1442,9 @@ export default async function loadConfig(
// Check deprecation warnings on the actual user config before merging with defaults
// Check deprecation warnings on the actual user config before merging with defaults
checkDeprecations
(
userConfig
,
configFileName
,
silent
,
dir
)
checkDeprecations
(
userConfig
,
configFileName
,
silent
,
dir
)
// Always validate the config against schema in non minimal mode.
// Always validate the config against schema in non minimal mode
// Only validate once in the root Next.js process, not in forked processes.
if
(
!
process
.
env
.
NEXT_MINIMAL
&&
!
silent
)
{
const
isRootProcess
=
typeof
process
.
send
!==
'function'
validateConfigSchema
(
userConfig
,
configFileName
,
curLog
.
warn
)
if
(
!
process
.
env
.
NEXT_MINIMAL
&&
isRootProcess
)
{
// We only validate the config against schema in non minimal mode
const
{
configSchema
}
=
require
(
'./config-schema'
)
as
typeof
import
(
'./config-schema'
)
const
state
=
configSchema
.
safeParse
(
userConfig
)
if
(
!
state
.
success
)
{
// error message header
const
messages
=
[
`Invalid
${
configFileName
}
options detected: `
]
const
[
errorMessages
,
shouldExit
]
=
normalizeNextConfigZodErrors
(
state
.
error
)
// ident list item
for
(
const
error
of
errorMessages
)
{
messages
.
push
(
`
${
error
}
`
)
}
// error message footer
messages
.
push
(
'See more info here: https://nextjs.org/docs/messages/invalid-next-config'
)
if
(
shouldExit
)
{
for
(
const
message
of
messages
)
{
console
.
error
(
message
)
}
await
flushAndExit
(
1
)
}
else
{
for
(
const
message
of
messages
)
{
curLog
.
warn
(
message
)
}
}
}
}
}
if
(
userConfig
.
target
&&
userConfig
.
target
!==
'server'
)
{
if
(
userConfig
.
target
&&
userConfig
.
target
!==
'server'
)
{
...
@@ -1897,3 +1873,43 @@ function cloneObject(obj: any): any {
...
@@ -1897,3 +1873,43 @@ function cloneObject(obj: any): any {
return
result
return
result
}
}
async
function
validateConfigSchema
(
userConfig
:
NextConfig
,
configFileName
:
string
,
warn
:
(
message
:
string
)
=>
void
)
{
// We only validate the config against schema in non minimal mode
const
{
configSchema
}
=
require
(
'./config-schema'
)
as
typeof
import
(
'./config-schema'
)
const
state
=
configSchema
.
safeParse
(
userConfig
)
if
(
!
state
.
success
)
{
// error message header
const
messages
=
[
`Invalid
${
configFileName
}
options detected: `
]
const
[
errorMessages
,
shouldExit
]
=
normalizeNextConfigZodErrors
(
state
.
error
)
// ident list item
for
(
const
error
of
errorMessages
)
{
messages
.
push
(
`
${
error
}
`
)
}
// error message footer
messages
.
push
(
'See more info here: https://nextjs.org/docs/messages/invalid-next-config'
)
if
(
shouldExit
)
{
for
(
const
message
of
messages
)
{
console
.
error
(
message
)
}
await
flushAndExit
(
1
)
}
else
{
for
(
const
message
of
messages
)
{
warn
(
message
)
}
}
}
}
packages/next/src/server/lib/app-info-log.ts
View file @
df5bc544
...
@@ -95,6 +95,7 @@ export async function getStartServerInfo({
...
@@ -95,6 +95,7 @@ export async function getStartServerInfo({
)
)
},
},
debugPrerender
,
debugPrerender
,
silent
:
false
,
}
}
)
)
...
...
test/development/load-config-freq/app/layout.js
0 → 100644
View file @
df5bc544
export
default
function
Root
({
children
})
{
return
(
<
html
>
<
body
>
{
children
}
<
/body
>
<
/html
>
)
}
test/development/load-config-freq/app/page.js
0 → 100644
View file @
df5bc544
export
default
function
Page
()
{
return
<
p
>
hello
world
<
/p
>
}
test/development/load-config-freq/load-config-freq.test.ts
0 → 100644
View file @
df5bc544
import
{
nextTestSetup
}
from
'e2e-utils'
describe
(
'load-config-freq'
,
()
=>
{
const
{
next
}
=
nextTestSetup
({
files
:
__dirname
,
})
it
(
'should only load next config once when start next dev server'
,
async
()
=>
{
await
next
.
fetch
(
'/'
)
const
output
=
next
.
cliOutput
// Ensure there's only one "[ASSERTION] load nextConfig" in the text
const
parts
=
output
.
split
(
/
\[
ASSERTION
\]
load nextConfig/g
)
expect
(
parts
).
toHaveLength
(
2
)
})
})
test/development/load-config-freq/next.config.js
0 → 100644
View file @
df5bc544
/**
* @type {import('next').NextConfig}
*/
const
nextConfig
=
{}
console
.
log
(
'[ASSERTION] load nextConfig'
)
module
.
exports
=
nextConfig
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment