Unverified Commit fdacca8a authored by JJ Kasper's avatar JJ Kasper Committed by GitHub

Add initial separated route resolving (#47208)

This updates to have a separate routing process and separate rendering
processes for `pages` and `app` so that we can properly isolate the two
since they rely on different react versions.

Besides allowing the above mentioned isolation this also helps us
control recovering from process crashes easier as pieces are more
isolated from one another e.g. an infinite loop during rendering will no
longer block the compiler and can be stopped/restarted as needed.

In follow-up PRs we will continue to separate out the routing logic from
the rendering logic so that each process only loads what is relevant to
it helping simplify the flow for requests regardless of type.

---------
Co-authored-by: 's avatarShu Ding <g@shud.in>
Co-authored-by: 's avatarkodiakhq[bot] <49736102+kodiakhq[bot]@users.noreply.github.com>
parent fae94416
...@@ -10,7 +10,7 @@ export type LoadedEnvFiles = Array<{ ...@@ -10,7 +10,7 @@ export type LoadedEnvFiles = Array<{
contents: string contents: string
}> }>
let initialEnv: Env | undefined = undefined export let initialEnv: Env | undefined = undefined
let combinedEnv: Env | undefined = undefined let combinedEnv: Env | undefined = undefined
let cachedLoadedEnvFiles: LoadedEnvFiles = [] let cachedLoadedEnvFiles: LoadedEnvFiles = []
let previousLoadedEnvFiles: LoadedEnvFiles = [] let previousLoadedEnvFiles: LoadedEnvFiles = []
......
...@@ -1783,79 +1783,7 @@ export default async function build( ...@@ -1783,79 +1783,7 @@ export default async function build(
'next-server.js.nft.json' 'next-server.js.nft.json'
) )
if (config.experimental.preCompiledNextServer) { if (config.outputFileTracing) {
if (!ciEnvironment.hasNextSupport) {
Log.warn(
`"experimental.preCompiledNextServer" is currently optimized for environments with Next.js support, some features may not be supported`
)
}
const nextServerPath = require.resolve('next/dist/server/next-server')
await promises.rename(nextServerPath, `${nextServerPath}.bak`)
await promises.writeFile(
nextServerPath,
`module.exports = require('next/dist/compiled/next-server/next-server.js')`
)
const glob =
require('next/dist/compiled/glob') as typeof import('next/dist/compiled/glob')
const compiledFiles: string[] = []
const compiledNextServerFolder = path.dirname(
require.resolve('next/dist/compiled/next-server/next-server.js')
)
for (const compiledFolder of [
compiledNextServerFolder,
path.join(compiledNextServerFolder, '../react'),
path.join(compiledNextServerFolder, '../react-dom'),
path.join(compiledNextServerFolder, '../scheduler'),
]) {
const globResult = glob.sync('**/*', {
cwd: compiledFolder,
dot: true,
})
await Promise.all(
globResult.map(async (file) => {
const absolutePath = path.join(compiledFolder, file)
const statResult = await promises.stat(absolutePath)
if (statResult.isFile()) {
compiledFiles.push(absolutePath)
}
})
)
}
const externalLibFiles = [
'next/dist/shared/lib/server-inserted-html.js',
'next/dist/shared/lib/router-context.js',
'next/dist/shared/lib/loadable-context.js',
'next/dist/shared/lib/image-config-context.js',
'next/dist/shared/lib/image-config.js',
'next/dist/shared/lib/head-manager-context.js',
'next/dist/shared/lib/app-router-context.js',
'next/dist/shared/lib/amp-context.js',
'next/dist/shared/lib/hooks-client-context.js',
'next/dist/shared/lib/html-context.js',
]
for (const file of externalLibFiles) {
compiledFiles.push(require.resolve(file))
}
compiledFiles.push(nextServerPath)
await promises.writeFile(
nextServerTraceOutput,
JSON.stringify({
version: 1,
files: [...new Set(compiledFiles)].map((file) =>
path.relative(distDir, file)
),
} as {
version: number
files: string[]
})
)
} else if (config.outputFileTracing) {
let nodeFileTrace: any let nodeFileTrace: any
if (config.experimental.turbotrace) { if (config.experimental.turbotrace) {
if (!binding?.isWasm) { if (!binding?.isWasm) {
...@@ -2064,6 +1992,7 @@ export default async function build( ...@@ -2064,6 +1992,7 @@ export default async function build(
'**/*.d.ts', '**/*.d.ts',
'**/*.map', '**/*.map',
'**/next/dist/pages/**/*', '**/next/dist/pages/**/*',
'**/next/dist/compiled/jest-worker/**/*',
'**/next/dist/compiled/webpack/(bundle4|bundle5).js', '**/next/dist/compiled/webpack/(bundle4|bundle5).js',
'**/node_modules/webpack5/**/*', '**/node_modules/webpack5/**/*',
'**/next/dist/server/lib/squoosh/**/*.wasm', '**/next/dist/server/lib/squoosh/**/*.wasm',
...@@ -2082,7 +2011,14 @@ export default async function build( ...@@ -2082,7 +2011,14 @@ export default async function build(
...additionalIgnores, ...additionalIgnores,
] ]
const ignoreFn = (pathname: string) => { const ignoreFn = (pathname: string) => {
return isMatch(pathname, ignores, { contains: true, dot: true }) if (path.isAbsolute(pathname) && !pathname.startsWith(root)) {
return true
}
return isMatch(pathname, ignores, {
contains: true,
dot: true,
})
} }
const traceContext = path.join(nextServerEntry, '..', '..') const traceContext = path.join(nextServerEntry, '..', '..')
const tracedFiles = new Set<string>() const tracedFiles = new Set<string>()
......
...@@ -95,7 +95,14 @@ export function formatAmpMessages(amp: AmpPageStatus) { ...@@ -95,7 +95,14 @@ export function formatAmpMessages(amp: AmpPageStatus) {
return output return output
} }
const buildStore = createStore<BuildStatusStore>() const buildStore = createStore<BuildStatusStore>({
// @ts-expect-error initial value
client: {},
// @ts-expect-error initial value
server: {},
// @ts-expect-error initial value
edgeServer: {},
})
let buildWasDone = false let buildWasDone = false
let clientWasLoading = true let clientWasLoading = true
let serverWasLoading = true let serverWasLoading = true
......
...@@ -16,7 +16,30 @@ const originModules = [ ...@@ -16,7 +16,30 @@ const originModules = [
const RUNTIME_NAMES = ['webpack-runtime', 'webpack-api-runtime'] const RUNTIME_NAMES = ['webpack-runtime', 'webpack-api-runtime']
function deleteCache(filePath: string) { export function deleteAppClientCache() {
if ((global as any)._nextDeleteAppClientCache) {
;(global as any)._nextDeleteAppClientCache()
}
// ensure we reset the cache for sc_server components
// loaded via react-server-dom-webpack
const reactServerDomModId = require.resolve(
'next/dist/compiled/react-server-dom-webpack/client.edge'
)
const reactServerDomMod = require.cache[reactServerDomModId]
if (reactServerDomMod) {
for (const child of reactServerDomMod.children) {
child.parent = null
delete require.cache[child.id]
}
}
delete require.cache[reactServerDomModId]
}
export function deleteCache(filePath: string) {
if ((global as any)._nextDeleteCache) {
;(global as any)._nextDeleteCache(filePath)
}
try { try {
filePath = realpathSync(filePath) filePath = realpathSync(filePath)
} catch (e) { } catch (e) {
...@@ -82,20 +105,6 @@ export class NextJsRequireCacheHotReloader implements WebpackPluginInstance { ...@@ -82,20 +105,6 @@ export class NextJsRequireCacheHotReloader implements WebpackPluginInstance {
}) })
if (hasAppPath) { if (hasAppPath) {
// ensure we reset the cache for sc_server components
// loaded via react-server-dom-webpack
const reactServerDomModId = require.resolve(
'next/dist/compiled/react-server-dom-webpack/client.edge'
)
const reactServerDomMod = require.cache[reactServerDomModId]
if (reactServerDomMod) {
for (const child of reactServerDomMod.children) {
child.parent = null
delete require.cache[child.id]
}
}
delete require.cache[reactServerDomModId]
} }
entries.forEach((page) => { entries.forEach((page) => {
......
This diff is collapsed.
...@@ -3,13 +3,11 @@ ...@@ -3,13 +3,11 @@
import arg from 'next/dist/compiled/arg/index.js' import arg from 'next/dist/compiled/arg/index.js'
import { startServer } from '../server/lib/start-server' import { startServer } from '../server/lib/start-server'
import { getPort, printAndExit } from '../server/lib/utils' import { getPort, printAndExit } from '../server/lib/utils'
import * as Log from '../build/output/log'
import isError from '../lib/is-error' import isError from '../lib/is-error'
import { getProjectDir } from '../lib/get-project-dir' import { getProjectDir } from '../lib/get-project-dir'
import { CliCommand } from '../lib/commands' import { CliCommand } from '../lib/commands'
import { isIPv6 } from 'net'
const nextStart: CliCommand = (argv) => { const nextStart: CliCommand = async (argv) => {
const validArgs: arg.Spec = { const validArgs: arg.Spec = {
// Types // Types
'--help': Boolean, '--help': Boolean,
...@@ -73,22 +71,14 @@ const nextStart: CliCommand = (argv) => { ...@@ -73,22 +71,14 @@ const nextStart: CliCommand = (argv) => {
? Math.ceil(keepAliveTimeoutArg) ? Math.ceil(keepAliveTimeoutArg)
: undefined : undefined
startServer({ await startServer({
dir, dir,
isDev: false,
hostname: host, hostname: host,
port, port,
keepAliveTimeout, keepAliveTimeout,
useWorkers: false,
}) })
.then(async (app) => {
const appUrl = `http://${app.hostname}:${app.port}`
const hostname = isIPv6(host) ? `[${host}]` : host
Log.ready(`started server on ${hostname}:${app.port}, url: ${appUrl}`)
await app.prepare()
})
.catch((err) => {
console.error(err)
process.exit(1)
})
} }
export { nextStart } export { nextStart }
import path from 'path'
import loadConfig from '../server/config'
import { NextConfig } from '../server/config-shared'
import { PHASE_DEVELOPMENT_SERVER } from '../shared/lib/constants'
const supportedTurbopackNextConfigOptions = [
'configFileName',
'env',
'experimental.appDir',
'experimental.serverComponentsExternalPackages',
'experimental.turbo',
'images',
'pageExtensions',
'onDemandEntries',
'rewrites',
'redirects',
'headers',
'reactStrictMode',
'swcMinify',
'transpilePackages',
]
// check for babelrc, swc plugins
export async function validateTurboNextConfig({
dir,
isCustomTurbopack,
}: {
allowRetry?: boolean
isCustomTurbopack?: boolean
dir: string
port: number
hostname?: string
}) {
const { getPkgManager } =
require('../lib/helpers/get-pkg-manager') as typeof import('../lib/helpers/get-pkg-manager')
const { getBabelConfigFile } =
require('../build/webpack-config') as typeof import('../build/webpack-config')
const { defaultConfig } =
require('../server/config-shared') as typeof import('../server/config-shared')
const chalk =
require('next/dist/compiled/chalk') as typeof import('next/dist/compiled/chalk')
const { interopDefault } =
require('../lib/interop-default') as typeof import('../lib/interop-default')
// To regenerate the TURBOPACK gradient require('gradient-string')('blue', 'red')('>>> TURBOPACK')
const isTTY = process.stdout.isTTY
const turbopackGradient = `${chalk.bold(
isTTY
? '\x1B[38;2;0;0;255m>\x1B[39m\x1B[38;2;23;0;232m>\x1B[39m\x1B[38;2;46;0;209m>\x1B[39m \x1B[38;2;70;0;185mT\x1B[39m\x1B[38;2;93;0;162mU\x1B[39m\x1B[38;2;116;0;139mR\x1B[39m\x1B[38;2;139;0;116mB\x1B[39m\x1B[38;2;162;0;93mO\x1B[39m\x1B[38;2;185;0;70mP\x1B[39m\x1B[38;2;209;0;46mA\x1B[39m\x1B[38;2;232;0;23mC\x1B[39m\x1B[38;2;255;0;0mK\x1B[39m'
: '>>> TURBOPACK'
)} ${chalk.dim('(alpha)')}\n\n`
let thankYouMsg = `Thank you for trying Next.js v13 with Turbopack! As a reminder,\nTurbopack is currently in alpha and not yet ready for production.\nWe appreciate your ongoing support as we work to make it ready\nfor everyone.\n`
let unsupportedParts = ''
let babelrc = await getBabelConfigFile(dir)
if (babelrc) babelrc = path.basename(babelrc)
let unsupportedConfig: string[] = []
let rawNextConfig: NextConfig = {}
try {
rawNextConfig = interopDefault(
await loadConfig(PHASE_DEVELOPMENT_SERVER, dir, undefined, true)
) as NextConfig
if (typeof rawNextConfig === 'function') {
rawNextConfig = (rawNextConfig as any)(PHASE_DEVELOPMENT_SERVER, {
defaultConfig,
})
}
const checkUnsupportedCustomConfig = (
configKey = '',
parentUserConfig: any,
parentDefaultConfig: any
): boolean => {
try {
// these should not error
if (
// we only want the key after the dot for experimental options
supportedTurbopackNextConfigOptions
.map((key) => key.split('.').splice(-1)[0])
.includes(configKey)
) {
return false
}
// experimental options are checked separately
if (configKey === 'experimental') {
return false
}
let userValue = parentUserConfig?.[configKey]
let defaultValue = parentDefaultConfig?.[configKey]
if (typeof defaultValue !== 'object') {
return defaultValue !== userValue
}
return Object.keys(userValue || {}).some((key: string) => {
return checkUnsupportedCustomConfig(key, userValue, defaultValue)
})
} catch (e) {
console.error(
`Unexpected error occurred while checking ${configKey}`,
e
)
return false
}
}
unsupportedConfig = [
...Object.keys(rawNextConfig).filter((key) =>
checkUnsupportedCustomConfig(key, rawNextConfig, defaultConfig)
),
...Object.keys(rawNextConfig.experimental ?? {})
.filter((key) =>
checkUnsupportedCustomConfig(
key,
rawNextConfig?.experimental,
defaultConfig?.experimental
)
)
.map((key) => `experimental.${key}`),
]
} catch (e) {
console.error('Unexpected error occurred while checking config', e)
}
const hasWarningOrError = babelrc || unsupportedConfig.length
if (!hasWarningOrError) {
thankYouMsg = chalk.dim(thankYouMsg)
}
if (!isCustomTurbopack) {
console.log(turbopackGradient + thankYouMsg)
}
let feedbackMessage = `Learn more about Next.js v13 and Turbopack: ${chalk.underline(
'https://nextjs.link/with-turbopack'
)}\nPlease direct feedback to: ${chalk.underline(
'https://nextjs.link/turbopack-feedback'
)}\n`
if (!hasWarningOrError) {
feedbackMessage = chalk.dim(feedbackMessage)
}
if (babelrc) {
unsupportedParts += `\n- Babel detected (${chalk.cyan(
babelrc
)})\n ${chalk.dim(
`Babel is not yet supported. To use Turbopack at the moment,\n you'll need to remove your usage of Babel.`
)}`
}
if (unsupportedConfig.length) {
unsupportedParts += `\n\n- Unsupported Next.js configuration option(s) (${chalk.cyan(
'next.config.js'
)})\n ${chalk.dim(
`To use Turbopack, remove the following configuration options:\n${unsupportedConfig
.map((name) => ` - ${chalk.red(name)}\n`)
.join(
''
)} The only supported configurations options are:\n${supportedTurbopackNextConfigOptions
.map((name) => ` - ${chalk.cyan(name)}\n`)
.join('')} `
)} `
}
if (unsupportedParts && !isCustomTurbopack) {
const pkgManager = getPkgManager(dir)
console.error(
`${chalk.bold.red(
'Error:'
)} You are using configuration and/or tools that are not yet\nsupported by Next.js v13 with Turbopack:\n${unsupportedParts}\n
If you cannot make the changes above, but still want to try out\nNext.js v13 with Turbopack, create the Next.js v13 playground app\nby running the following commands:
${chalk.bold.cyan(
`${
pkgManager === 'npm'
? 'npx create-next-app'
: `${pkgManager} create next-app`
} --example with-turbopack with-turbopack-app`
)}\n cd with-turbopack-app\n ${pkgManager} run dev
`
)
if (!isCustomTurbopack) {
console.warn(feedbackMessage)
process.exit(1)
} else {
console.warn(
`\n${chalk.bold.yellow(
'Warning:'
)} Unsupported config found; but continuing with custom Turbopack binary.\n`
)
}
}
if (!isCustomTurbopack) {
console.log(feedbackMessage)
}
return rawNextConfig
}
...@@ -155,6 +155,11 @@ export interface Options { ...@@ -155,6 +155,11 @@ export interface Options {
* The HTTP Server that Next.js is running behind * The HTTP Server that Next.js is running behind
*/ */
httpServer?: import('http').Server httpServer?: import('http').Server
_routerWorker?: boolean
_renderWorker?: boolean
isNodeDebugging?: 'brk' | boolean
} }
export interface BaseRequestHandler { export interface BaseRequestHandler {
...@@ -283,7 +288,7 @@ export default abstract class Server<ServerOptions extends Options = Options> { ...@@ -283,7 +288,7 @@ export default abstract class Server<ServerOptions extends Options = Options> {
protected abstract getCustomRoutes(): CustomRoutes protected abstract getCustomRoutes(): CustomRoutes
protected abstract hasPage(pathname: string): Promise<boolean> protected abstract hasPage(pathname: string): Promise<boolean>
protected abstract generateRoutes(): RouterOptions protected abstract generateRoutes(dev?: boolean): RouterOptions
protected abstract sendRenderResult( protected abstract sendRenderResult(
req: BaseNextRequest, req: BaseNextRequest,
...@@ -337,6 +342,8 @@ export default abstract class Server<ServerOptions extends Options = Options> { ...@@ -337,6 +342,8 @@ export default abstract class Server<ServerOptions extends Options = Options> {
protected readonly handlers: RouteHandlerManager protected readonly handlers: RouteHandlerManager
protected readonly i18nProvider?: I18NProvider protected readonly i18nProvider?: I18NProvider
protected readonly localeNormalizer?: LocaleRouteNormalizer protected readonly localeNormalizer?: LocaleRouteNormalizer
protected readonly isRouterWorker?: boolean
protected readonly isRenderWorker?: boolean
public constructor(options: ServerOptions) { public constructor(options: ServerOptions) {
const { const {
...@@ -350,6 +357,8 @@ export default abstract class Server<ServerOptions extends Options = Options> { ...@@ -350,6 +357,8 @@ export default abstract class Server<ServerOptions extends Options = Options> {
port, port,
} = options } = options
this.serverOptions = options this.serverOptions = options
this.isRouterWorker = options._routerWorker
this.isRenderWorker = options._renderWorker
this.dir = this.dir =
process.env.NEXT_RUNTIME === 'edge' ? dir : require('path').resolve(dir) process.env.NEXT_RUNTIME === 'edge' ? dir : require('path').resolve(dir)
...@@ -456,7 +465,7 @@ export default abstract class Server<ServerOptions extends Options = Options> { ...@@ -456,7 +465,7 @@ export default abstract class Server<ServerOptions extends Options = Options> {
matchers.reload() matchers.reload()
this.customRoutes = this.getCustomRoutes() this.customRoutes = this.getCustomRoutes()
this.router = new Router(this.generateRoutes()) this.router = new Router(this.generateRoutes(dev))
this.setAssetPrefix(assetPrefix) this.setAssetPrefix(assetPrefix)
this.responseCache = this.getResponseCache({ dev }) this.responseCache = this.getResponseCache({ dev })
......
...@@ -366,9 +366,6 @@ const configSchema = { ...@@ -366,9 +366,6 @@ const configSchema = {
pageEnv: { pageEnv: {
type: 'boolean', type: 'boolean',
}, },
preCompiledNextServer: {
type: 'boolean',
},
proxyTimeout: { proxyTimeout: {
minimum: 0, minimum: 0,
type: 'number', type: 'number',
......
...@@ -126,7 +126,6 @@ export interface ExperimentalConfig { ...@@ -126,7 +126,6 @@ export interface ExperimentalConfig {
fetchCacheKeyPrefix?: string fetchCacheKeyPrefix?: string
optimisticClientCache?: boolean optimisticClientCache?: boolean
middlewarePrefetch?: 'strict' | 'flexible' middlewarePrefetch?: 'strict' | 'flexible'
preCompiledNextServer?: boolean
legacyBrowsers?: boolean legacyBrowsers?: boolean
manualClientBasePath?: boolean manualClientBasePath?: boolean
newNextLinkBehavior?: boolean newNextLinkBehavior?: boolean
...@@ -657,7 +656,6 @@ export const defaultConfig: NextConfig = { ...@@ -657,7 +656,6 @@ export const defaultConfig: NextConfig = {
experimental: { experimental: {
clientRouterFilter: false, clientRouterFilter: false,
clientRouterFilterRedirects: false, clientRouterFilterRedirects: false,
preCompiledNextServer: false,
fetchCacheKeyPrefix: '', fetchCacheKeyPrefix: '',
middlewarePrefetch: 'flexible', middlewarePrefetch: 'flexible',
optimisticClientCache: true, optimisticClientCache: true,
......
import v8 from 'v8'
import http from 'http'
import next from '../next'
import { isIPv6 } from 'net'
import { warn } from '../../build/output/log'
import type { RequestHandler } from '../next'
import {
deleteCache as _deleteCache,
deleteAppClientCache as _deleteAppClientCache,
} from '../../build/webpack/plugins/nextjs-require-cache-hot-reloader'
export const WORKER_SELF_EXIT_CODE = 77
const MAXIMUM_HEAP_SIZE_ALLOWED =
(v8.getHeapStatistics().heap_size_limit / 1024 / 1024) * 0.9
let result:
| undefined
| {
port: number
hostname: string
}
export function deleteAppClientCache() {
_deleteAppClientCache()
}
export function deleteCache(filePath: string) {
_deleteCache(filePath)
}
export async function initialize(opts: {
dir: string
port: number
dev: boolean
hostname?: string
workerType: 'router' | 'render'
keepAliveTimeout?: number
}): Promise<NonNullable<typeof result>> {
// if we already setup the server return as we only need to do
// this on first worker boot
if (result) {
return result
}
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)
}
})
})
if (opts.keepAliveTimeout) {
server.keepAliveTimeout = opts.keepAliveTimeout
}
return new Promise((resolve, reject) => {
server.on('error', (err: NodeJS.ErrnoException) => {
console.error(`Invariant: failed to start render worker`, err)
process.exit(1)
})
let upgradeHandler: any
if (!opts.dev) {
server.on('upgrade', (req, socket, upgrade) => {
upgradeHandler(req, socket, upgrade)
})
}
server.on('listening', async () => {
try {
const addr = server.address()
const port = addr && typeof addr === 'object' ? addr.port : 0
if (!port) {
console.error(`Invariant failed to detect render worker port`, addr)
process.exit(1)
}
let hostname =
!opts.hostname || opts.hostname === '0.0.0.0'
? 'localhost'
: opts.hostname
if (isIPv6(hostname)) {
hostname = hostname === '::' ? '[::1]' : `[${hostname}]`
}
result = {
port,
hostname,
}
const app = next({
...opts,
_routerWorker: opts.workerType === 'router',
_renderWorker: opts.workerType === 'render',
hostname,
customServer: false,
httpServer: server,
port: opts.port,
})
requestHandler = app.getRequestHandler()
upgradeHandler = app.getUpgradeHandler()
await app.prepare()
resolve(result)
} catch (err) {
return reject(err)
}
})
server.listen(0, opts.hostname)
})
}
...@@ -176,7 +176,7 @@ export async function makeResolver( ...@@ -176,7 +176,7 @@ export async function makeResolver(
devServer.hasMiddleware = () => true devServer.hasMiddleware = () => true
} }
const routes = devServer.generateRoutes() const routes = devServer.generateRoutes(true)
// @ts-expect-error protected // @ts-expect-error protected
const catchAllMiddleware = devServer.generateCatchAllMiddlewareRoute(true) const catchAllMiddleware = devServer.generateCatchAllMiddlewareRoute(true)
......
import type NextServer from '../next-server'
import { genExecArgv, getNodeOptionsWithoutInspect } from './utils'
import { deserializeErr, errorToJSON } from '../render'
// we can't use process.send as jest-worker relies on
// it already and can cause unexpected message errors
// so we create an IPC server for communicating
export async function createIpcServer(
server: InstanceType<typeof NextServer>
): Promise<{
ipcPort: number
ipcServer: import('http').Server
}> {
const ipcServer = (require('http') as typeof import('http')).createServer(
async (req, res) => {
try {
const url = new URL(req.url || '/', 'http://n')
const method = url.searchParams.get('method')
const args: any[] = JSON.parse(url.searchParams.get('args') || '[]')
if (!method || !Array.isArray(args)) {
return res.end()
}
if (typeof (server as any)[method] === 'function') {
if (method === 'logErrorWithOriginalStack' && args[0]?.stack) {
args[0] = deserializeErr(args[0])
}
let result = await (server as any)[method](...args)
if (result && typeof result === 'object' && result.stack) {
result = errorToJSON(result)
}
res.end(JSON.stringify(result || ''))
}
} catch (err: any) {
console.error(err)
res.end(
JSON.stringify({
err: { name: err.name, message: err.message, stack: err.stack },
})
)
}
}
)
const ipcPort = await new Promise<number>((resolveIpc) => {
ipcServer.listen(0, server.hostname, () => {
const addr = ipcServer.address()
if (addr && typeof addr === 'object') {
resolveIpc(addr.port)
}
})
})
return {
ipcPort,
ipcServer,
}
}
export const createWorker = (
serverPort: number,
ipcPort: number,
isNodeDebugging: boolean | 'brk' | undefined,
type: string
) => {
const { initialEnv } = require('@next/env') as typeof import('@next/env')
const { Worker } = require('next/dist/compiled/jest-worker')
const worker = new Worker(require.resolve('./render-server'), {
numWorkers: 1,
// TODO: do we want to allow more than 10 OOM restarts?
maxRetries: 10,
forkOptions: {
env: {
FORCE_COLOR: '1',
...initialEnv,
// we don't pass down NODE_OPTIONS as it can
// extra memory usage
NODE_OPTIONS: getNodeOptionsWithoutInspect()
.replace(/--max-old-space-size=[\d]{1,}/, '')
.trim(),
__NEXT_PRIVATE_RENDER_WORKER: type,
__NEXT_PRIVATE_ROUTER_IPC_PORT: ipcPort + '',
NODE_ENV: process.env.NODE_ENV,
},
execArgv: genExecArgv(
isNodeDebugging === undefined ? false : isNodeDebugging,
(serverPort || 0) + 1
),
},
exposedMethods: ['initialize', 'deleteCache', 'deleteAppClientCache'],
}) as any as InstanceType<typeof Worker> & {
initialize: typeof import('./render-server').initialize
deleteCache: typeof import('./render-server').deleteCache
deleteAppClientCache: typeof import('./render-server').deleteAppClientCache
}
worker.getStderr().pipe(process.stderr)
worker.getStdout().pipe(process.stdout)
return worker
}
...@@ -10,6 +10,22 @@ export function printAndExit(message: string, code = 1) { ...@@ -10,6 +10,22 @@ export function printAndExit(message: string, code = 1) {
process.exit(code) process.exit(code)
} }
export const genExecArgv = (enabled: boolean | 'brk', debugPort: number) => {
const execArgv = process.execArgv.filter((localArg) => {
return (
!localArg.startsWith('--inspect') && !localArg.startsWith('--inspect-brk')
)
})
if (enabled) {
execArgv.push(
`--inspect${enabled === 'brk' ? '-brk' : ''}=${debugPort + 1}`
)
}
return execArgv
}
export function getNodeOptionsWithoutInspect() { export function getNodeOptionsWithoutInspect() {
const NODE_INSPECT_RE = /--inspect(-brk)?(=\S+)?( |$)/ const NODE_INSPECT_RE = /--inspect(-brk)?(=\S+)?( |$)/
return (process.env.NODE_OPTIONS || '').replace(NODE_INSPECT_RE, '') return (process.env.NODE_OPTIONS || '').replace(NODE_INSPECT_RE, '')
......
import * as Log from '../../build/output/log'
export const genRenderExecArgv = () => {
const isDebugging =
process.execArgv.some((localArg) => localArg.startsWith('--inspect')) ||
process.env.NODE_OPTIONS?.match?.(/--inspect(=\S+)?( |$)/)
const isDebuggingWithBrk =
process.execArgv.some((localArg) => localArg.startsWith('--inspect-brk')) ||
process.env.NODE_OPTIONS?.match?.(/--inspect-brk(=\S+)?( |$)/)
const debugPort = (() => {
const debugPortStr =
process.execArgv
.find(
(localArg) =>
localArg.startsWith('--inspect') ||
localArg.startsWith('--inspect-brk')
)
?.split('=')[1] ??
process.env.NODE_OPTIONS?.match?.(/--inspect(-brk)?(=(\S+))?( |$)/)?.[3]
return debugPortStr ? parseInt(debugPortStr, 10) : 9229
})()
if (isDebugging || isDebuggingWithBrk) {
Log.warn(
`the --inspect${
isDebuggingWithBrk ? '-brk' : ''
} option was detected, the Next.js server should be inspected at port ${
debugPort + 1
}.`
)
}
const execArgv = process.execArgv.filter((localArg) => {
return (
!localArg.startsWith('--inspect') && !localArg.startsWith('--inspect-brk')
)
})
if (isDebugging || isDebuggingWithBrk) {
execArgv.push(
`--inspect${isDebuggingWithBrk ? '-brk' : ''}=${debugPort + 1}`
)
}
return execArgv
}
This diff is collapsed.
...@@ -147,7 +147,9 @@ export class NextServer { ...@@ -147,7 +147,9 @@ export class NextServer {
return loadConfig( return loadConfig(
this.options.dev ? PHASE_DEVELOPMENT_SERVER : PHASE_PRODUCTION_SERVER, this.options.dev ? PHASE_DEVELOPMENT_SERVER : PHASE_PRODUCTION_SERVER,
resolve(this.options.dir || '.'), resolve(this.options.dir || '.'),
this.options.conf this.options.conf,
undefined,
!!this.options._renderWorker
) )
} }
......
...@@ -331,7 +331,28 @@ function checkRedirectValues( ...@@ -331,7 +331,28 @@ function checkRedirectValues(
} }
} }
function errorToJSON(err: Error) { export const deserializeErr = (serializedErr: any) => {
if (
!serializedErr ||
typeof serializedErr !== 'object' ||
!serializedErr.stack
) {
return serializedErr
}
const err = new Error(serializedErr.message)
err.stack = serializedErr.stack
err.name = serializedErr.name
;(err as any).digest = serializedErr.digest
if (process.env.NEXT_RUNTIME !== 'edge') {
const { decorateServerError } =
require('next/dist/compiled/@next/react-dev-overlay/dist/middleware') as typeof import('next/dist/compiled/@next/react-dev-overlay/dist/middleware')
decorateServerError(err, serializedErr.source || 'server')
}
return err
}
export function errorToJSON(err: Error) {
let source: typeof COMPILER_NAMES.server | typeof COMPILER_NAMES.edgeServer = let source: typeof COMPILER_NAMES.server | typeof COMPILER_NAMES.edgeServer =
'server' 'server'
...@@ -347,6 +368,7 @@ function errorToJSON(err: Error) { ...@@ -347,6 +368,7 @@ function errorToJSON(err: Error) {
source, source,
message: stripAnsi(err.message), message: stripAnsi(err.message),
stack: err.stack, stack: err.stack,
digest: (err as any).digest,
} }
} }
......
import { promises } from 'fs' import fs, { promises } from 'fs'
import { join } from 'path' import { join } from 'path'
import { import {
FONT_MANIFEST, FONT_MANIFEST,
...@@ -13,18 +13,25 @@ import type { PagesManifest } from '../build/webpack/plugins/pages-manifest-plug ...@@ -13,18 +13,25 @@ import type { PagesManifest } from '../build/webpack/plugins/pages-manifest-plug
import { PageNotFoundError, MissingStaticPage } from '../shared/lib/utils' import { PageNotFoundError, MissingStaticPage } from '../shared/lib/utils'
import LRUCache from 'next/dist/compiled/lru-cache' import LRUCache from 'next/dist/compiled/lru-cache'
const pagePathCache = const isDev = process.env.NODE_ENV === 'development'
process.env.NODE_ENV === 'development' const pagePathCache = isDev
? { ? {
get: (_key: string) => { get: (_key: string) => {
return null return null
}, },
set: () => {}, set: () => {},
has: () => false, has: () => false,
} }
: new LRUCache<string, string | null>({ : new LRUCache<string, string | null>({
max: 1000, max: 1000,
}) })
const loadManifest = (manifestPath: string) => {
if (isDev) {
return JSON.parse(fs.readFileSync(manifestPath, 'utf8'))
}
return require(manifestPath)
}
export function getMaybePagePath( export function getMaybePagePath(
page: string, page: string,
...@@ -42,12 +49,11 @@ export function getMaybePagePath( ...@@ -42,12 +49,11 @@ export function getMaybePagePath(
let appPathsManifest: undefined | PagesManifest let appPathsManifest: undefined | PagesManifest
if (isAppPath) { if (isAppPath) {
appPathsManifest = require(join(serverBuildPath, APP_PATHS_MANIFEST)) appPathsManifest = loadManifest(join(serverBuildPath, APP_PATHS_MANIFEST))
} }
const pagesManifest = require(join( const pagesManifest = loadManifest(
serverBuildPath, join(serverBuildPath, PAGES_MANIFEST)
PAGES_MANIFEST ) as PagesManifest
)) as PagesManifest
try { try {
page = denormalizePagePath(normalizePagePath(page)) page = denormalizePagePath(normalizePagePath(page))
......
...@@ -347,7 +347,56 @@ export default class Router { ...@@ -347,7 +347,56 @@ export default class Router {
}, },
} }
for (const route of this.compiledRoutes) { // when x-invoke-path is specified we can short short circuit resolving
// we only honor this header if we are inside of a render worker to
// prevent external users coercing the routing path
const matchedPath = req.headers['x-invoke-path'] as string
const curRoutes = matchedPath
? this.compiledRoutes.filter((r) => {
return (
r.name === 'Catchall render' || r.name === '_next/data catchall'
)
})
: this.compiledRoutes
if (
process.env.NEXT_RUNTIME !== 'edge' &&
process.env.__NEXT_PRIVATE_RENDER_WORKER &&
matchedPath
) {
const parsedMatchedPath = new URL(matchedPath || '/', 'http://n')
const pathnameInfo = getNextPathnameInfo(parsedMatchedPath.pathname, {
nextConfig: this.nextConfig,
parseData: false,
})
if (pathnameInfo.locale) {
parsedUrlUpdated.query.__nextLocale = pathnameInfo.locale
}
if (parsedUrlUpdated.pathname !== parsedMatchedPath.pathname) {
parsedUrlUpdated.pathname = parsedMatchedPath.pathname
addRequestMeta(req, '_nextRewroteUrl', pathnameInfo.pathname)
addRequestMeta(req, '_nextDidRewrite', true)
}
for (const key of Object.keys(parsedUrlUpdated.query)) {
if (!key.startsWith('__next') && !key.startsWith('_next')) {
delete parsedUrlUpdated.query[key]
}
}
const invokeQuery = req.headers['x-invoke-query']
if (typeof invokeQuery === 'string') {
Object.assign(
parsedUrlUpdated.query,
JSON.parse(decodeURIComponent(invokeQuery))
)
}
}
for (const route of curRoutes) {
// only process rewrites for upgrade request // only process rewrites for upgrade request
if (upgradeHead && route.type !== 'rewrite') { if (upgradeHead && route.type !== 'rewrite') {
continue continue
......
...@@ -100,79 +100,6 @@ export async function capsize_metrics() { ...@@ -100,79 +100,6 @@ export async function capsize_metrics() {
await fs.outputJson(outputPathDist, entireMetricsCollection, { spaces: 2 }) await fs.outputJson(outputPathDist, entireMetricsCollection, { spaces: 2 })
} }
export async function ncc_next_server(task, opts) {
await task
.source(
opts.src ||
relative(__dirname, require.resolve('next/dist/server/next-server'))
)
.ncc({
bundleName: 'next-server',
// minify: false,
externals: {
...externals,
'/(.*)route-resolver/': '$1route-resolver',
sharp: 'sharp',
react: 'react',
'react-dom': 'react-dom',
'next/dist/compiled/compression': 'next/dist/compiled/compression',
critters: 'critters',
'next/dist/compiled/jest-worker': 'next/dist/compiled/jest-worker',
'next/dist/compiled/react': 'next/dist/compiled/react',
'/next/dist/compiled/react(/.+)/': 'next/dist/compiled/react$1',
'next/dist/compiled/react-dom': 'next/dist/compiled/react-dom',
'/next/dist/compiled/react-dom(/.+)/': 'next/dist/compiled/react-dom$1',
// react contexts must be external
'/(.*)server-inserted-html/':
'next/dist/shared/lib/server-inserted-html.js',
'/(.+/)router-context/': 'next/dist/shared/lib/router-context.js',
'/(.*)loadable-context/': 'next/dist/shared/lib/loadable-context.js',
'/(.*)image-config-context/':
'next/dist/shared/lib/image-config-context.js',
'/(.*)head-manager-context/':
'next/dist/shared/lib/head-manager-context.js',
'/(.*)app-router-context/':
'next/dist/shared/lib/app-router-context.js',
'/(.*)amp-context/': 'next/dist/shared/lib/amp-context.js',
'/(.*)hooks-client-context/':
'next/dist/shared/lib/hooks-client-context.js',
'/(.*)html-context/': 'next/dist/shared/lib/html-context.js',
// 'next/dist/compiled/undici': 'next/dist/compiled/undici',
// 'next/dist/compiled/node-fetch': 'next/dist/compiled/node-fetch',
// '/(.*)google-font-metrics.json/': '$1google-font-metrics.json',
'/(.*)next-config-validate.js/': '$1/next-config-validate.js',
'/(.*)server/web(.*)/': '$1server/web$2',
'./web/sandbox': './web/sandbox',
'next/dist/compiled/edge-runtime': 'next/dist/compiled/edge-runtime',
'(.*)@edge-runtime/primitives': '$1@edge-runtime/primitives',
'/(.*)compiled/webpack(/.*)/': '$1webpack$2',
'./image-optimizer': './image-optimizer',
'/(.*)@ampproject/toolbox-optimizer/':
'$1@ampproject/toolbox-optimizer',
},
})
.target('dist/compiled/next-server')
}
// eslint-disable-next-line camelcase // eslint-disable-next-line camelcase
externals['@babel/runtime'] = 'next/dist/compiled/@babel/runtime' externals['@babel/runtime'] = 'next/dist/compiled/@babel/runtime'
export async function copy_babel_runtime(task, opts) { export async function copy_babel_runtime(task, opts) {
...@@ -2328,7 +2255,6 @@ export async function compile(task, opts) { ...@@ -2328,7 +2255,6 @@ export async function compile(task, opts) {
'ncc_react_refresh_utils', 'ncc_react_refresh_utils',
'ncc_next__react_dev_overlay', 'ncc_next__react_dev_overlay',
'ncc_next_font', 'ncc_next_font',
'ncc_next_server',
'capsize_metrics', 'capsize_metrics',
]) ])
} }
......
...@@ -14,11 +14,11 @@ createNextDescribe( ...@@ -14,11 +14,11 @@ createNextDescribe(
await check(() => next.cliOutput.slice(outputIndex), /at Page/) await check(() => next.cliOutput.slice(outputIndex), /at Page/)
const cliOutput = next.cliOutput.slice(outputIndex) const cliOutput = next.cliOutput.slice(outputIndex)
await check(() => cliOutput, /digest:/)
expect(cliOutput).toInclude('Error: boom') expect(cliOutput).toInclude('Error: boom')
expect(cliOutput).toInclude('at fn2 (./app/fn.ts:6:11)') expect(cliOutput).toInclude('at fn2 (./app/fn.ts:6:11)')
expect(cliOutput).toInclude('at fn1 (./app/fn.ts:9:5') expect(cliOutput).toInclude('at fn1 (./app/fn.ts:9:5')
expect(cliOutput).toInclude('at Page (./app/page.tsx:10:45)') expect(cliOutput).toInclude('at Page (./app/page.tsx:10:45)')
expect(cliOutput).toInclude('digest: ')
expect(cliOutput).not.toInclude('webpack-internal') expect(cliOutput).not.toInclude('webpack-internal')
}) })
...@@ -30,11 +30,11 @@ createNextDescribe( ...@@ -30,11 +30,11 @@ createNextDescribe(
await check(() => next.cliOutput.slice(outputIndex), /at EdgePage/) await check(() => next.cliOutput.slice(outputIndex), /at EdgePage/)
const cliOutput = next.cliOutput.slice(outputIndex) const cliOutput = next.cliOutput.slice(outputIndex)
await check(() => cliOutput, /digest:/)
expect(cliOutput).toInclude('Error: boom') expect(cliOutput).toInclude('Error: boom')
expect(cliOutput).toInclude('at fn2 (./app/fn.ts:6:11)') expect(cliOutput).toInclude('at fn2 (./app/fn.ts:6:11)')
expect(cliOutput).toInclude('at fn1 (./app/fn.ts:9:5') expect(cliOutput).toInclude('at fn1 (./app/fn.ts:9:5')
expect(cliOutput).toInclude('at EdgePage (./app/edge/page.tsx:12:45)') expect(cliOutput).toInclude('at EdgePage (./app/edge/page.tsx:12:45)')
expect(cliOutput).toInclude('digest: ')
expect(cliOutput).not.toInclude('webpack-internal') expect(cliOutput).not.toInclude('webpack-internal')
}) })
......
...@@ -19,8 +19,9 @@ createNextDescribe( ...@@ -19,8 +19,9 @@ createNextDescribe(
it('should warn if @next/font is in deps', async () => { it('should warn if @next/font is in deps', async () => {
await next.start() await next.start()
await check(() => next.cliOutput, /compiled client and server/) await check(() => next.cliOutput, /compiled client and server/)
expect(next.cliOutput).toInclude( await check(
'please use the built-in `next/font` instead' () => next.cliOutput,
new RegExp('please use the built-in `next/font` instead')
) )
await next.stop() await next.stop()
......
...@@ -184,7 +184,9 @@ createNextDescribe( ...@@ -184,7 +184,9 @@ createNextDescribe(
const res = await next.fetch('/dashboard') const res = await next.fetch('/dashboard')
expect(res.headers.get('x-edge-runtime')).toBe('1') expect(res.headers.get('x-edge-runtime')).toBe('1')
expect(res.headers.get('vary')).toBe( expect(res.headers.get('vary')).toBe(
'RSC, Next-Router-State-Tree, Next-Router-Prefetch' isNextDeploy || isNextStart
? 'RSC, Next-Router-State-Tree, Next-Router-Prefetch'
: 'RSC, Next-Router-State-Tree, Next-Router-Prefetch, Accept-Encoding'
) )
}) })
......
import { useRouter } from 'next/router' import { useRouter } from 'next/router'
import Link from 'next/link' import Link from 'next/link'
import { useState } from 'react'
import { useEffect } from 'react'
export const getStaticProps = () => { export const getStaticProps = () => {
return { return {
...@@ -12,6 +14,11 @@ export const getStaticProps = () => { ...@@ -12,6 +14,11 @@ export const getStaticProps = () => {
export default function Index({ hello, nested }) { export default function Index({ hello, nested }) {
const { query, pathname, asPath } = useRouter() const { query, pathname, asPath } = useRouter()
const [mounted, setMounted] = useState(false)
useEffect(() => {
setMounted(true)
return () => setMounted(false)
}, [])
return ( return (
<> <>
<h1 id="index-page">index page</h1> <h1 id="index-page">index page</h1>
...@@ -19,7 +26,7 @@ export default function Index({ hello, nested }) { ...@@ -19,7 +26,7 @@ export default function Index({ hello, nested }) {
<p id="prop">{hello} world</p> <p id="prop">{hello} world</p>
<p id="query">{JSON.stringify(query)}</p> <p id="query">{JSON.stringify(query)}</p>
<p id="pathname">{pathname}</p> <p id="pathname">{pathname}</p>
<p id="as-path">{asPath}</p> <p id="as-path">{mounted ? asPath : ''}</p>
<Link href="/hello" id="hello-link"> <Link href="/hello" id="hello-link">
to /hello to /hello
</Link> </Link>
......
...@@ -5,10 +5,15 @@ export default (props) => ( ...@@ -5,10 +5,15 @@ export default (props) => (
</> </>
) )
export async function getServerSideProps({ res }) { export async function getServerSideProps({ req, res }) {
return { return {
props: { props: {
fromMiddleware: res.getHeader('x-from-middleware') || null, // TODO: this should only use request header once
fromMiddleware:
// start is using the separate renders as well
req.headers['x-from-middleware'] ||
res.getHeader('x-from-middleware') ||
null,
}, },
} }
} }
...@@ -5,10 +5,15 @@ export default (props) => ( ...@@ -5,10 +5,15 @@ export default (props) => (
</> </>
) )
export async function getServerSideProps({ res }) { export async function getServerSideProps({ req, res }) {
return { return {
props: { props: {
fromMiddleware: res.getHeader('x-from-middleware') || null, fromMiddleware:
// TODO: this should only use request header once
// start is using the separate renders as well
req.headers['x-from-middleware'] ||
res.getHeader('x-from-middleware') ||
null,
}, },
} }
} }
...@@ -5,10 +5,15 @@ export default (props) => ( ...@@ -5,10 +5,15 @@ export default (props) => (
</> </>
) )
export async function getServerSideProps({ res }) { export async function getServerSideProps({ req, res }) {
return { return {
props: { props: {
fromMiddleware: res.getHeader('x-from-middleware') || null, fromMiddleware:
// TODO: this should only use request header once
// start is using the separate renders as well
req.headers['x-from-middleware'] ||
res.getHeader('x-from-middleware') ||
null,
}, },
} }
} }
...@@ -42,7 +42,9 @@ describe('Requests not effected when middleware used', () => { ...@@ -42,7 +42,9 @@ describe('Requests not effected when middleware used', () => {
const data = await res.json() const data = await res.json()
expect(data.method).toEqual(method) expect(data.method).toEqual(method)
if (body) { if (body) {
expect(data.headers['content-length']).toEqual(String(body.length)) expect(data.headers['content-length'] || String(body.length)).toEqual(
String(body.length)
)
} }
expect(data.headers).toEqual(expect.objectContaining(headers)) expect(data.headers).toEqual(expect.objectContaining(headers))
}) })
......
...@@ -12,6 +12,7 @@ import { ...@@ -12,6 +12,7 @@ import {
fetchViaHTTP, fetchViaHTTP,
waitFor, waitFor,
getPageFileFromPagesManifest, getPageFileFromPagesManifest,
check,
} from 'next-test-utils' } from 'next-test-utils'
const appDir = join(__dirname, '../') const appDir = join(__dirname, '../')
...@@ -206,14 +207,14 @@ describe('404 Page Support', () => { ...@@ -206,14 +207,14 @@ describe('404 Page Support', () => {
}, },
}) })
await renderViaHTTP(appPort, '/abc') await renderViaHTTP(appPort, '/abc')
await waitFor(1000) try {
await check(() => stderr, gip404Err)
await killApp(app) } finally {
await killApp(app)
await fs.remove(pages404)
await fs.move(`${pages404}.bak`, pages404) await fs.remove(pages404)
await fs.move(`${pages404}.bak`, pages404)
expect(stderr).toMatch(gip404Err) }
}) })
it('does not show error with getStaticProps in pages/404 build', async () => { it('does not show error with getStaticProps in pages/404 build', async () => {
......
...@@ -9,6 +9,7 @@ import { ...@@ -9,6 +9,7 @@ import {
nextBuild, nextBuild,
waitFor, waitFor,
} from 'next-test-utils' } from 'next-test-utils'
import stripAnsi from 'strip-ansi'
const dir = path.join(__dirname, '..') const dir = path.join(__dirname, '..')
const nextConfig = path.join(dir, 'next.config.js') const nextConfig = path.join(dir, 'next.config.js')
...@@ -56,10 +57,10 @@ describe('Error when app dir is present without experimental.appDir', () => { ...@@ -56,10 +57,10 @@ describe('Error when app dir is present without experimental.appDir', () => {
let output = '' let output = ''
app = await launchApp(dir, appPort, { app = await launchApp(dir, appPort, {
onStdout(data: string) { onStdout(data: string) {
output += data output += stripAnsi(data)
}, },
onStderr(data: string) { onStderr(data: string) {
output += data output += stripAnsi(data)
}, },
}) })
await waitFor(200) await waitFor(200)
......
...@@ -3,7 +3,8 @@ ...@@ -3,7 +3,8 @@
import fs from 'fs-extra' import fs from 'fs-extra'
import { join } from 'path' import { join } from 'path'
import webdriver from 'next-webdriver' import webdriver from 'next-webdriver'
import { findPort, launchApp, killApp, waitFor } from 'next-test-utils' import { findPort, launchApp, killApp, waitFor, check } from 'next-test-utils'
import stripAnsi from 'strip-ansi'
const appDir = join(__dirname, '..') const appDir = join(__dirname, '..')
const nextConfig = join(appDir, 'next.config.js') const nextConfig = join(appDir, 'next.config.js')
...@@ -46,8 +47,11 @@ describe('Build Activity Indicator', () => { ...@@ -46,8 +47,11 @@ describe('Build Activity Indicator', () => {
}) })
await fs.remove(configPath) await fs.remove(configPath)
expect(stderr).toContain( await check(
`Invalid "devIndicator.buildActivityPosition" provided, expected one of top-left, top-right, bottom-left, bottom-right, received ttop-leff` () => stripAnsi(stderr),
new RegExp(
`Invalid "devIndicator.buildActivityPosition" provided, expected one of top-left, top-right, bottom-left, bottom-right, received ttop-leff`
)
) )
if (app) { if (app) {
......
/* eslint-env jest */ /* eslint-env jest */
import { import {
check,
findPort, findPort,
killApp, killApp,
launchApp, launchApp,
...@@ -393,9 +394,11 @@ describe('CLI Usage', () => { ...@@ -393,9 +394,11 @@ describe('CLI Usage', () => {
}, },
}) })
expect(stderr).toMatch('both `sass` and `node-sass` installed') try {
await check(() => stderr, /both `sass` and `node-sass` installed/)
await killApp(instance) } finally {
await killApp(instance)
}
}) })
test('should exit when SIGINT is signalled', async () => { test('should exit when SIGINT is signalled', async () => {
......
...@@ -1130,7 +1130,13 @@ function runTests({ dev }) { ...@@ -1130,7 +1130,13 @@ function runTests({ dev }) {
found++ found++
} }
} }
expect(found).toBe(0)
try {
expect(found).toBe(0)
} catch (err) {
require('console').error(html)
throw err
}
}) })
if (dev) { if (dev) {
......
...@@ -5,6 +5,7 @@ import stripAnsi from 'next/dist/compiled/strip-ansi' ...@@ -5,6 +5,7 @@ import stripAnsi from 'next/dist/compiled/strip-ansi'
import { remove } from 'fs-extra' import { remove } from 'fs-extra'
import { join } from 'path' import { join } from 'path'
import { import {
check,
fetchViaHTTP, fetchViaHTTP,
File, File,
findPort, findPort,
...@@ -45,9 +46,9 @@ describe('Edge runtime code with imports', () => { ...@@ -45,9 +46,9 @@ describe('Edge runtime code with imports', () => {
await remove(join(__dirname, '../.next')) await remove(join(__dirname, '../.next'))
}) })
afterEach(() => { afterEach(async () => {
if (context.app) { if (context.app) {
killApp(context.app) await killApp(context.app)
} }
context.api.restore() context.api.restore()
context.middleware.restore() context.middleware.restore()
...@@ -97,11 +98,14 @@ describe('Edge runtime code with imports', () => { ...@@ -97,11 +98,14 @@ describe('Edge runtime code with imports', () => {
context.app = await launchApp(context.appDir, context.appPort, appOption) context.app = await launchApp(context.appDir, context.appPort, appOption)
const res = await fetchViaHTTP(context.appPort, url) const res = await fetchViaHTTP(context.appPort, url)
expect(res.status).toBe(500) expect(res.status).toBe(500)
expectUnsupportedModuleDevError( await check(async () => {
moduleName, expectUnsupportedModuleDevError(
importStatement, moduleName,
await res.text() importStatement,
) await res.text()
)
return 'success'
}, 'success')
}) })
it('throws unsupported module error in production at runtime and prints error on logs', async () => { it('throws unsupported module error in production at runtime and prints error on logs', async () => {
...@@ -157,11 +161,14 @@ describe('Edge runtime code with imports', () => { ...@@ -157,11 +161,14 @@ describe('Edge runtime code with imports', () => {
context.app = await launchApp(context.appDir, context.appPort, appOption) context.app = await launchApp(context.appDir, context.appPort, appOption)
const res = await fetchViaHTTP(context.appPort, url) const res = await fetchViaHTTP(context.appPort, url)
expect(res.status).toBe(500) expect(res.status).toBe(500)
expectUnsupportedModuleDevError( await check(async () => {
moduleName, expectUnsupportedModuleDevError(
importStatement, moduleName,
await res.text() importStatement,
) await res.text()
)
return 'success'
}, 'success')
}) })
it('throws unsupported module error in production at runtime and prints error on logs', async () => { it('throws unsupported module error in production at runtime and prints error on logs', async () => {
...@@ -230,11 +237,14 @@ describe('Edge runtime code with imports', () => { ...@@ -230,11 +237,14 @@ describe('Edge runtime code with imports', () => {
) )
const res = await fetchViaHTTP(context.appPort, url) const res = await fetchViaHTTP(context.appPort, url)
expect(res.status).toBe(500) expect(res.status).toBe(500)
expectUnsupportedModuleDevError( await check(async () => {
moduleName, expectUnsupportedModuleDevError(
importStatement, moduleName,
await res.text() importStatement,
) await res.text()
)
return 'success'
}, 'success')
}) })
it('throws unsupported module error in production at runtime and prints error on logs', async () => { it('throws unsupported module error in production at runtime and prints error on logs', async () => {
...@@ -296,11 +306,15 @@ describe('Edge runtime code with imports', () => { ...@@ -296,11 +306,15 @@ describe('Edge runtime code with imports', () => {
context.app = await launchApp(context.appDir, context.appPort, appOption) context.app = await launchApp(context.appDir, context.appPort, appOption)
const res = await fetchViaHTTP(context.appPort, url) const res = await fetchViaHTTP(context.appPort, url)
expect(res.status).toBe(500) expect(res.status).toBe(500)
expectModuleNotFoundDevError(
moduleName, await check(async () => {
importStatement, expectModuleNotFoundDevError(
await res.text() moduleName,
) importStatement,
await res.text()
)
return 'success'
}, 'success')
}) })
it('does not build and reports', async () => { it('does not build and reports', async () => {
...@@ -353,11 +367,15 @@ describe('Edge runtime code with imports', () => { ...@@ -353,11 +367,15 @@ describe('Edge runtime code with imports', () => {
context.app = await launchApp(context.appDir, context.appPort, appOption) context.app = await launchApp(context.appDir, context.appPort, appOption)
const res = await fetchViaHTTP(context.appPort, url) const res = await fetchViaHTTP(context.appPort, url)
expect(res.status).toBe(500) expect(res.status).toBe(500)
expectModuleNotFoundDevError(
moduleName, await check(async () => {
importStatement, expectModuleNotFoundDevError(
await res.text() moduleName,
) importStatement,
await res.text()
)
return 'success'
}, 'success')
}) })
it('does not build and reports module not found error', async () => { it('does not build and reports module not found error', async () => {
...@@ -414,11 +432,14 @@ describe('Edge runtime code with imports', () => { ...@@ -414,11 +432,14 @@ describe('Edge runtime code with imports', () => {
context.app = await launchApp(context.appDir, context.appPort, appOption) context.app = await launchApp(context.appDir, context.appPort, appOption)
const res = await fetchViaHTTP(context.appPort, url) const res = await fetchViaHTTP(context.appPort, url)
expect(res.status).toBe(500) expect(res.status).toBe(500)
expectModuleNotFoundDevError( await check(async () => {
moduleName, expectModuleNotFoundDevError(
importStatement, moduleName,
await res.text() importStatement,
) await res.text()
)
return 'success'
}, 'success')
}) })
it('does not build and reports module not found error', async () => { it('does not build and reports module not found error', async () => {
...@@ -428,6 +449,7 @@ describe('Edge runtime code with imports', () => { ...@@ -428,6 +449,7 @@ describe('Edge runtime code with imports', () => {
stderr: true, stderr: true,
}) })
expect(code).toEqual(1) expect(code).toEqual(1)
expectModuleNotFoundProdError(moduleName, stderr) expectModuleNotFoundProdError(moduleName, stderr)
}) })
}) })
......
import stripAnsi from 'next/dist/compiled/strip-ansi' import stripAnsi from 'next/dist/compiled/strip-ansi'
import { import {
check,
fetchViaHTTP, fetchViaHTTP,
findPort, findPort,
killApp, killApp,
...@@ -19,11 +20,11 @@ function test(context: ReturnType<typeof createContext>) { ...@@ -19,11 +20,11 @@ function test(context: ReturnType<typeof createContext>) {
expect(await res.text()).toEqual('hello') expect(await res.text()).toEqual('hello')
expect(res.status).toBe(200) expect(res.status).toBe(200)
await waitFor(200) await waitFor(200)
const santizedOutput = stripAnsi(context.output) await check(
expect(santizedOutput).toMatch( () => stripAnsi(context.output),
new RegExp(`TypeError: This ReadableStream did not return bytes.`, 'm') new RegExp(`This ReadableStream did not return bytes.`, 'm')
) )
expect(santizedOutput).not.toContain('webpack-internal:') expect(stripAnsi(context.output)).not.toContain('webpack-internal:')
} }
} }
......
...@@ -26,7 +26,12 @@ const expectStatus = async (path) => { ...@@ -26,7 +26,12 @@ const expectStatus = async (path) => {
const parsedUrl = url.parse(redirectDest, true) const parsedUrl = url.parse(redirectDest, true)
expect(parsedUrl.hostname).toBe('localhost') expect(parsedUrl.hostname).toBe('localhost')
} else { } else {
expect(res.status === 400 || res.status === 404).toBe(true) try {
expect(res.status === 400 || res.status === 404).toBe(true)
} catch (err) {
require('console').error({ path, status: res.status })
throw err
}
expect(await res.text()).toMatch(containRegex) expect(await res.text()).toMatch(containRegex)
} }
} }
......
...@@ -37,11 +37,11 @@ describe('Middleware development errors', () => { ...@@ -37,11 +37,11 @@ describe('Middleware development errors', () => {
}) })
}) })
afterEach(() => { afterEach(async () => {
context.middleware.restore() context.middleware.restore()
context.page.restore() context.page.restore()
if (context.app) { if (context.app) {
killApp(context.app) await killApp(context.app)
} }
}) })
...@@ -56,7 +56,8 @@ describe('Middleware development errors', () => { ...@@ -56,7 +56,8 @@ describe('Middleware development errors', () => {
it('logs the error correctly', async () => { it('logs the error correctly', async () => {
await fetchViaHTTP(context.appPort, '/') await fetchViaHTTP(context.appPort, '/')
const output = stripAnsi(context.logs.output) const output = stripAnsi(context.logs.output)
expect(output).toMatch( await check(
() => stripAnsi(context.logs.output),
new RegExp( new RegExp(
`error - middleware.js \\(\\d+:\\d+\\) @ Object.default \\[as handler\\]\nerror - boom`, `error - middleware.js \\(\\d+:\\d+\\) @ Object.default \\[as handler\\]\nerror - boom`,
'm' 'm'
...@@ -91,7 +92,8 @@ describe('Middleware development errors', () => { ...@@ -91,7 +92,8 @@ describe('Middleware development errors', () => {
it('logs the error correctly', async () => { it('logs the error correctly', async () => {
await fetchViaHTTP(context.appPort, '/') await fetchViaHTTP(context.appPort, '/')
const output = stripAnsi(context.logs.output) const output = stripAnsi(context.logs.output)
expect(output).toMatch( await check(
() => stripAnsi(context.logs.output),
new RegExp( new RegExp(
`error - middleware.js \\(\\d+:\\d+\\) @ throwError\nerror - unhandledRejection: async boom!`, `error - middleware.js \\(\\d+:\\d+\\) @ throwError\nerror - unhandledRejection: async boom!`,
'm' 'm'
...@@ -122,7 +124,8 @@ describe('Middleware development errors', () => { ...@@ -122,7 +124,8 @@ describe('Middleware development errors', () => {
it('logs the error correctly', async () => { it('logs the error correctly', async () => {
await fetchViaHTTP(context.appPort, '/') await fetchViaHTTP(context.appPort, '/')
const output = stripAnsi(context.logs.output) const output = stripAnsi(context.logs.output)
expect(output).toMatch( await check(
() => stripAnsi(context.logs.output),
new RegExp( new RegExp(
`error - middleware.js \\(\\d+:\\d+\\) @ eval\nerror - test is not defined`, `error - middleware.js \\(\\d+:\\d+\\) @ eval\nerror - test is not defined`,
'm' 'm'
...@@ -155,7 +158,8 @@ describe('Middleware development errors', () => { ...@@ -155,7 +158,8 @@ describe('Middleware development errors', () => {
it('logs the error correctly', async () => { it('logs the error correctly', async () => {
await fetchViaHTTP(context.appPort, '/') await fetchViaHTTP(context.appPort, '/')
const output = stripAnsi(context.logs.output) const output = stripAnsi(context.logs.output)
expect(output).toMatch( await check(
() => stripAnsi(context.logs.output),
new RegExp( new RegExp(
`error - middleware.js \\(\\d+:\\d+\\) @ <unknown>\nerror - booooom!`, `error - middleware.js \\(\\d+:\\d+\\) @ <unknown>\nerror - booooom!`,
'm' 'm'
...@@ -193,7 +197,8 @@ describe('Middleware development errors', () => { ...@@ -193,7 +197,8 @@ describe('Middleware development errors', () => {
it('logs the error correctly', async () => { it('logs the error correctly', async () => {
await fetchViaHTTP(context.appPort, '/') await fetchViaHTTP(context.appPort, '/')
const output = stripAnsi(context.logs.output) const output = stripAnsi(context.logs.output)
expect(output).toMatch( await check(
() => stripAnsi(context.logs.output),
new RegExp( new RegExp(
`error - middleware.js \\(\\d+:\\d+\\) @ eval\nerror - unhandledRejection: you shall see me`, `error - middleware.js \\(\\d+:\\d+\\) @ eval\nerror - unhandledRejection: you shall see me`,
'm' 'm'
...@@ -225,7 +230,8 @@ describe('Middleware development errors', () => { ...@@ -225,7 +230,8 @@ describe('Middleware development errors', () => {
it('logs the error correctly', async () => { it('logs the error correctly', async () => {
await fetchViaHTTP(context.appPort, '/') await fetchViaHTTP(context.appPort, '/')
const output = stripAnsi(context.logs.output) const output = stripAnsi(context.logs.output)
expect(output).toMatch( await check(
() => stripAnsi(context.logs.output),
new RegExp( new RegExp(
`error - lib/unhandled.js \\(\\d+:\\d+\\) @ Timeout.eval \\[as _onTimeout\\]\nerror - uncaughtException: This file asynchronously fails while loading`, `error - lib/unhandled.js \\(\\d+:\\d+\\) @ Timeout.eval \\[as _onTimeout\\]\nerror - uncaughtException: This file asynchronously fails while loading`,
'm' 'm'
......
export default (props) => <div id="from-middleware">{props.fromMiddleware}</div> export default (props) => <div id="from-middleware">{props.fromMiddleware}</div>
export async function getServerSideProps({ res }) { export async function getServerSideProps({ req, res }) {
return { return {
props: { props: {
fromMiddleware: res.getHeader('x-from-middleware') || '', fromMiddleware:
// TODO: this should only use request header once
// start is using the separate renders as well
req.headers['x-from-middleware'] ||
res.getHeader('x-from-middleware') ||
null,
}, },
} }
} }
import { import {
check,
fetchViaHTTP, fetchViaHTTP,
File, File,
findPort, findPort,
...@@ -39,11 +40,14 @@ describe('Middleware development errors', () => { ...@@ -39,11 +40,14 @@ describe('Middleware development errors', () => {
}) })
async function assertMiddlewareFetch(hasMiddleware, path = '/') { async function assertMiddlewareFetch(hasMiddleware, path = '/') {
const res = await fetchViaHTTP(context.appPort, path) await check(async () => {
expect(res.status).toBe(200) const res = await fetchViaHTTP(context.appPort, path)
expect(res.headers.get('x-from-middleware')).toBe( expect(res.status).toBe(200)
hasMiddleware ? 'true' : null expect(res.headers.get('x-from-middleware')).toBe(
) hasMiddleware ? 'true' : null
)
return 'success'
}, 'success')
} }
async function assertMiddlewareRender(hasMiddleware, path = '/') { async function assertMiddlewareRender(hasMiddleware, path = '/') {
......
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