Skip to content

Commit 9a10f71

Browse files
committed
perf(dev): warm the fork pool on first file change instead of on ready
1 parent c16f02b commit 9a10f71

3 files changed

Lines changed: 21 additions & 4 deletions

File tree

packages/nuxt-cli/src/commands/dev.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ const command = defineCommand({
153153
}
154154

155155
// Start the initial dev server in-process with listener
156-
const { listener, close, reload, onRestart, onReady } = await initialize({ cwd, args: ctx.args }, {
156+
const { listener, close, reload, onRestart, onReady, onFileChange } = await initialize({ cwd, args: ctx.args }, {
157157
data: ctx.data,
158158
listenOverrides,
159159
showBanner: true,
@@ -176,14 +176,18 @@ const command = defineCommand({
176176
inspect,
177177
})
178178

179-
// When ready, start warming up the fork pool
180179
onReady((_address) => {
181-
pool.startWarming()
182180
if (startTime) {
183181
debug(`Dev server ready for connections in ${Date.now() - startTime}ms`)
184182
}
185183
})
186184

185+
// Warming costs a whole extra Node process, so wait until the user shows
186+
// signs of editing the project rather than paying for it on every `nuxt dev`.
187+
onFileChange(() => {
188+
pool.startWarming()
189+
})
190+
187191
// On hard restart, use a fork from the pool
188192
let cleanupCurrentFork: (() => Promise<void>) | undefined
189193

packages/nuxt-cli/src/dev/index.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,8 @@ interface InitializeReturn {
6363
/** Reload Nuxt in place, keeping the current listener. */
6464
reload: (reason?: string) => Promise<void>
6565
onReady: (callback: (address: string) => void) => void
66+
/** Called the first time a watched file changes, before Nuxt reloads. */
67+
onFileChange: (callback: () => void) => void
6668
onRestart: (callback: (devServer: NuxtDevServer) => void) => void
6769
}
6870

@@ -171,6 +173,9 @@ export async function initialize(devContext: NuxtDevContext, ctx: InitializeOpti
171173
devServer.once('ready', payload => callback(payload))
172174
}
173175
},
176+
onFileChange: (callback: () => void) => {
177+
devServer.once('change', callback)
178+
},
174179
onRestart: (callback: (devServer: NuxtDevServer) => void) => {
175180
let restarted = false
176181
function restart() {

packages/nuxt-cli/src/dev/utils.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,7 @@ interface DevServerEventMap {
153153
'loading': [loadingMessage: string]
154154
'ready': [address: string]
155155
'restart': []
156+
'change': []
156157
}
157158

158159
export class NuxtDevServer extends EventEmitter<DevServerEventMap> {
@@ -390,6 +391,10 @@ export class NuxtDevServer extends EventEmitter<DevServerEventMap> {
390391
throw new Error('Nuxt must be loaded before configuration')
391392
}
392393

394+
this.#currentNuxt.hooks.hook('builder:watch', () => {
395+
this.emit('change')
396+
})
397+
393398
// Connect Vite HMR
394399
if (!process.env.NUXI_DISABLE_VITE_HMR) {
395400
this.#currentNuxt.hooks.hook('vite:extend', ({ config }) => {
@@ -613,7 +618,10 @@ export class NuxtDevServer extends EventEmitter<DevServerEventMap> {
613618
this.#cwd,
614619
this.options.dotenv.fileName,
615620
() => this.emit('restart'),
616-
file => this.loadDebounced(true, `${file} updated`),
621+
(file) => {
622+
this.emit('change')
623+
this.loadDebounced(true, `${file} updated`)
624+
},
617625
getLocalLayerDirs(this.#currentNuxt?.options._layers ?? [], this.#cwd),
618626
)
619627
}

0 commit comments

Comments
 (0)