Skip to content

Commit c878caa

Browse files
authored
Fix to use keep-alive in standalone mode (#50221)
You can specify keepAliveTimeout as an environment variable in standalone mode, but there is a problem with it not being properly applied. #46052 #### before <img width="574" alt="2023-05-24 12 49 12" src="https://github.com/vercel/next.js/assets/90969158/9014252e-dcac-4b32-805a-68e844e853b1"> <img width="574" alt="2023-05-24 12 49 20" src="https://github.com/vercel/next.js/assets/90969158/8c5672b2-8af8-4751-aa0c-7347428c3cbb"> #### after <img width="574" alt="2023-05-24 1 19 12" src="https://github.com/vercel/next.js/assets/90969158/96a83b0d-1dd1-45b7-b053-e0103185dd47">
1 parent ce483fd commit c878caa

File tree

2 files changed

+10
-5
lines changed

2 files changed

+10
-5
lines changed

packages/next/src/build/utils.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1955,6 +1955,10 @@ if (!process.env.NEXT_MANUAL_SIG_HANDLE) {
19551955
const currentPort = parseInt(process.env.PORT, 10) || 3000
19561956
const hostname = process.env.HOSTNAME || 'localhost'
19571957
const keepAliveTimeout = parseInt(process.env.KEEP_ALIVE_TIMEOUT, 10);
1958+
const isValidKeepAliveTimeout =
1959+
!Number.isNaN(keepAliveTimeout) &&
1960+
Number.isFinite(keepAliveTimeout) &&
1961+
keepAliveTimeout >= 0;
19581962
const nextConfig = ${JSON.stringify({
19591963
...serverConfig,
19601964
distDir: `./${path.relative(dir, distDir)}`,
@@ -1967,6 +1971,7 @@ createServerHandler({
19671971
hostname,
19681972
dir,
19691973
conf: nextConfig,
1974+
keepAliveTimeout: isValidKeepAliveTimeout ? keepAliveTimeout : undefined,
19701975
}).then((nextHandler) => {
19711976
const server = http.createServer(async (req, res) => {
19721977
try {
@@ -1978,13 +1983,10 @@ createServerHandler({
19781983
}
19791984
})
19801985
1981-
if (
1982-
!Number.isNaN(keepAliveTimeout) &&
1983-
Number.isFinite(keepAliveTimeout) &&
1984-
keepAliveTimeout >= 0
1985-
) {
1986+
if (isValidKeepAliveTimeout) {
19861987
server.keepAliveTimeout = keepAliveTimeout
19871988
}
1989+
19881990
server.listen(currentPort, async (err) => {
19891991
if (err) {
19901992
console.error("Failed to start server", err)

packages/next/src/server/lib/render-server-standalone.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,14 @@ export const createServerHandler = async ({
1111
dir,
1212
dev = false,
1313
minimalMode,
14+
keepAliveTimeout,
1415
}: {
1516
port: number
1617
hostname: string
1718
dir: string
1819
dev?: boolean
1920
minimalMode: boolean
21+
keepAliveTimeout?: number
2022
}) => {
2123
const routerWorker = new Worker(require.resolve('./render-server'), {
2224
numWorkers: 1,
@@ -65,6 +67,7 @@ export const createServerHandler = async ({
6567
minimalMode,
6668
workerType: 'router',
6769
isNodeDebugging: false,
70+
keepAliveTimeout,
6871
})
6972
didInitialize = true
7073

0 commit comments

Comments
 (0)