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
6 changes: 6 additions & 0 deletions packages/next/src/cli/next-dev.ts
Original file line number Diff line number Diff line change
Expand Up @@ -371,6 +371,12 @@ const nextDev: CliCommand = async (argv) => {
undefined,
true
)

if (config.experimental?.appDir && !devServerOptions.useWorkers) {
Log.error(
`Disabling dev workers is not supported with appDir enabled`
)
}
}
}

Expand Down
13 changes: 12 additions & 1 deletion packages/next/src/cli/next-start.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ import { getPort, printAndExit } from '../server/lib/utils'
import isError from '../lib/is-error'
import { getProjectDir } from '../lib/get-project-dir'
import { CliCommand } from '../lib/commands'
import { resolve } from 'path'
import { PHASE_PRODUCTION_SERVER } from '../shared/lib/constants'
import loadConfig from '../server/config'

const nextStart: CliCommand = async (argv) => {
const validArgs: arg.Spec = {
Expand Down Expand Up @@ -71,13 +74,21 @@ const nextStart: CliCommand = async (argv) => {
? Math.ceil(keepAliveTimeoutArg)
: undefined

const config = await loadConfig(
PHASE_PRODUCTION_SERVER,
resolve(dir || '.'),
undefined,
undefined,
true
)

await startServer({
dir,
isDev: false,
hostname: host,
port,
keepAliveTimeout,
useWorkers: false,
useWorkers: !!config.experimental.appDir,
})
}

Expand Down
10 changes: 6 additions & 4 deletions packages/next/src/server/lib/start-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { getNodeOptionsWithoutInspect } from './utils'
import type { IncomingMessage, ServerResponse } from 'http'
import type { ChildProcess } from 'child_process'
import { normalizeRepeatedSlashes } from '../../shared/lib/utils'
import { initialEnv } from '@next/env'

export interface StartServerOptions {
dir: string
Expand Down Expand Up @@ -185,7 +186,7 @@ export async function startServer({
forkOptions: {
env: {
FORCE_COLOR: '1',
...process.env,
...((initialEnv || process.env) as typeof process.env),
// we don't pass down NODE_OPTIONS as it can
// extra memory usage
NODE_OPTIONS: getNodeOptionsWithoutInspect()
Expand Down Expand Up @@ -242,8 +243,9 @@ export async function startServer({
didInitialize = true

const getProxyServer = (pathname: string) => {
const targetUrl = `http://${targetHost}:${routerPort}${pathname}`

const targetUrl = `http://${
targetHost === 'localhost' ? '127.0.0.1' : targetHost
}:${routerPort}${pathname}`
const proxyServer = httpProxy.createProxy({
target: targetUrl,
changeOrigin: false,
Expand All @@ -253,7 +255,7 @@ export async function startServer({
followRedirects: false,
})

proxyServer.on('error', () => {
proxyServer.on('error', (_err) => {
// TODO?: enable verbose error logs with --debug flag?
})
return proxyServer
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
23 changes: 13 additions & 10 deletions test/e2e/app-dir/app-static/app-static.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,16 +93,19 @@ createNextDescribe(
)
expect((await revalidateRes.json()).revalidated).toBe(true)

const newRes = await next.fetch('/variable-revalidate/revalidate-360')
const newHtml = await newRes.text()
const new$ = cheerio.load(newHtml)
const newLayoutData = new$('#layout-data').text()
const newPageData = new$('#page-data').text()

expect(newLayoutData).toBeTruthy()
expect(newPageData).toBeTruthy()
expect(newLayoutData).not.toBe(initLayoutData)
expect(newPageData).not.toBe(initPageData)
await check(async () => {
const newRes = await next.fetch('/variable-revalidate/revalidate-360')
const newHtml = await newRes.text()
const new$ = cheerio.load(newHtml)
const newLayoutData = new$('#layout-data').text()
const newPageData = new$('#page-data').text()

expect(newLayoutData).toBeTruthy()
expect(newPageData).toBeTruthy()
expect(newLayoutData).not.toBe(initLayoutData)
expect(newPageData).not.toBe(initPageData)
return 'success'
}, 'success')
})
}

Expand Down
2 changes: 1 addition & 1 deletion test/e2e/app-dir/app/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ createNextDescribe(
const res = await next.fetch('/dashboard')
expect(res.headers.get('x-edge-runtime')).toBe('1')
expect(res.headers.get('vary')).toBe(
isNextDeploy || isNextStart
isNextDeploy
? 'RSC, Next-Router-State-Tree, Next-Router-Prefetch'
: 'RSC, Next-Router-State-Tree, Next-Router-Prefetch, Accept-Encoding'
)
Expand Down
29 changes: 13 additions & 16 deletions test/e2e/middleware-general/test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -484,22 +484,19 @@ describe('Middleware Runtime', () => {
it('should contain process polyfill', async () => {
const res = await fetchViaHTTP(next.url, `/global`)
const json = readMiddlewareJSON(res)
delete json.process.env['JEST_WORKER_ID']

expect(json).toEqual({
process: {
env: {
ANOTHER_MIDDLEWARE_TEST: 'asdf2',
STRING_ENV_VAR: 'asdf3',
MIDDLEWARE_TEST: 'asdf',
...((global as any).isNextDeploy
? {}
: {
NEXT_RUNTIME: 'edge',
}),
},
},
})

for (const [key, value] of Object.entries({
ANOTHER_MIDDLEWARE_TEST: 'asdf2',
STRING_ENV_VAR: 'asdf3',
MIDDLEWARE_TEST: 'asdf',
...((global as any).isNextDeploy
? {}
: {
NEXT_RUNTIME: 'edge',
}),
})) {
expect(json.process.env[key]).toBe(value)
}
})

it(`should contain \`globalThis\``, async () => {
Expand Down