Unverified Commit 19c2e197 authored by Aly Ahmed's avatar Aly Ahmed Committed by GitHub

fix setAssetPrefix when running on NextCustomServer (#61676)

### What?

Fixes `setAssetPrefix` when running on a custom server using next()
function

### Why?

Looks like when we run next custom server, there are 2 instances created
to handle this case

* main server in `/server/next.ts`
* and Render server `./server/lib/render-server.ts`

The issue mainly happens when calling `setAssetPrefix` it will update
only the main server and not render server

``` javascript
  const next = require('next')
  const app = next({ });
    app.setAssetPrefix('my-fancy-cdn/static');
    app.render(); // render server is used instead of the main server
```

### How?

when calling `setAssetPrefix` on the main server also update the render
server with the same information.

Fixes: #59940
Fixes: #51223 
Fixes: #61383

### Related Resources

#53523  #49805


https://github.com/vercel/next.js/blob/c9c6ff6d77d3f7b61b955452bd31ec84c5e71cf0/docs/03-pages/01-building-your-application/06-configuring/10-custom-server.mdx

---------
Co-authored-by: 's avatarJJ Kasper <jj@jjsweb.site>
parent a4d33d4a
...@@ -125,6 +125,7 @@ export async function initialize( ...@@ -125,6 +125,7 @@ export async function initialize(
upgradeHandler: ReturnType< upgradeHandler: ReturnType<
InstanceType<typeof NextServer>['getUpgradeHandler'] InstanceType<typeof NextServer>['getUpgradeHandler']
> >
app: NextServer
}> { }> {
// if we already setup the server return as we only need to do // if we already setup the server return as we only need to do
// this on first worker boot // this on first worker boot
......
...@@ -2,6 +2,8 @@ ...@@ -2,6 +2,8 @@
import type { WorkerRequestHandler, WorkerUpgradeHandler } from './types' import type { WorkerRequestHandler, WorkerUpgradeHandler } from './types'
import type { DevBundler } from './router-utils/setup-dev-bundler' import type { DevBundler } from './router-utils/setup-dev-bundler'
import type { NextUrlWithParsedQuery } from '../request-meta' import type { NextUrlWithParsedQuery } from '../request-meta'
import type { NextServer } from '../next'
// This is required before other imports to ensure the require hook is setup. // This is required before other imports to ensure the require hook is setup.
import '../node-environment' import '../node-environment'
import '../require-hook' import '../require-hook'
...@@ -67,7 +69,7 @@ export async function initialize(opts: { ...@@ -67,7 +69,7 @@ export async function initialize(opts: {
experimentalTestProxy?: boolean experimentalTestProxy?: boolean
experimentalHttpsServer?: boolean experimentalHttpsServer?: boolean
startServerSpan?: Span startServerSpan?: Span
}): Promise<[WorkerRequestHandler, WorkerUpgradeHandler]> { }): Promise<[WorkerRequestHandler, WorkerUpgradeHandler, NextServer]> {
if (!process.env.NODE_ENV) { if (!process.env.NODE_ENV) {
// @ts-ignore not readonly // @ts-ignore not readonly
process.env.NODE_ENV = opts.dev ? 'development' : 'production' process.env.NODE_ENV = opts.dev ? 'development' : 'production'
...@@ -625,5 +627,5 @@ export async function initialize(opts: { ...@@ -625,5 +627,5 @@ export async function initialize(opts: {
} }
} }
return [requestHandler, upgradeHandler] return [requestHandler, upgradeHandler, handlers.app]
} }
...@@ -270,6 +270,8 @@ class NextCustomServer extends NextServer { ...@@ -270,6 +270,8 @@ class NextCustomServer extends NextServer {
protected requestHandler: WorkerRequestHandler protected requestHandler: WorkerRequestHandler
// @ts-expect-error These are initialized in prepare() // @ts-expect-error These are initialized in prepare()
protected upgradeHandler: WorkerUpgradeHandler protected upgradeHandler: WorkerUpgradeHandler
// @ts-expect-error These are initialized in prepare()
protected renderServer: NextServer
async prepare() { async prepare() {
const { getRequestHandlers } = const { getRequestHandlers } =
...@@ -287,6 +289,7 @@ class NextCustomServer extends NextServer { ...@@ -287,6 +289,7 @@ class NextCustomServer extends NextServer {
}) })
this.requestHandler = initResult[0] this.requestHandler = initResult[0]
this.upgradeHandler = initResult[1] this.upgradeHandler = initResult[1]
this.renderServer = initResult[2]
} }
private setupWebSocketHandler( private setupWebSocketHandler(
...@@ -340,6 +343,11 @@ class NextCustomServer extends NextServer { ...@@ -340,6 +343,11 @@ class NextCustomServer extends NextServer {
await this.requestHandler(req as any, res as any) await this.requestHandler(req as any, res as any)
return return
} }
setAssetPrefix(assetPrefix: string): void {
super.setAssetPrefix(assetPrefix)
this.renderServer.setAssetPrefix(assetPrefix)
}
} }
// This file is used for when users run `require('next')` // This file is used for when users run `require('next')`
......
...@@ -23,8 +23,7 @@ let server ...@@ -23,8 +23,7 @@ let server
const context = {} const context = {}
// TODO: investigate this test stalling in CI describe.each([
describe.skip.each([
{ title: 'using HTTP', useHttps: false }, { title: 'using HTTP', useHttps: false },
{ title: 'using HTTPS', useHttps: true }, { title: 'using HTTPS', useHttps: true },
])('Custom Server $title', ({ useHttps }) => { ])('Custom Server $title', ({ useHttps }) => {
...@@ -56,8 +55,7 @@ describe.skip.each([ ...@@ -56,8 +55,7 @@ describe.skip.each([
) )
} }
// TODO: continue supporting this or remove it? describe('with dynamic assetPrefix', () => {
describe.skip('with dynamic assetPrefix', () => {
beforeAll(() => startServer()) beforeAll(() => startServer())
afterAll(() => killApp(server)) afterAll(() => killApp(server))
...@@ -135,7 +133,7 @@ describe.skip.each([ ...@@ -135,7 +133,7 @@ describe.skip.each([
expect(html).toMatch(/made it to dashboard/) expect(html).toMatch(/made it to dashboard/)
}) })
it('should contain customServer in NEXT_DATA', async () => { it.skip('should contain customServer in NEXT_DATA', async () => {
const html = await renderViaHTTP(nextUrl, '/', undefined, { agent }) const html = await renderViaHTTP(nextUrl, '/', undefined, { agent })
const $ = cheerio.load(html) const $ = cheerio.load(html)
expect(JSON.parse($('#__NEXT_DATA__').text()).customServer).toBe(true) expect(JSON.parse($('#__NEXT_DATA__').text()).customServer).toBe(true)
...@@ -303,7 +301,7 @@ describe.skip.each([ ...@@ -303,7 +301,7 @@ describe.skip.each([
await fetchViaHTTP(nextUrl, '/unhandled-rejection', undefined, { agent }) await fetchViaHTTP(nextUrl, '/unhandled-rejection', undefined, { agent })
await check(() => stderr, /unhandledRejection/) await check(() => stderr, /unhandledRejection/)
expect(stderr).toContain('unhandledRejection: Error: unhandled rejection') expect(stderr).toContain('unhandledRejection: Error: unhandled rejection')
expect(stderr).toContain('server.js:37:22') expect(stderr).toContain('server.js:38:22')
}) })
}) })
......
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