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

remove inheritance from NextCustomServer #73005

Merged
merged 5 commits into from
Nov 22, 2024
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
71 changes: 30 additions & 41 deletions packages/next/src/server/lib/render-server.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,17 @@
import type { NextServer, RequestHandler } from '../next'
import type { NextServer, RequestHandler, UpgradeHandler } from '../next'
import type { DevBundlerService } from './dev-bundler-service'
import type { PropagateToWorkersField } from './router-utils/types'

import next from '../next'
import type { Span } from '../../trace'

let initializations: Record<
string,
| Promise<{
requestHandler: ReturnType<
InstanceType<typeof NextServer>['getRequestHandler']
>
upgradeHandler: ReturnType<
InstanceType<typeof NextServer>['getUpgradeHandler']
>
app: ReturnType<typeof next>
}>
| undefined
> = {}
export type ServerInitResult = {
requestHandler: RequestHandler
upgradeHandler: UpgradeHandler
server: NextServer
}

let initializations: Record<string, Promise<ServerInitResult> | undefined> = {}

let sandboxContext: undefined | typeof import('../web/sandbox/context')

Expand All @@ -41,9 +35,9 @@ export async function getServerField(
if (!initialization) {
throw new Error('Invariant cant propagate server field, no app initialized')
}
const { app } = initialization
let appField = (app as any).server
return appField[field]
const { server } = initialization
let wrappedServer = server['server']! // NextServer.server is private
return wrappedServer[field as keyof typeof wrappedServer]
}

export async function propagateServerField(
Expand All @@ -55,17 +49,20 @@ export async function propagateServerField(
if (!initialization) {
throw new Error('Invariant cant propagate server field, no app initialized')
}
const { app } = initialization
let appField = (app as any).server
const { server } = initialization
let wrappedServer = server['server']
const _field = field as keyof NonNullable<typeof wrappedServer>

if (appField) {
if (typeof appField[field] === 'function') {
await appField[field].apply(
(app as any).server,
if (wrappedServer) {
if (typeof wrappedServer[_field] === 'function') {
// @ts-expect-error
await wrappedServer[_field].apply(
wrappedServer,
Array.isArray(value) ? value : []
)
} else {
appField[field] = value
// @ts-expect-error
wrappedServer[_field] = value
}
}
}
Expand All @@ -86,45 +83,37 @@ async function initializeImpl(opts: {
bundlerService: DevBundlerService | undefined
startServerSpan: Span | undefined
quiet?: boolean
}) {
}): Promise<ServerInitResult> {
const type = process.env.__NEXT_PRIVATE_RENDER_WORKER
if (type) {
process.title = 'next-render-worker-' + type
}

let requestHandler: RequestHandler
let upgradeHandler: any
let upgradeHandler: UpgradeHandler

const app = next({
const server = next({
...opts,
hostname: opts.hostname || 'localhost',
customServer: false,
httpServer: opts.server,
port: opts.port,
})
requestHandler = app.getRequestHandler()
upgradeHandler = app.getUpgradeHandler()
}) as NextServer // should return a NextServer when `customServer: false`
requestHandler = server.getRequestHandler()
upgradeHandler = server.getUpgradeHandler()

await app.prepare(opts.serverFields)
await server.prepare(opts.serverFields)

return {
requestHandler,
upgradeHandler,
app,
server,
}
}

export async function initialize(
opts: Parameters<typeof initializeImpl>[0]
): Promise<{
requestHandler: ReturnType<
InstanceType<typeof NextServer>['getRequestHandler']
>
upgradeHandler: ReturnType<
InstanceType<typeof NextServer>['getUpgradeHandler']
>
app: NextServer
}> {
): Promise<ServerInitResult> {
// if we already setup the server return as we only need to do
// this on first worker boot
if (initializations[opts.dir]) {
Expand Down
6 changes: 3 additions & 3 deletions packages/next/src/server/lib/router-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
import type { WorkerRequestHandler, WorkerUpgradeHandler } from './types'
import type { DevBundler, ServerFields } from './router-utils/setup-dev-bundler'
import type { NextUrlWithParsedQuery, RequestMeta } from '../request-meta'
import type { NextServer } from '../next'

// This is required before other imports to ensure the require hook is setup.
import '../node-environment'
Expand Down Expand Up @@ -47,6 +46,7 @@ import {
} from '../dev/hot-reloader-types'
import { normalizedAssetPrefix } from '../../shared/lib/normalized-asset-prefix'
import { NEXT_PATCH_SYMBOL } from './patch-fetch'
import type { ServerInitResult } from './render-server'

const debug = setupDebug('next:router-server:main')
const isNextFont = (pathname: string | null) =>
Expand Down Expand Up @@ -79,7 +79,7 @@ export async function initialize(opts: {
experimentalHttpsServer?: boolean
startServerSpan?: Span
quiet?: boolean
}): Promise<[WorkerRequestHandler, WorkerUpgradeHandler, NextServer]> {
}): Promise<ServerInitResult> {
if (!process.env.NODE_ENV) {
// @ts-ignore not readonly
process.env.NODE_ENV = opts.dev ? 'development' : 'production'
Expand Down Expand Up @@ -739,5 +739,5 @@ export async function initialize(opts: {
}
}

return [requestHandler, upgradeHandler, handlers.app]
return { requestHandler, upgradeHandler, server: handlers.server }
}
4 changes: 2 additions & 2 deletions packages/next/src/server/lib/start-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -334,8 +334,8 @@ export async function startServer(
keepAliveTimeout,
experimentalHttpsServer: !!selfSignedCertificate,
})
requestHandler = initResult[0]
upgradeHandler = initResult[1]
requestHandler = initResult.requestHandler
upgradeHandler = initResult.upgradeHandler

const startServerProcessDuration =
performance.mark('next-start-end') &&
Expand Down
Loading
Loading