Skip to content

Commit

Permalink
Fix to use keep-alive in standalone mode (#50221)
Browse files Browse the repository at this point in the history
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">
  • Loading branch information
doinki committed Jul 6, 2023
1 parent ce483fd commit c878caa
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 5 deletions.
12 changes: 7 additions & 5 deletions packages/next/src/build/utils.ts
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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

0 comments on commit c878caa

Please sign in to comment.