mirror of
https://github.com/hanzoai/next.js.git
synced 2026-09-24 17:00:51 +00:00
Currently we create separate workers to isolate `pages` and `app` routers due to differing react versions being used between the two. This adds overhead and complexity in the rendering process which we can avoid by leveraging an `esm-loader` similar to our `require-hook` to properly alias `pages` router to the bundled react version to match `app` router when both are leveraged together. This aims to seamlessly inject the `esm-loader` by restarting the process with the loader arg added whenever `next` is imported so that this also works with custom-servers and fixes the issue with custom req/res fields not working after upgrading. x-ref: https://github.com/vercel/next.js/issues/53883 x-ref: https://github.com/vercel/next.js/issues/54288 x-ref: https://github.com/vercel/next.js/issues/54129 x-ref: https://github.com/vercel/next.js/issues/54435 closes: https://github.com/vercel/next.js/issues/54440 closes: https://github.com/vercel/next.js/issues/52702 x-ref: [slack thread](https://vercel.slack.com/archives/C03KAR5DCKC/p1693348443932499?thread_ts=1693275196.347509&cid=C03KAR5DCKC) --------- Co-authored-by: Tim Neutkens <[email protected]> Co-authored-by: Zack Tanner <[email protected]> Co-authored-by: kodiakhq[bot] <49736102+kodiakhq[bot]@users.noreply.github.com>
45 lines
1.3 KiB
JavaScript
45 lines
1.3 KiB
JavaScript
const { createServer } = require('http')
|
|
const { parse } = require('url')
|
|
const next = require('next')
|
|
const getPort = require('get-port')
|
|
|
|
async function main() {
|
|
const port = await getPort()
|
|
const hostname = 'localhost'
|
|
// when using middleware `hostname` and `port` must be provided below
|
|
const app = next({ hostname, port })
|
|
const handle = app.getRequestHandler()
|
|
|
|
app.prepare().then(() => {
|
|
createServer(async (req, res) => {
|
|
try {
|
|
// Be sure to pass `true` as the second argument to `url.parse`.
|
|
// This tells it to parse the query portion of the URL.
|
|
const parsedUrl = parse(req.url, true)
|
|
const { pathname, query } = parsedUrl
|
|
|
|
if (pathname === '/a') {
|
|
await app.render(req, res, '/a', query)
|
|
} else if (pathname === '/b') {
|
|
await app.render(req, res, '/page-b', query)
|
|
} else {
|
|
await handle(req, res, parsedUrl)
|
|
}
|
|
} catch (err) {
|
|
console.error('Error occurred handling', req.url, err)
|
|
res.statusCode = 500
|
|
res.end('Internal Server Error')
|
|
}
|
|
}).listen(port, undefined, (err) => {
|
|
if (err) throw err
|
|
// Start mode
|
|
console.log(`- Local: http://${hostname}:${port}`)
|
|
})
|
|
})
|
|
}
|
|
|
|
main().catch((err) => {
|
|
console.error(err)
|
|
process.exit(1)
|
|
})
|