Skip to content

Commit eecf725

Browse files
fix(vite): kill prerender child process when the vite preview server closes (#3907)
1 parent 9e2ab3f commit eecf725

File tree

1 file changed

+15
-5
lines changed

1 file changed

+15
-5
lines changed

src/build/vite/preview.ts

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -79,16 +79,26 @@ export function nitroPreviewPlugin(ctx: NitroPluginContext): VitePlugin {
7979
PORT: String(randomPort),
8080
},
8181
});
82+
83+
const killChild = (signal: NodeJS.Signals) => {
84+
if (child && !child.killed) {
85+
child.kill(signal);
86+
}
87+
};
88+
8289
for (const sig of ["SIGINT", "SIGHUP"] as const) {
8390
process.once(sig, () => {
8491
consola.info(`Stopping preview server...`);
85-
if (child.killed === false) {
86-
child.kill(sig);
87-
process.exit();
88-
}
92+
killChild(sig);
93+
process.exit();
8994
});
9095
}
91-
child.on("exit", (code) => {
96+
97+
server.httpServer.once("close", () => {
98+
killChild("SIGTERM");
99+
});
100+
101+
child.once("exit", (code) => {
92102
if (code && code !== 0) {
93103
consola.error(`[nitro] Preview server exited with code ${code}`);
94104
}

0 commit comments

Comments
 (0)