Unverified Commit 83b774ee authored by Shu Ding's avatar Shu Ding Committed by GitHub

Fix standalone mode with appDir running in a single process (#49116)

Fixes #49055, fixes #48918. 

App dir will always require the server to run in the workers mode, so it
can keep a separate Node.js process for pages. This PR updates the
standalone server to initialize a "standalone server" (which works
similar to `start-server`), and changes the tracked files to include
Jest worker.
parent c5406fc6
......@@ -51,7 +51,7 @@ app.prepare().then(() => {
} catch (err) {
console.error('Error occurred handling', req.url, err)
res.statusCode = 500
res.end('internal server error')
res.end('Internal Server Error')
}
})
.once('error', (err) => {
......
......@@ -1899,10 +1899,21 @@ export default async function build(
config.experimental?.turbotrace?.contextDirectory ??
outputFileTracingRoot
// Under standalone mode, we need to trace the extra IPC server and
// worker files.
const isStandalone = config.output === 'standalone'
const nextServerEntry = require.resolve(
'next/dist/server/next-server'
)
const toTrace = [nextServerEntry]
const toTrace = [
nextServerEntry,
isStandalone
? require.resolve(
'next/dist/server/lib/render-server-standalone'
)
: null,
].filter(nonNullable)
// ensure we trace any dependencies needed for custom
// incremental cache handler
......@@ -1927,7 +1938,7 @@ export default async function build(
'**/*.d.ts',
'**/*.map',
'**/next/dist/pages/**/*',
'**/next/dist/compiled/jest-worker/**/*',
isStandalone ? null : '**/next/dist/compiled/jest-worker/**/*',
'**/next/dist/compiled/webpack/(bundle4|bundle5).js',
'**/node_modules/webpack5/**/*',
'**/next/dist/server/lib/squoosh/**/*.wasm',
......@@ -1944,7 +1955,8 @@ export default async function build(
? ['**/next/dist/compiled/@ampproject/toolbox-optimizer/**/*']
: []),
...additionalIgnores,
]
].filter(nonNullable)
const ignoreFn = (pathname: string) => {
if (path.isAbsolute(pathname) && !pathname.startsWith(root)) {
return true
......@@ -1957,6 +1969,26 @@ export default async function build(
}
const traceContext = path.join(nextServerEntry, '..', '..')
const tracedFiles = new Set<string>()
function addToTracedFiles(base: string, file: string) {
tracedFiles.add(
path
.relative(distDir, path.join(base, file))
.replace(/\\/g, '/')
)
}
if (isStandalone) {
addToTracedFiles(
'',
require.resolve('next/dist/compiled/jest-worker/processChild')
)
addToTracedFiles(
'',
require.resolve('next/dist/compiled/jest-worker/threadChild')
)
}
if (config.experimental.turbotrace) {
const files: string[] = await nodeFileTrace(
{
......@@ -1972,11 +2004,7 @@ export default async function build(
)
for (const file of files) {
if (!ignoreFn(path.join(traceContext, file))) {
tracedFiles.add(
path
.relative(distDir, path.join(traceContext, file))
.replace(/\\/g, '/')
)
addToTracedFiles(traceContext, file)
}
}
} else {
......@@ -1987,11 +2015,7 @@ export default async function build(
})
serverResult.fileList.forEach((file) => {
tracedFiles.add(
path
.relative(distDir, path.join(root, file))
.replace(/\\/g, '/')
)
addToTracedFiles(root, file)
})
}
await promises.writeFile(
......
......@@ -1913,17 +1913,20 @@ export async function copyTracedFiles(
serverOutputPath,
`${
moduleType
? `import Server from 'next/dist/server/next-server.js'
import http from 'http'
? `import http from 'http'
import path from 'path'
import { fileURLToPath } from 'url'
const __dirname = fileURLToPath(new URL('.', import.meta.url))
const NextServer = Server.default`
import { createServerHandler } from 'next/dist/server/lib/render-server-standalone.js'
`
: `
const NextServer = require('next/dist/server/next-server').default
const http = require('http')
const path = require('path')`
const path = require('path')
const { createServerHandler } = require('next/dist/server/lib/render-server-standalone')`
}
const dir = path.join(__dirname)
process.env.NODE_ENV = 'production'
process.chdir(__dirname)
......@@ -1944,16 +1947,7 @@ const nextConfig = ${JSON.stringify({
distDir: `./${path.relative(dir, distDir)}`,
})}
${
// In standalone mode, we don't have separated render workers so if both
// app and pages are used, we need to resolve to the prebundled React
// to ensure the correctness of the version for app.
`\
if (nextConfig && nextConfig.experimental && nextConfig.experimental.appDir) {
process.env.__NEXT_PRIVATE_PREBUNDLED_REACT = nextConfig.experimental.serverActions ? 'experimental' : 'next'
}
`
}
process.env.__NEXT_PRIVATE_STANDALONE_CONFIG = JSON.stringify(nextConfig)
const server = http.createServer(async (req, res) => {
try {
......@@ -1961,7 +1955,7 @@ const server = http.createServer(async (req, res) => {
} catch (err) {
console.error(err);
res.statusCode = 500
res.end('internal server error')
res.end('Internal Server Error')
}
})
......@@ -1972,20 +1966,18 @@ if (
) {
server.keepAliveTimeout = keepAliveTimeout
}
server.listen(currentPort, (err) => {
server.listen(currentPort, async (err) => {
if (err) {
console.error("Failed to start server", err)
process.exit(1)
}
const nextServer = new NextServer({
hostname,
handler = await createServerHandler({
port: currentPort,
dir: path.join(__dirname),
dev: false,
customServer: false,
hostname,
dir,
conf: nextConfig,
})
handler = nextServer.getRequestHandler()
console.log(
'Listening on port',
......
......@@ -689,6 +689,10 @@ export default async function loadConfig(
rawConfig?: boolean,
silent?: boolean
): Promise<NextConfigComplete> {
if (process.env.__NEXT_PRIVATE_STANDALONE_CONFIG) {
return JSON.parse(process.env.__NEXT_PRIVATE_STANDALONE_CONFIG)
}
const curLog = silent
? {
warn: () => {},
......
......@@ -460,7 +460,7 @@ export async function optimizeImage({
console.error(
`Error: 'sharp' is required to be installed in standalone mode for the image optimization to function correctly. Read more at: https://nextjs.org/docs/messages/sharp-missing-in-production`
)
throw new ImageError(500, 'internal server error')
throw new ImageError(500, 'Internal Server Error')
}
// Show sharp warning in production once
if (showSharpMissingWarning) {
......
import type { IncomingMessage, ServerResponse } from 'http'
import type { ChildProcess } from 'child_process'
import httpProxy from 'next/dist/compiled/http-proxy'
import { Worker } from 'next/dist/compiled/jest-worker'
import { normalizeRepeatedSlashes } from '../../shared/lib/utils'
const renderServerPath = require.resolve('./render-server')
export const createServerHandler = async ({
port,
hostname,
dir,
minimalMode,
}: {
port: number
hostname: string
dir: string
minimalMode: boolean
}) => {
const routerWorker = new Worker(renderServerPath, {
numWorkers: 1,
maxRetries: 10,
forkOptions: {
env: {
FORCE_COLOR: '1',
...process.env,
},
},
exposedMethods: ['initialize'],
}) as any as InstanceType<typeof Worker> & {
initialize: typeof import('./render-server').initialize
}
let didInitialize = false
for (const _worker of ((routerWorker as any)._workerPool?._workers || []) as {
_child: ChildProcess
}[]) {
// eslint-disable-next-line no-loop-func
_worker._child.on('exit', (code, signal) => {
// catch failed initializing without retry
if ((code || signal) && !didInitialize) {
routerWorker?.end()
process.exit(1)
}
})
}
const workerStdout = routerWorker.getStdout()
const workerStderr = routerWorker.getStderr()
workerStdout.on('data', (data) => {
process.stdout.write(data)
})
workerStderr.on('data', (data) => {
process.stderr.write(data)
})
const { port: routerPort } = await routerWorker.initialize({
dir,
port,
dev: false,
hostname,
minimalMode,
workerType: 'router',
})
didInitialize = true
const getProxyServer = (pathname: string) => {
const targetUrl = `http://${hostname}:${routerPort}${pathname}`
const proxyServer = httpProxy.createProxy({
target: targetUrl,
changeOrigin: false,
ignorePath: true,
xfwd: true,
ws: true,
followRedirects: false,
})
return proxyServer
}
// proxy to router worker
return async (req: IncomingMessage, res: ServerResponse) => {
const urlParts = (req.url || '').split('?')
const urlNoQuery = urlParts[0]
// this normalizes repeated slashes in the path e.g. hello//world ->
// hello/world or backslashes to forward slashes, this does not
// handle trailing slash as that is handled the same as a next.config.js
// redirect
if (urlNoQuery?.match(/(\\|\/\/)/)) {
const cleanUrl = normalizeRepeatedSlashes(req.url!)
res.statusCode = 308
res.setHeader('Location', cleanUrl)
res.end(cleanUrl)
return
}
const proxyServer = getProxyServer(req.url || '/')
proxyServer.web(req, res)
proxyServer.on('error', (err) => {
res.statusCode = 500
res.end('Internal Server Error')
console.error(err)
})
}
}
......@@ -42,6 +42,7 @@ export async function initialize(opts: {
dir: string
port: number
dev: boolean
minimalMode?: boolean
hostname?: string
workerType: 'router' | 'render'
keepAliveTimeout?: number
......@@ -54,18 +55,24 @@ export async function initialize(opts: {
let requestHandler: RequestHandler
const server = http.createServer((req, res) => {
return requestHandler(req, res).finally(() => {
if (
process.memoryUsage().heapUsed / 1024 / 1024 >
MAXIMUM_HEAP_SIZE_ALLOWED
) {
warn(
'The server is running out of memory, restarting to free up memory.'
)
server.close()
process.exit(WORKER_SELF_EXIT_CODE)
}
})
return requestHandler(req, res)
.catch((err) => {
res.statusCode = 500
res.end('Internal Server Error')
console.error(err)
})
.finally(() => {
if (
process.memoryUsage().heapUsed / 1024 / 1024 >
MAXIMUM_HEAP_SIZE_ALLOWED
) {
warn(
'The server is running out of memory, restarting to free up memory.'
)
server.close()
process.exit(WORKER_SELF_EXIT_CODE)
}
})
})
if (opts.keepAliveTimeout) {
......
......@@ -104,6 +104,8 @@ export const createWorker = (
__NEXT_PRIVATE_RENDER_WORKER: type,
__NEXT_PRIVATE_ROUTER_IPC_PORT: ipcPort + '',
__NEXT_PRIVATE_ROUTER_IPC_KEY: ipcValidationKey,
__NEXT_PRIVATE_STANDALONE_CONFIG:
process.env.__NEXT_PRIVATE_STANDALONE_CONFIG,
NODE_ENV: process.env.NODE_ENV,
...(type === 'app'
? {
......
......@@ -263,6 +263,7 @@ export default class NextNodeServer extends BaseServer {
dir: this.dir,
workerType: 'render',
hostname: this.hostname,
minimalMode: this.minimalMode,
dev: !!options.dev,
}
const { createWorker, createIpcServer } =
......
......@@ -169,9 +169,11 @@ export class NextServer {
this.serverPromise = this.loadConfig().then(async (conf) => {
if (!this.options.dev) {
if (conf.output === 'standalone') {
log.warn(
`"next start" does not work with "output: standalone" configuration. Use "node .next/standalone/server.js" instead.`
)
if (!process.env.__NEXT_PRIVATE_STANDALONE_CONFIG) {
log.warn(
`"next start" does not work with "output: standalone" configuration. Use "node .next/standalone/server.js" instead.`
)
}
} else if (conf.output === 'export') {
throw new Error(
`"next start" does not work with "output: export" configuration. Use "npx serve@latest out" instead.`
......
......@@ -14,7 +14,7 @@ const server = http.createServer(async (req, res) => {
} catch (err) {
console.error(err)
res.statusCode = 500
res.end('internal server error')
res.end('Internal Server Error')
}
})
const currentPort = parseInt(process.env.PORT, 10) || 3000
......
......@@ -25,7 +25,7 @@ describe('Handles an Error in _error', () => {
it('Handles error during SSR', async () => {
const html = await renderViaHTTP(port, '/some-404-page')
expect(html).toMatch(/internal server error/i)
expect(html).toMatch(/Internal Server Error/i)
})
it('Handles error during client transition', async () => {
......@@ -33,6 +33,6 @@ describe('Handles an Error in _error', () => {
await browser.waitForElementByCss('a').click()
await waitFor(1000)
const html = await browser.eval('document.body.innerHTML')
expect(html).toMatch(/internal server error/i)
expect(html).toMatch(/Internal Server Error/i)
})
})
......@@ -83,7 +83,7 @@ describe('i18n Support', () => {
'utf8'
)
expect(content).toContain('500')
expect(content).toMatch(/internal server error/i)
expect(content).toMatch(/Internal Server Error/i)
}
})
})
......
......@@ -26,7 +26,7 @@ app.prepare().then(() => {
} catch (err) {
console.error('Error occurred handling', req.url, err)
res.statusCode = 500
res.end('internal server error')
res.end('Internal Server Error')
}
}).listen(port, (err) => {
if (err) throw err
......
......@@ -71,9 +71,9 @@ describe('should set-up next', () => {
const testServer = join(next.testDir, 'standalone/server.js')
await fs.writeFile(
testServer,
(await fs.readFile(testServer, 'utf8'))
.replace('console.error(err)', `console.error('top-level', err)`)
.replace('conf:', `minimalMode: ${minimalMode},conf:`)
(
await fs.readFile(testServer, 'utf8')
).replace('conf:', `minimalMode: ${minimalMode},conf:`)
)
appPort = await findPort()
server = await initNextServerScript(
......
......@@ -105,9 +105,9 @@ describe('should set-up next', () => {
const testServer = join(next.testDir, 'standalone/server.js')
await fs.writeFile(
testServer,
(await fs.readFile(testServer, 'utf8'))
.replace('console.error(err)', `console.error('top-level', err)`)
.replace('conf:', 'minimalMode: true,conf:')
(
await fs.readFile(testServer, 'utf8')
).replace('conf:', 'minimalMode: true,conf:')
)
appPort = await findPort()
server = await initNextServerScript(
......@@ -121,9 +121,7 @@ describe('should set-up next', () => {
{
cwd: next.testDir,
onStderr(msg) {
if (msg.includes('top-level')) {
errors.push(msg)
}
errors.push(msg)
},
}
)
......@@ -592,7 +590,7 @@ describe('should set-up next', () => {
errors = []
const res = await fetchViaHTTP(appPort, '/errors/gip', { crash: '1' })
expect(res.status).toBe(500)
expect(await res.text()).toBe('internal server error')
expect(await res.text()).toBe('Internal Server Error')
await check(
() => (errors[0].includes('gip hit an oops') ? 'success' : errors[0]),
......@@ -604,7 +602,7 @@ describe('should set-up next', () => {
errors = []
const res = await fetchViaHTTP(appPort, '/errors/gssp', { crash: '1' })
expect(res.status).toBe(500)
expect(await res.text()).toBe('internal server error')
expect(await res.text()).toBe('Internal Server Error')
await check(
() => (errors[0].includes('gssp hit an oops') ? 'success' : errors[0]),
'success'
......@@ -615,7 +613,7 @@ describe('should set-up next', () => {
errors = []
const res = await fetchViaHTTP(appPort, '/errors/gsp/crash')
expect(res.status).toBe(500)
expect(await res.text()).toBe('internal server error')
expect(await res.text()).toBe('Internal Server Error')
await check(
() => (errors[0].includes('gsp hit an oops') ? 'success' : errors[0]),
'success'
......@@ -626,7 +624,7 @@ describe('should set-up next', () => {
errors = []
const res = await fetchViaHTTP(appPort, '/api/error')
expect(res.status).toBe(500)
expect(await res.text()).toBe('internal server error')
expect(await res.text()).toBe('Internal Server Error')
await check(
() =>
errors[0].includes('some error from /api/error')
......
......@@ -107,9 +107,9 @@ describe('should set-up next', () => {
const testServer = join(next.testDir, 'standalone/server.js')
await fs.writeFile(
testServer,
(await fs.readFile(testServer, 'utf8'))
.replace('console.error(err)', `console.error('top-level', err)`)
.replace('conf:', `minimalMode: ${minimalMode},conf:`)
(
await fs.readFile(testServer, 'utf8')
).replace('conf:', `minimalMode: ${minimalMode},conf:`)
)
appPort = await findPort()
server = await initNextServerScript(
......@@ -123,9 +123,7 @@ describe('should set-up next', () => {
{
cwd: next.testDir,
onStderr(msg) {
if (msg.includes('top-level')) {
errors.push(msg)
}
errors.push(msg)
stderr += msg
},
}
......@@ -941,7 +939,7 @@ describe('should set-up next', () => {
errors = []
const res = await fetchViaHTTP(appPort, '/errors/gip', { crash: '1' })
expect(res.status).toBe(500)
expect(await res.text()).toBe('internal server error')
expect(await res.text()).toBe('Internal Server Error')
await check(
() => (errors[0].includes('gip hit an oops') ? 'success' : errors[0]),
......@@ -953,7 +951,7 @@ describe('should set-up next', () => {
errors = []
const res = await fetchViaHTTP(appPort, '/errors/gssp', { crash: '1' })
expect(res.status).toBe(500)
expect(await res.text()).toBe('internal server error')
expect(await res.text()).toBe('Internal Server Error')
await check(
() => (errors[0].includes('gssp hit an oops') ? 'success' : errors[0]),
'success'
......@@ -964,7 +962,7 @@ describe('should set-up next', () => {
errors = []
const res = await fetchViaHTTP(appPort, '/errors/gsp/crash')
expect(res.status).toBe(500)
expect(await res.text()).toBe('internal server error')
expect(await res.text()).toBe('Internal Server Error')
await check(
() => (errors[0].includes('gsp hit an oops') ? 'success' : errors[0]),
'success'
......@@ -975,7 +973,7 @@ describe('should set-up next', () => {
errors = []
const res = await fetchViaHTTP(appPort, '/api/error')
expect(res.status).toBe(500)
expect(await res.text()).toBe('internal server error')
expect(await res.text()).toBe('Internal Server Error')
await check(
() =>
errors[0].includes('some error from /api/error')
......@@ -1280,9 +1278,7 @@ describe('should set-up next', () => {
{
cwd: next.testDir,
onStderr(msg) {
if (msg.includes('top-level')) {
errors.push(msg)
}
errors.push(msg)
stderr += msg
},
}
......
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