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

Opt-into worker mode when appDir is enabled #47857

Merged
merged 16 commits into from
Apr 11, 2023
18 changes: 17 additions & 1 deletion packages/next/src/server/lib/start-server.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
import http from 'http'
import { resolve } from 'path'
import { isIPv6 } from 'net'
import * as Log from '../../build/output/log'
import { getNodeOptionsWithoutInspect } from './utils'
import type { IncomingMessage, ServerResponse } from 'http'
import type { ChildProcess } from 'child_process'
import { normalizeRepeatedSlashes } from '../../shared/lib/utils'
import { PHASE_DEVELOPMENT_SERVER } from '../../shared/lib/constants'
import { PHASE_PRODUCTION_SERVER } from '../../shared/lib/constants'
import loadConfig from '../config'

export interface StartServerOptions {
dir: string
Expand Down Expand Up @@ -162,8 +166,19 @@ export async function startServer({
server.listen(port, hostname)
})

const config = await loadConfig(
isDev ? PHASE_DEVELOPMENT_SERVER : PHASE_PRODUCTION_SERVER,
resolve(dir || '.'),
undefined,
undefined,
false
)

// Always use workers when `appDir` is enabled.
const shouldUseWorkers = useWorkers || config.experimental.appDir

try {
if (useWorkers) {
if (shouldUseWorkers) {
const httpProxy =
require('next/dist/compiled/http-proxy') as typeof import('next/dist/compiled/http-proxy')

Expand Down Expand Up @@ -295,6 +310,7 @@ export async function startServer({
httpServer: server,
customServer: false,
port: addr && typeof addr === 'object' ? addr.port : port,
preloadedConfig: config,
})
// handle in process
requestHandler = app.getRequestHandler()
Expand Down
21 changes: 14 additions & 7 deletions packages/next/src/server/next.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import type { Options as DevServerOptions } from './dev/next-dev-server'
import type { NodeRequestHandler } from './next-server'
import type { UrlWithParsedQuery } from 'url'
import type { NextConfigComplete } from './config-shared'

import './node-polyfill-fetch'
import { default as Server } from './next-server'
import * as log from '../build/output/log'
Expand Down Expand Up @@ -29,7 +31,9 @@ const getServerImpl = async () => {
return ServerImpl
}

export type NextServerOptions = Partial<DevServerOptions>
export type NextServerOptions = Partial<DevServerOptions> & {
preloadedConfig?: NextConfigComplete
}

export interface RequestHandler {
(
Expand Down Expand Up @@ -144,12 +148,15 @@ export class NextServer {
}

private async loadConfig() {
return loadConfig(
this.options.dev ? PHASE_DEVELOPMENT_SERVER : PHASE_PRODUCTION_SERVER,
resolve(this.options.dir || '.'),
this.options.conf,
undefined,
!!this.options._renderWorker
return (
this.options.preloadedConfig ||
loadConfig(
this.options.dev ? PHASE_DEVELOPMENT_SERVER : PHASE_PRODUCTION_SERVER,
resolve(this.options.dir || '.'),
this.options.conf,
undefined,
!!this.options._renderWorker
)
)
}

Expand Down