Unverified Commit ac8a6035 authored by Alex Yang's avatar Alex Yang Committed by GitHub

fix: http2 example issue (#56768)

### What?

If the custom server uses `app.render(xxx)` will render the normal js files as server components in dev mode and cause 404 error when loading every non-HTML file.

### How?

Fixes https://github.com/nodejs/help/issues/4253, fixes https://github.com/vercel/next.js/issues/50270
parent f95a11f8
const next = require('next')
const http2 = require('http2')
const fs = require('fs')
const http2 = require('node:http2')
const { parse } = require('node:url')
const fs = require('node:fs')
const port = parseInt(process.env.PORT, 10) || 3000
const dev = process.env.NODE_ENV !== 'production'
......@@ -15,10 +16,13 @@ const server = http2.createSecureServer({
cert: fs.readFileSync('localhost-cert.pem'),
})
const handler = app.getRequestHandler()
app.prepare().then(() => {
server.on('error', (err) => console.error(err))
server.on('request', (req, res) => {
app.render(req, res, req.url || '/', req.query)
const parsedUrl = parse(req.url, true)
handler(req, res, parsedUrl)
})
server.listen(port)
......
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