mirror of
https://github.com/hanzoai/next.js.git
synced 2026-09-24 17:00:51 +00:00
### What This PR changes `NextCustomServer` to no longer subclass `NextServer` and instead just delegate all methods to `this.renderServer`. I've also added an interface implemented by both `NextServer` and `NextCustomServer` to make it explicit what we're actually exposing. (Note that we're currently exposing too much for legacy reasons. we might want to decide to clean this up a bit, i've started that in https://github.com/vercel/next.js/pull/73021) I've also cleaned up some random `(_ as any)`s and unnecessary indirections around the area. ### Why currently, `NextCustomServer` is a subclass of `NextServer`, which makes it very confusing to reason about, because in `prepare()` it creates *a nested `NextServer`* (stored in `this.renderServer`). this isn't immediately visible in the code, but that's what `getRequestHandlers` ends up doing. https://github.com/vercel/next.js/blob/58b993c239689f22641682bbcb0d8dea6d85fdf1/packages/next/src/server/next.ts#L281 then, it delegates *some* methods to that (`render`, `getRequestHandler`, `setAssetPrefix`). But some methods (e.g. `render404`) *aren't* overridden/delegated and just run the implementation inherited from `NextServer`. And those implementations are unaware of `this.renderServer`, so they end up initializing a *second* `NextNodeServer` (`this.server` vs `this.renderServer.server`) and run against that instead. that's bad! and it's why e.g. #61676 was necessary -- we needed to update both servers! i think that this is a leftover of the old implementation where we ran a separate process for rendering. for historical context, see: - #49805 which seems to have introduced this split (the proxy only overrides `prepare`, `getRequestHandler`, and `render`) - #53523 which turned the proxy code into `NextCustomServer` also apparently `render404` and others were broken in development (try running the tests in the first commit I've changed `NextCustomServer` and see the server hang forever).
152 lines
3.7 KiB
JavaScript
152 lines
3.7 KiB
JavaScript
// @ts-check
|
|
|
|
if (process.env.POLYFILL_FETCH) {
|
|
// @ts-expect-error
|
|
global.fetch = require('node-fetch').default
|
|
// @ts-expect-error
|
|
global.Request = require('node-fetch').Request
|
|
// @ts-expect-error
|
|
global.Headers = require('node-fetch').Headers
|
|
}
|
|
|
|
const { readFileSync } = require('fs')
|
|
|
|
/** @type {import('next').default} */
|
|
// @ts-ignore: missing interopDefault
|
|
const next = require('next')
|
|
|
|
const { join } = require('path')
|
|
const { parse } = require('url')
|
|
|
|
const dev = process.env.NODE_ENV !== 'production'
|
|
const dir = __dirname
|
|
const port =
|
|
(process.env.PORT ? Number.parseInt(process.env.PORT) : undefined) || 3000
|
|
const { createServer } = require(
|
|
process.env.USE_HTTPS === 'true' ? 'https' : 'http'
|
|
)
|
|
|
|
const app = next({ dev, hostname: 'localhost', port, dir })
|
|
const handleNextRequests = app.getRequestHandler()
|
|
|
|
const httpOptions = {
|
|
key: readFileSync(join(__dirname, 'ssh/localhost-key.pem')),
|
|
cert: readFileSync(join(__dirname, 'ssh/localhost.pem')),
|
|
}
|
|
|
|
process.on('unhandledRejection', (err) => {
|
|
console.error('unhandledRejection:', err)
|
|
})
|
|
|
|
app.prepare().then(() => {
|
|
const server = createServer(httpOptions, async (req, res) => {
|
|
// let next.js handle assets from /_next/
|
|
if (/\/_next\//.test(req.url)) {
|
|
return handleNextRequests(req, res)
|
|
}
|
|
|
|
if (req.url === '/no-query') {
|
|
return app.render(req, res, '/no-query')
|
|
}
|
|
|
|
if (req.url === '/unhandled-rejection') {
|
|
Promise.reject(new Error('unhandled rejection'))
|
|
return res.end('ok')
|
|
}
|
|
|
|
if (/setAssetPrefix/.test(req.url)) {
|
|
app.setAssetPrefix(`http://127.0.0.1:${port}`)
|
|
} else if (/setEmptyAssetPrefix/.test(req.url)) {
|
|
app.setAssetPrefix('')
|
|
} else {
|
|
// This is to support multi-zones support in localhost
|
|
// and may be in staging deployments
|
|
app.setAssetPrefix('')
|
|
}
|
|
|
|
if (/test-index-hmr/.test(req.url)) {
|
|
return app.render(req, res, '/index')
|
|
}
|
|
|
|
if (/dashboard/.test(req.url)) {
|
|
return app.render(req, res, '/dashboard')
|
|
}
|
|
|
|
if (/static\/hello\.text/.test(req.url)) {
|
|
return app.render(req, res, '/static/hello.text')
|
|
}
|
|
|
|
if (/no-slash/.test(req.url)) {
|
|
try {
|
|
await app.render(req, res, 'dashboard')
|
|
} catch (err) {
|
|
res.end(err.message)
|
|
}
|
|
}
|
|
|
|
if (/custom-url-with-request-handler/.test(req.url)) {
|
|
return handleNextRequests(req, res, parse('/dashboard', true))
|
|
}
|
|
|
|
if (/legacy-methods\/render-to-html/.test(req.url)) {
|
|
try {
|
|
const html = await app.renderToHTML(req, res, '/dynamic-dashboard', {
|
|
q: '1',
|
|
})
|
|
res.end(html)
|
|
} catch (err) {
|
|
res.end(err.message)
|
|
}
|
|
return
|
|
}
|
|
|
|
if (/legacy-methods\/render404/.test(req.url)) {
|
|
try {
|
|
await app.render404(req, res, parse('/__non_existent__?q=1', true))
|
|
} catch (err) {
|
|
res.end(err.message)
|
|
}
|
|
return
|
|
}
|
|
|
|
if (/legacy-methods\/render-error/.test(req.url)) {
|
|
try {
|
|
res.statusCode = 500
|
|
await app.renderError(new Error('kaboom'), req, res, '/dashboard', {
|
|
q: '1',
|
|
})
|
|
} catch (err) {
|
|
res.end(err.message)
|
|
}
|
|
return
|
|
}
|
|
|
|
if (/legacy-methods\/render-error-to-html/.test(req.url)) {
|
|
try {
|
|
res.statusCode = 500
|
|
const html = await app.renderErrorToHTML(
|
|
new Error('kaboom'),
|
|
req,
|
|
res,
|
|
'/dashboard',
|
|
{ q: '1' }
|
|
)
|
|
res.end(html)
|
|
} catch (err) {
|
|
res.end(err.message)
|
|
}
|
|
return
|
|
}
|
|
|
|
handleNextRequests(req, res)
|
|
})
|
|
|
|
server.listen(port, (err) => {
|
|
if (err) {
|
|
throw err
|
|
}
|
|
|
|
console.log(`> Ready on http://localhost:${port}`)
|
|
})
|
|
})
|