Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix setAssetPrefix when running on NextCustomServer #61676

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/next/src/server/lib/render-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ export async function initialize(
upgradeHandler: ReturnType<
InstanceType<typeof NextServer>['getUpgradeHandler']
>
app: NextServer
}> {
// if we already setup the server return as we only need to do
// this on first worker boot
Expand Down
6 changes: 4 additions & 2 deletions packages/next/src/server/lib/router-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
import type { WorkerRequestHandler, WorkerUpgradeHandler } from './types'
import type { DevBundler } from './router-utils/setup-dev-bundler'
import type { NextUrlWithParsedQuery } from '../request-meta'
import type { NextServer } from '../next'

// This is required before other imports to ensure the require hook is setup.
import '../node-environment'
import '../require-hook'
Expand Down Expand Up @@ -67,7 +69,7 @@ export async function initialize(opts: {
experimentalTestProxy?: boolean
experimentalHttpsServer?: boolean
startServerSpan?: Span
}): Promise<[WorkerRequestHandler, WorkerUpgradeHandler]> {
}): Promise<[WorkerRequestHandler, WorkerUpgradeHandler, NextServer]> {
if (!process.env.NODE_ENV) {
// @ts-ignore not readonly
process.env.NODE_ENV = opts.dev ? 'development' : 'production'
Expand Down Expand Up @@ -625,5 +627,5 @@ export async function initialize(opts: {
}
}

return [requestHandler, upgradeHandler]
return [requestHandler, upgradeHandler, handlers.app]
}
8 changes: 8 additions & 0 deletions packages/next/src/server/next.ts
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,8 @@ class NextCustomServer extends NextServer {
protected requestHandler: WorkerRequestHandler
// @ts-expect-error These are initialized in prepare()
protected upgradeHandler: WorkerUpgradeHandler
// @ts-expect-error These are initialized in prepare()
protected renderServer: NextServer

async prepare() {
const { getRequestHandlers } =
Expand All @@ -287,6 +289,7 @@ class NextCustomServer extends NextServer {
})
this.requestHandler = initResult[0]
this.upgradeHandler = initResult[1]
this.renderServer = initResult[2]
}

private setupWebSocketHandler(
Expand Down Expand Up @@ -340,6 +343,11 @@ class NextCustomServer extends NextServer {
await this.requestHandler(req as any, res as any)
return
}

setAssetPrefix(assetPrefix: string): void {
super.setAssetPrefix(assetPrefix)
this.renderServer.setAssetPrefix(assetPrefix)
}
}

// This file is used for when users run `require('next')`
Expand Down
10 changes: 4 additions & 6 deletions test/integration/custom-server/test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,7 @@ let server

const context = {}

// TODO: investigate this test stalling in CI
describe.skip.each([
describe.each([
{ title: 'using HTTP', useHttps: false },
{ title: 'using HTTPS', useHttps: true },
])('Custom Server $title', ({ useHttps }) => {
Expand Down Expand Up @@ -56,8 +55,7 @@ describe.skip.each([
)
}

// TODO: continue supporting this or remove it?
describe.skip('with dynamic assetPrefix', () => {
describe('with dynamic assetPrefix', () => {
beforeAll(() => startServer())
afterAll(() => killApp(server))

Expand Down Expand Up @@ -135,7 +133,7 @@ describe.skip.each([
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 $ = cheerio.load(html)
expect(JSON.parse($('#__NEXT_DATA__').text()).customServer).toBe(true)
Expand Down Expand Up @@ -303,7 +301,7 @@ describe.skip.each([
await fetchViaHTTP(nextUrl, '/unhandled-rejection', undefined, { agent })
await check(() => stderr, /unhandledRejection/)
expect(stderr).toContain('unhandledRejection: Error: unhandled rejection')
expect(stderr).toContain('server.js:37:22')
expect(stderr).toContain('server.js:38:22')
})
})

Expand Down