Skip to content

Commit

Permalink
fix(nextjs): kill Next.js server when parent process exits
Browse files Browse the repository at this point in the history
  • Loading branch information
jaysoo authored and vsavkin committed May 24, 2023
1 parent a29afe9 commit 1fd7836
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 23 deletions.
2 changes: 1 addition & 1 deletion packages/next/src/executors/build/build.impl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ export default async function buildExecutor(
const { experimentalAppOnly, profile, debug } = options;

const args = createCliOptions({ experimentalAppOnly, profile, debug });
const command = `npx next build ${args}`;
const command = `npx next build ${args.join(' ')}`;
const execSyncOptions: ExecSyncOptions = {
stdio: 'inherit',
encoding: 'utf-8',
Expand Down
22 changes: 11 additions & 11 deletions packages/next/src/executors/server/server.impl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import {
NextBuildBuilderOptions,
NextServeBuilderOptions,
} from '../../utils/types';
import { spawn } from 'child_process';
import { fork } from 'child_process';
import customServer from './custom-server.impl';
import { createCliOptions } from '../../utils/create-cli-options';
import { createAsyncIterable } from '@nx/devkit/src/utils/async-iterable';
Expand Down Expand Up @@ -49,13 +49,13 @@ export default async function* serveExecutor(

const mode = options.dev ? 'dev' : 'start';
const turbo = options.turbo && options.dev ? '--turbo' : '';
const command = `npx next ${mode} ${args} ${turbo}`;
const nextBin = require.resolve('next/dist/bin/next');

yield* createAsyncIterable<{ success: boolean; baseUrl: string }>(
async ({ done, next, error }) => {
const server = spawn(command, {
const server = fork(nextBin, [mode, ...args, turbo], {
cwd: options.dev ? root : nextDir,
stdio: 'inherit',
shell: true,
});

server.once('exit', (code) => {
Expand All @@ -66,15 +66,15 @@ export default async function* serveExecutor(
}
});

process.on('exit', async (code) => {
if (code === 128 + 2) {
server.kill('SIGINT');
} else if (code === 128 + 1) {
server.kill('SIGHUP');
} else {
const killServer = () => {
if (server.connected) {
server.kill('SIGTERM');
}
});
};
process.on('exit', () => killServer());
process.on('SIGINT', () => killServer());
process.on('SIGTERM', () => killServer());
process.on('SIGHUP', () => killServer());

await waitForPortOpen(port);

Expand Down
21 changes: 10 additions & 11 deletions packages/next/src/utils/create-cli-options.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
export function createCliOptions(obj: { [key: string]: any }): string {
return Object.entries(obj)
.reduce((arr, [key, value]) => {
if (value !== undefined) {
const kebabCase = key.replace(/[A-Z]/g, (m) => '-' + m.toLowerCase());
return `${arr}--${kebabCase}=${value} `;
} else {
return arr;
}
}, '')
.trim();
export function createCliOptions(
obj: Record<string, string | number | boolean>
): string[] {
return Object.entries(obj).reduce((arr, [key, value]) => {
if (value !== undefined) {
const kebabCase = key.replace(/[A-Z]/g, (m) => '-' + m.toLowerCase());
arr.push(`--${kebabCase}=${value}`);
}
return arr;
}, []);
}

1 comment on commit 1fd7836

@vercel
Copy link

@vercel vercel bot commented on 1fd7836 May 24, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

nx-dev – ./

nx-five.vercel.app
nx-dev-git-master-nrwl.vercel.app
nx.dev
nx-dev-nrwl.vercel.app

Please sign in to comment.