Unverified Commit 0f332055 authored by Fernando Dingler's avatar Fernando Dingler Committed by GitHub

fix: Standalone node http server starts accepting requests before next handler is ready (#49548)

### Fixing a bug

Fixes #49536
Fixes #49338

### What?
Starting on next.js 13.4.0, the `server.js` file created by the
standalone output is incorrect because it does not wait until the Next
handler is ready before node http server starts accepting requests.

Initial requests will produce the following error:

```
TypeError: handler is not a function
at Server.<anonymous> (/tmp/app/server.js:29:11)
at Server.emit (node:events:513:28)
at parserOnIncoming (node:_http_server:998:12)
at HTTPParser.parserOnHeadersComplete (node:_http_common:128:17)
```

### Why?
The creation of the next server takes roughly 800ms on a basic hello
world app which causes the initial requests to fail because the
`handler` variable is not initialized.

### Solution
This changes the order of code so that Next Server is initialized before
the Node HTTP server and awaits until the promise is completed to kick
off the HTTP server.

### Alternative code

```js
let handler;

const server = http.createServer(async (req, res) => {
  try {
    await waitUntilHandlerIsReady(); // <---- wait here
    await handler(req, res);
  } catch (err) {
    console.error("Failed to call handler", err);
    res.statusCode = 500;
    res.end("Internal Server Error");
  }
});

async function waitUntilHandlerIsReady() {
  if (!handler) {
    await new Promise((resolve) => setTimeout(resolve, 200));
    await waitUntilHandlerIsReady();
  }
}
```

---------
Co-authored-by: 's avatarTim Neutkens <tim@timneutkens.nl>
Co-authored-by: 's avatarJJ Kasper <jj@jjsweb.site>
parent ec6cdb39
...@@ -1939,8 +1939,6 @@ if (!process.env.NEXT_MANUAL_SIG_HANDLE) { ...@@ -1939,8 +1939,6 @@ if (!process.env.NEXT_MANUAL_SIG_HANDLE) {
process.on('SIGINT', () => process.exit(0)) process.on('SIGINT', () => process.exit(0))
} }
let handler
const currentPort = parseInt(process.env.PORT, 10) || 3000 const currentPort = parseInt(process.env.PORT, 10) || 3000
const hostname = process.env.HOSTNAME || 'localhost' const hostname = process.env.HOSTNAME || 'localhost'
const keepAliveTimeout = parseInt(process.env.KEEP_ALIVE_TIMEOUT, 10); const keepAliveTimeout = parseInt(process.env.KEEP_ALIVE_TIMEOUT, 10);
...@@ -1951,42 +1949,46 @@ const nextConfig = ${JSON.stringify({ ...@@ -1951,42 +1949,46 @@ const nextConfig = ${JSON.stringify({
process.env.__NEXT_PRIVATE_STANDALONE_CONFIG = JSON.stringify(nextConfig) process.env.__NEXT_PRIVATE_STANDALONE_CONFIG = JSON.stringify(nextConfig)
const server = http.createServer(async (req, res) => { createServerHandler({
try { port: currentPort,
await handler(req, res) hostname,
} catch (err) { dir,
console.error(err); conf: nextConfig,
res.statusCode = 500 }).then((nextHandler) => {
res.end('Internal Server Error') const server = http.createServer(async (req, res) => {
} try {
}) await nextHandler(req, res)
} catch (err) {
if ( console.error(err);
!Number.isNaN(keepAliveTimeout) && res.statusCode = 500
Number.isFinite(keepAliveTimeout) && res.end('Internal Server Error')
keepAliveTimeout >= 0 }
) {
server.keepAliveTimeout = keepAliveTimeout
}
server.listen(currentPort, async (err) => {
if (err) {
console.error("Failed to start server", err)
process.exit(1)
}
handler = await createServerHandler({
port: currentPort,
hostname,
dir,
conf: nextConfig,
}) })
if (
!Number.isNaN(keepAliveTimeout) &&
Number.isFinite(keepAliveTimeout) &&
keepAliveTimeout >= 0
) {
server.keepAliveTimeout = keepAliveTimeout
}
server.listen(currentPort, async (err) => {
if (err) {
console.error("Failed to start server", err)
process.exit(1)
}
console.log(
'Listening on port',
currentPort,
'url: http://' + hostname + ':' + currentPort
)
});
console.log( }).catch(err => {
'Listening on port', console.error(err);
currentPort, process.exit(1);
'url: http://' + hostname + ':' + currentPort });`
)
})`
) )
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment