Unverified Commit a0d15704 authored by JJ Kasper's avatar JJ Kasper Committed by GitHub

Ensure custom NextServer config is honored (#81681)

This ensures if using NextServer or a custom-server directly we honor
the custom config passed in correctly.
parent f44efa80
...@@ -1106,12 +1106,17 @@ export default class NextNodeServer extends BaseServer< ...@@ -1106,12 +1106,17 @@ export default class NextNodeServer extends BaseServer<
routerServerGlobal[RouterServerContextSymbol] = {} routerServerGlobal[RouterServerContextSymbol] = {}
} }
const relativeProjectDir = relative(process.cwd(), this.dir) const relativeProjectDir = relative(process.cwd(), this.dir)
const existingServerContext =
routerServerGlobal[RouterServerContextSymbol][relativeProjectDir]
if (!routerServerGlobal[RouterServerContextSymbol][relativeProjectDir]) { if (!existingServerContext) {
routerServerGlobal[RouterServerContextSymbol][relativeProjectDir] = { routerServerGlobal[RouterServerContextSymbol][relativeProjectDir] = {
render404: this.render404.bind(this), render404: this.render404.bind(this),
} }
} }
routerServerGlobal[RouterServerContextSymbol][
relativeProjectDir
].nextConfig = this.nextConfig
try { try {
// next.js core assumes page path without trailing slash // next.js core assumes page path without trailing slash
......
...@@ -50,6 +50,52 @@ describe('custom server', () => { ...@@ -50,6 +50,52 @@ describe('custom server', () => {
}) })
}) })
describe('custom server provided config', () => {
const { next } = nextTestSetup({
files: __dirname,
startCommand: 'node server.js',
serverReadyPattern: /^- Local:/,
dependencies: {
'get-port': '5.1.1',
},
env: {
PROVIDED_CONFIG: 'true',
},
})
it.each(['a', 'b', 'c'])('can navigate to /%s', async (page) => {
const $ = await next.render$(`/docs/${page}`)
expect($('p').text()).toBe(`Page ${page}`)
})
describe('with app dir', () => {
it('should render app with react canary', async () => {
const $ = await next.render$(`/docs/1`)
expect($('body').text()).toMatch(/app: .+-canary/)
})
it('should render pages with installed react', async () => {
const $ = await next.render$(`/docs/2`)
if (isReact18) {
expect($('body').text()).toMatch(/pages: 18\.\d+\.\d+\{/)
} else {
expect($('body').text()).toMatch(/pages: 19\.\d+\.\d+/)
}
})
describe('when using "use cache" with a custom cache handler', () => {
it("should not unset the custom server's ALS context", async () => {
const cliOutputLength = next.cliOutput.length
const $ = await next.render$('/docs/use-cache')
expect($('p').text()).toBe('inner cache')
const cliOutput = next.cliOutput.slice(cliOutputLength)
expect(cliOutput).toMatch(createCacheSetLogRegExp('outer'))
expect(cliOutput).toMatch(createCacheSetLogRegExp('inner'))
})
})
})
})
describe('custom server with quiet setting', () => { describe('custom server with quiet setting', () => {
const { next } = nextTestSetup({ const { next } = nextTestSetup({
files: __dirname, files: __dirname,
......
...@@ -10,8 +10,15 @@ let requestId = 0 ...@@ -10,8 +10,15 @@ let requestId = 0
async function main() { async function main() {
const port = await getPort() const port = await getPort()
const hostname = 'localhost' const hostname = 'localhost'
let conf = undefined
if (process.env.PROVIDED_CONFIG) {
conf = require('./.next/required-server-files.json').config
conf.basePath = '/docs'
}
// when using middleware `hostname` and `port` must be provided below // when using middleware `hostname` and `port` must be provided below
const app = next({ hostname, port, quiet }) const app = next({ hostname, port, quiet, conf })
const handle = app.getRequestHandler() const handle = app.getRequestHandler()
app.prepare().then(() => { app.prepare().then(() => {
...@@ -21,7 +28,11 @@ async function main() { ...@@ -21,7 +28,11 @@ async function main() {
// Be sure to pass `true` as the second argument to `url.parse`. // Be sure to pass `true` as the second argument to `url.parse`.
// This tells it to parse the query portion of the URL. // This tells it to parse the query portion of the URL.
const parsedUrl = parse(req.url, true) const parsedUrl = parse(req.url, true)
const { pathname, query } = parsedUrl let { pathname, query } = parsedUrl
if (conf?.basePath) {
pathname = pathname.replace(conf.basePath, '') || '/'
}
if (pathname === '/a') { if (pathname === '/a') {
await app.render(req, res, '/a', query) await app.render(req, res, '/a', query)
...@@ -30,6 +41,7 @@ async function main() { ...@@ -30,6 +41,7 @@ async function main() {
} else if (pathname === '/error') { } else if (pathname === '/error') {
await app.render(req, res, '/page-error') await app.render(req, res, '/page-error')
} else { } else {
parsedUrl.pathname = pathname
await handle(req, res, parsedUrl) await handle(req, res, parsedUrl)
} }
} catch (err) { } catch (err) {
......
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