0f332055d4
### 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> |
||
---|---|---|
.. | ||
compat | ||
font | ||
image-types | ||
legacy | ||
navigation-types/compat | ||
src | ||
types | ||
amp.d.ts | ||
amp.js | ||
app.d.ts | ||
app.js | ||
babel.d.ts | ||
babel.js | ||
cache.d.ts | ||
cache.js | ||
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 | ||
jest.d.ts | ||
jest.js | ||
license.md | ||
link.d.ts | ||
link.js | ||
navigation.d.ts | ||
navigation.js | ||
package.json | ||
README.md | ||
router.d.ts | ||
router.js | ||
script.d.ts | ||
script.js | ||
server.d.ts | ||
server.js | ||
taskfile-ncc.js | ||
taskfile-swc.js | ||
taskfile-watch.js | ||
taskfile.js | ||
tsconfig.json | ||
web-vitals.d.ts | ||
web-vitals.js |
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
- Tim Neutkens (@timneutkens)
- Naoyuki Kanezawa (@nkzawa)
- Guillermo Rauch (@rauchg)
- Arunoda Susiripala (@arunoda)
- Tony Kovanen (@tonykovanen)
- Dan Zajdband (@impronunciable)
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.