Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix to use keep-alive in standalone mode #50221

Merged
merged 9 commits into from Jul 6, 2023
12 changes: 7 additions & 5 deletions packages/next/src/build/utils.ts
Expand Up @@ -1955,6 +1955,10 @@ if (!process.env.NEXT_MANUAL_SIG_HANDLE) {
const currentPort = parseInt(process.env.PORT, 10) || 3000
const hostname = process.env.HOSTNAME || 'localhost'
const keepAliveTimeout = parseInt(process.env.KEEP_ALIVE_TIMEOUT, 10);
const isValidKeepAliveTimeout =
!Number.isNaN(keepAliveTimeout) &&
Number.isFinite(keepAliveTimeout) &&
keepAliveTimeout >= 0;
const nextConfig = ${JSON.stringify({
...serverConfig,
distDir: `./${path.relative(dir, distDir)}`,
Expand All @@ -1967,6 +1971,7 @@ createServerHandler({
hostname,
dir,
conf: nextConfig,
keepAliveTimeout: isValidKeepAliveTimeout ? keepAliveTimeout : undefined,
}).then((nextHandler) => {
const server = http.createServer(async (req, res) => {
try {
Expand All @@ -1978,13 +1983,10 @@ createServerHandler({
}
})

if (
!Number.isNaN(keepAliveTimeout) &&
Number.isFinite(keepAliveTimeout) &&
keepAliveTimeout >= 0
) {
if (isValidKeepAliveTimeout) {
server.keepAliveTimeout = keepAliveTimeout
}

server.listen(currentPort, async (err) => {
if (err) {
console.error("Failed to start server", err)
Expand Down
3 changes: 3 additions & 0 deletions packages/next/src/server/lib/render-server-standalone.ts
Expand Up @@ -11,12 +11,14 @@ export const createServerHandler = async ({
dir,
dev = false,
minimalMode,
keepAliveTimeout,
}: {
port: number
hostname: string
dir: string
dev?: boolean
minimalMode: boolean
keepAliveTimeout?: number
}) => {
const routerWorker = new Worker(require.resolve('./render-server'), {
numWorkers: 1,
Expand Down Expand Up @@ -65,6 +67,7 @@ export const createServerHandler = async ({
minimalMode,
workerType: 'router',
isNodeDebugging: false,
keepAliveTimeout,
})
didInitialize = true

Expand Down