rsnext/packages/next
Fernando Dingler 0f332055d4
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: Tim Neutkens <tim@timneutkens.nl>
Co-authored-by: JJ Kasper <jj@jjsweb.site>
2023-05-11 10:36:40 -07:00
..
compat
font Remove experimental fontLoaders option from next.config.js (#46886) 2023-03-13 21:34:43 +00:00
image-types
legacy
navigation-types/compat Return null for useParams in pages (#47490) 2023-03-25 10:05:44 +01:00
src fix: Standalone node http server starts accepting requests before next handler is ready (#49548) 2023-05-11 10:36:40 -07:00
types Add stub Route type for typedRoutes (#48099) 2023-05-10 12:34:27 +02:00
amp.d.ts
amp.js
app.d.ts
app.js
babel.d.ts
babel.js
cache.d.ts Update cache methods for app dir (#49165) 2023-05-03 13:21:58 -07:00
cache.js Update cache methods for app dir (#49165) 2023-05-03 13:21:58 -07:00
client.d.ts
client.js
config.d.ts
config.js
constants.d.ts
constants.js
document.d.ts
document.js
dynamic.d.ts
dynamic.js
error.d.ts
error.js
head.d.ts
head.js
headers.d.ts
headers.js
image.d.ts
image.js
index.d.ts Update revalidate handling for app (#49062) 2023-05-02 08:19:02 -07:00
jest.d.ts
jest.js
license.md Update license year (#44403) 2023-01-01 11:12:49 +01:00
link.d.ts
link.js
navigation.d.ts
navigation.js
package.json v13.4.2-canary.5 2023-05-11 05:02:19 +00:00
README.md
router.d.ts
router.js
script.d.ts
script.js Alias next public api to esm on edge runtime (#42709) 2022-11-09 19:20:32 -08:00
server.d.ts Update revalidate handling for app (#49062) 2023-05-02 08:19:02 -07:00
server.js Update revalidate handling for app (#49062) 2023-05-02 08:19:02 -07:00
taskfile-ncc.js Move core files to src folder and move JS files to TypeScript (#44405) 2023-01-03 10:05:50 +01:00
taskfile-swc.js feat: make enableUndici: true default and remove node-fetch (#49061) 2023-05-02 04:10:59 +00:00
taskfile-watch.js Replace taskr.watch for core compilation (#44027) 2022-12-14 16:06:53 +01:00
taskfile.js Enable App Router for all applications (#49088) 2023-05-04 17:47:10 +02:00
tsconfig.json next/navigation Typescript support for pages/ (#45919) 2023-02-14 17:26:01 -08:00
web-vitals.d.ts add useReportWebVitals that makes use of web-vitals package (#47319) 2023-03-24 10:13:50 +00:00
web-vitals.js add useReportWebVitals that makes use of web-vitals package (#47319) 2023-03-24 10:13:50 +00:00

Next.js

Getting Started

Visit https://nextjs.org/learn to get started with Next.js.

Documentation

Visit https://nextjs.org/docs to view the full documentation.

Who is using Next.js?

Next.js is used by the world's leading companies. Check out the Next.js Showcase to learn more.

Community

The Next.js community can be found on GitHub Discussions, where you can ask questions, voice ideas, and share your projects.

To chat with other community members you can join the Next.js Discord.

Our Code of Conduct applies to all Next.js community channels.

Contributing

Please see our contributing.md.

Good First Issues

We have a list of good first issues that contain bugs that have a relatively limited scope. This is a great place to get started, gain experience, and get familiar with our contribution process.

Authors

Security

If you believe you have found a security vulnerability in Next.js, we encourage you to responsibly disclose this and not open a public issue. We will investigate all legitimate reports. Email security@vercel.com to disclose any security vulnerabilities.

https://vercel.com/security