diff --git a/packages/nuxi/src/commands/dev.ts b/packages/nuxi/src/commands/dev.ts index c54566f75..7a9f4d9a4 100644 --- a/packages/nuxi/src/commands/dev.ts +++ b/packages/nuxi/src/commands/dev.ts @@ -161,8 +161,10 @@ const command = defineCommand({ // ... then fall back to pre-warmed fork if a hard restart is required const fork = startSubprocess(cwd, ctx.args, ctx.rawArgs, listenOptions) onRestart(async (devServer) => { - await devServer.close() - const subprocess = await fork + const [subprocess] = await Promise.all([ + fork, + devServer.close().catch(() => {}), + ]) await subprocess.initialize(devProxy, useSocket) }) diff --git a/packages/nuxi/src/dev/index.ts b/packages/nuxi/src/dev/index.ts index 04d318def..84967ff40 100644 --- a/packages/nuxi/src/dev/index.ts +++ b/packages/nuxi/src/dev/index.ts @@ -23,10 +23,13 @@ interface InitializeOptions { class IPC { enabled = !!process.send && !process.title?.includes('vitest') && process.env.__NUXT__FORK constructor() { - process.once('unhandledRejection', (reason) => { - this.send({ type: 'nuxt:internal:dev:rejection', message: reason instanceof Error ? reason.toString() : 'Unhandled Rejection' }) - process.exit() - }) + // only kill process if it is a fork + if (this.enabled) { + process.once('unhandledRejection', (reason) => { + this.send({ type: 'nuxt:internal:dev:rejection', message: reason instanceof Error ? reason.toString() : 'Unhandled Rejection' }) + process.exit() + }) + } process.on('message', (message: NuxtParentIPCMessage) => { if (message.type === 'nuxt:internal:dev:context') { initialize(message.context, {}, message.socket ? undefined : true) @@ -150,7 +153,16 @@ export async function initialize(devContext: NuxtDevContext, ctx: InitializeOpti } }, onRestart: (callback: (devServer: NuxtDevServer) => void) => { - devServer.once('restart', () => callback(devServer)) + let restarted = false + function restart() { + if (!restarted) { + restarted = true + callback(devServer) + } + } + devServer.once('restart', restart) + process.once('uncaughtException', restart) + process.once('unhandledRejection', restart) }, } }