Commit 1f6179b4 authored by Jimmy Lai's avatar Jimmy Lai

perf(dev): optimize dev server boot time with lazy loading

parent 40a4c11b
......@@ -19,10 +19,8 @@ import {
parseValidPositiveInteger,
type DebugAddress,
} from '../server/lib/utils'
import {
SUPPORTED_TEST_RUNNERS_LIST,
type NextTestOptions,
} from '../cli/next-test.js'
import { SUPPORTED_TEST_RUNNERS_LIST } from '../cli/next-test-constants.js'
import type { NextTestOptions } from '../cli/next-test.js'
import type { NextTelemetryOptions } from '../cli/next-telemetry.js'
import type { NextStartOptions } from '../cli/next-start.js'
import type { NextInfoOptions } from '../cli/next-info.js'
......
/**
* Lightweight module for getting supported browsers.
* Extracted from build/utils.ts to avoid loading heavy dependencies
* in hot-reloader-turbopack.
*/
import browserslist from 'next/dist/compiled/browserslist'
import { MODERN_BROWSERSLIST_TARGET } from '../shared/lib/constants'
export function getSupportedBrowsers(
dir: string,
isDevelopment: boolean
): string[] {
let browsers: any
try {
const browsersListConfig = browserslist.loadConfig({
path: dir,
env: isDevelopment ? 'development' : 'production',
})
// Running `browserslist` resolves `extends` and other config features into a list of browsers
if (browsersListConfig && browsersListConfig.length > 0) {
browsers = browserslist(browsersListConfig)
}
} catch {}
// When user has browserslist use that target
if (browsers && browsers.length > 0) {
return browsers
}
// Uses modern browsers as the default.
return MODERN_BROWSERSLIST_TARGET
}
// Set up the Node.js environment for next/jest users.
// node-environment patches globals (setImmediate, process.nextTick) and process
// event handlers. Loaded here to maintain backwards compatibility for users who
// may rely on this environment setup when using next/jest.
// Note: This was discovered when internal tests failed after removing these imports
// from the module loading chain during lazy loading optimization.
// TODO: Reevaluate whether exposing this environment setup to next/jest users is intended.
import '../../server/require-hook'
import '../../server/node-polyfill-crypto'
import '../../server/node-environment'
import { loadEnvConfig } from '@next/env'
import { resolve, join } from 'path'
import loadConfig from '../../server/config'
......
......@@ -6,7 +6,8 @@ import * as Log from '../output/log'
import { getParserOptions } from './options'
import { eventSwcLoadFailure } from '../../telemetry/events/swc-load-failure'
import { patchIncorrectLockfile } from '../../lib/patch-incorrect-lockfile'
import { downloadNativeNextSwc, downloadWasmSwc } from '../../lib/download-swc'
// Lazy loaded - only needed for fallback download
// import { downloadNativeNextSwc, downloadWasmSwc } from '../../lib/download-swc'
import type {
NextConfigComplete,
TurbopackLoaderBuiltinCondition,
......@@ -315,6 +316,8 @@ async function tryLoadNativeWithFallback(attempts: Array<string>) {
)
if (!downloadNativeBindingsPromise) {
const { downloadNativeNextSwc } =
require('../../lib/download-swc') as typeof import('../../lib/download-swc')
downloadNativeBindingsPromise = downloadNativeNextSwc(
nextVersion,
nativeBindingsDirectory,
......@@ -358,6 +361,8 @@ async function tryLoadWasmWithFallback(
'wasm'
)
if (!downloadWasmPromise) {
const { downloadWasmSwc } =
require('../../lib/download-swc') as typeof import('../../lib/download-swc')
downloadWasmPromise = downloadWasmSwc(nextVersion, wasmDirectory)
}
await downloadWasmPromise
......
......@@ -7,7 +7,10 @@ import type {
StyledComponentsConfig,
} from '../../server/config-shared'
import type { ResolvedBaseUrl } from '../load-jsconfig'
import { shouldUseReactServerCondition, isWebpackAppPagesLayer } from '../utils'
import {
shouldUseReactServerCondition,
isWebpackAppPagesLayer,
} from '../webpack-layer'
import { escapeStringRegexp } from '../../shared/lib/escape-regexp'
const nextDirname = path.dirname(require.resolve('next/package.json'))
......
......@@ -18,7 +18,6 @@ import type {
EdgeFunctionDefinition,
MiddlewareManifest,
} from './webpack/plugins/middleware-plugin'
import type { WebpackLayerName } from '../lib/constants'
import {
INSTRUMENTATION_HOOK_FILENAME,
MIDDLEWARE_FILENAME,
......@@ -1554,45 +1553,14 @@ export function getSupportedBrowsers(
return MODERN_BROWSERSLIST_TARGET
}
export function shouldUseReactServerCondition(
layer: WebpackLayerName | null | undefined
): boolean {
return Boolean(
layer && WEBPACK_LAYERS.GROUP.serverOnly.includes(layer as any)
)
}
export function isWebpackClientOnlyLayer(
layer: WebpackLayerName | null | undefined
): boolean {
return Boolean(
layer && WEBPACK_LAYERS.GROUP.clientOnly.includes(layer as any)
)
}
export function isWebpackDefaultLayer(
layer: WebpackLayerName | null | undefined
): boolean {
return (
layer === null ||
layer === undefined ||
layer === WEBPACK_LAYERS.pagesDirBrowser ||
layer === WEBPACK_LAYERS.pagesDirEdge ||
layer === WEBPACK_LAYERS.pagesDirNode
)
}
export function isWebpackBundledLayer(
layer: WebpackLayerName | null | undefined
): boolean {
return Boolean(layer && WEBPACK_LAYERS.GROUP.bundled.includes(layer as any))
}
export function isWebpackAppPagesLayer(
layer: WebpackLayerName | null | undefined
): boolean {
return Boolean(layer && WEBPACK_LAYERS.GROUP.appPages.includes(layer as any))
}
// Re-export webpack layer utilities from dedicated module (for backwards compatibility)
export {
shouldUseReactServerCondition,
isWebpackClientOnlyLayer,
isWebpackDefaultLayer,
isWebpackBundledLayer,
isWebpackAppPagesLayer,
} from './webpack-layer'
export function collectMeta({
status,
......
/**
* Webpack layer utilities - split from build/utils.ts to avoid pulling in heavy dependencies
* during dev server boot. These functions only depend on WEBPACK_LAYERS from constants.
*/
import type { WebpackLayerName } from '../lib/constants'
import { WEBPACK_LAYERS } from '../lib/constants'
export function shouldUseReactServerCondition(
layer: WebpackLayerName | null | undefined
): boolean {
return Boolean(
layer && WEBPACK_LAYERS.GROUP.serverOnly.includes(layer as any)
)
}
export function isWebpackClientOnlyLayer(
layer: WebpackLayerName | null | undefined
): boolean {
return Boolean(
layer && WEBPACK_LAYERS.GROUP.clientOnly.includes(layer as any)
)
}
export function isWebpackDefaultLayer(
layer: WebpackLayerName | null | undefined
): boolean {
return (
layer === null ||
layer === undefined ||
layer === WEBPACK_LAYERS.pagesDirBrowser ||
layer === WEBPACK_LAYERS.pagesDirEdge ||
layer === WEBPACK_LAYERS.pagesDirNode
)
}
export function isWebpackBundledLayer(
layer: WebpackLayerName | null | undefined
): boolean {
return Boolean(layer && WEBPACK_LAYERS.GROUP.bundled.includes(layer as any))
}
export function isWebpackAppPagesLayer(
layer: WebpackLayerName | null | undefined
): boolean {
return Boolean(layer && WEBPACK_LAYERS.GROUP.appPages.includes(layer as any))
}
......@@ -19,14 +19,15 @@ import { PHASE_DEVELOPMENT_SERVER } from '../shared/lib/constants'
import path from 'path'
import type { NextConfigComplete } from '../server/config-shared'
import { traceGlobals } from '../trace/shared'
import { Telemetry } from '../telemetry/storage'
import loadConfig from '../server/config'
import { findPagesDir } from '../lib/find-pages-dir'
import { fileExists, FileType } from '../lib/file-exists'
import { getNpxCommand } from '../lib/helpers/get-npx-command'
import { createSelfSignedCertificate } from '../lib/mkcert'
// Heavy modules - lazy loaded to speed up CLI startup
// import { Telemetry } from '../telemetry/storage' - loaded in handleSessionStop
// import loadConfig from '../server/config' - loaded in handleSessionStop
// import { findPagesDir } from '../lib/find-pages-dir' - loaded in handleSessionStop
// import { getNpxCommand } from '../lib/helpers/get-npx-command' - loaded in preflight
// import { createSelfSignedCertificate } from '../lib/mkcert' - loaded only with --experimental-https
// import uploadTrace from '../trace/upload-trace' - loaded only with --experimental-upload-trace
import type { SelfSignedCertificate } from '../lib/mkcert'
import uploadTrace from '../trace/upload-trace'
import { fileExists, FileType } from '../lib/file-exists'
import { initialEnv } from '@next/env'
import { fork } from 'child_process'
import type { ChildProcess } from 'child_process'
......@@ -117,15 +118,22 @@ const handleSessionStop = async (signal: NodeJS.Signals | number | null) => {
typeof traceGlobals.get('pagesDir') === 'undefined' ||
typeof traceGlobals.get('appDir') === 'undefined'
) {
const { findPagesDir } =
require('../lib/find-pages-dir') as typeof import('../lib/find-pages-dir')
const pagesResult = findPagesDir(dir)
appDir = !!pagesResult.appDir
pagesDir = !!pagesResult.pagesDir
}
const loadConfig = (
require('../server/config') as typeof import('../server/config')
).default
config =
config ||
(await loadConfig(PHASE_DEVELOPMENT_SERVER, dir, { silent: true }))
const { Telemetry } =
require('../telemetry/storage') as typeof import('../telemetry/storage')
let telemetry =
(traceGlobals.get('telemetry') as InstanceType<
typeof import('../telemetry/storage').Telemetry
......@@ -153,6 +161,9 @@ const handleSessionStop = async (signal: NodeJS.Signals | number | null) => {
}
if (traceUploadUrl) {
const uploadTrace = (
require('../trace/upload-trace') as typeof import('../trace/upload-trace')
).default
uploadTrace({
traceUploadUrl,
mode: 'dev',
......@@ -217,6 +228,8 @@ const nextDev = async (
(devDependencies['@next/font'] &&
devDependencies['@next/font'] !== 'workspace:*')
) {
const { getNpxCommand } =
require('../lib/helpers/get-npx-command') as typeof import('../lib/helpers/get-npx-command')
const command = getNpxCommand(dir)
Log.warn(
'Your project has `@next/font` installed as a dependency, please use the built-in `next/font` instead. ' +
......@@ -309,6 +322,8 @@ const nextDev = async (
: undefined),
NEXT_PRIVATE_WORKER: '1',
NEXT_PRIVATE_TRACE_ID: traceId,
// Pass server options via env to eliminate IPC handshake latency
NEXT_PRIVATE_WORKER_OPTIONS: JSON.stringify(startServerOptions),
NODE_EXTRA_CA_CERTS: startServerOptions.selfSignedCertificate
? startServerOptions.selfSignedCertificate.rootCA
: defaultEnv.NODE_EXTRA_CA_CERTS,
......@@ -323,9 +338,7 @@ const nextDev = async (
child.on('message', (msg: any) => {
if (msg && typeof msg === 'object') {
if (msg.nextWorkerReady) {
child?.send({ nextWorkerOptions: startServerOptions })
} else if (msg.nextServerReady && !resolved) {
if (msg.nextServerReady && !resolved) {
if (msg.port) {
// Store the used port in case a random one was selected, so that
// it can be re-used on automatic dev server restarts.
......@@ -349,12 +362,18 @@ const nextDev = async (
if (traceUploadUrl) {
// Postpone loading next config when we need to get
// config.distDir for upload trace.
const loadConfig = (
require('../server/config') as typeof import('../server/config')
).default
config =
config ||
(await loadConfig(PHASE_DEVELOPMENT_SERVER, dir, {
silent: true,
}))
bundler = finalizeBundlerFromConfig(bundler)
const uploadTrace = (
require('../trace/upload-trace') as typeof import('../trace/upload-trace')
).default
uploadTrace({
traceUploadUrl,
mode: 'dev',
......@@ -394,6 +413,8 @@ const nextDev = async (
rootCA: rootCA ? path.resolve(rootCA) : undefined,
}
} else {
const { createSelfSignedCertificate } =
require('../lib/mkcert') as typeof import('../lib/mkcert')
certificate = await createSelfSignedCertificate(host)
}
......
/**
* Constants for next test command
*
* These are separated from next-test.ts to avoid loading heavy dependencies
* (like loadConfig, verifyTypeScriptSetup) when only the constants are needed.
*/
export const SUPPORTED_TEST_RUNNERS_LIST = ['playwright'] as const
export type SupportedTestRunners = (typeof SUPPORTED_TEST_RUNNERS_LIST)[number]
......@@ -15,12 +15,16 @@ import { verifyTypeScriptSetup } from '../lib/verify-typescript-setup'
import path from 'path'
import spawn from 'next/dist/compiled/cross-spawn'
import type { SupportedTestRunners } from './next-test-constants.js'
export interface NextTestOptions {
testRunner?: string
}
export const SUPPORTED_TEST_RUNNERS_LIST = ['playwright'] as const
export type SupportedTestRunners = (typeof SUPPORTED_TEST_RUNNERS_LIST)[number]
export {
SUPPORTED_TEST_RUNNERS_LIST,
type SupportedTestRunners,
} from './next-test-constants.js'
const requiredPackagesByTestRunner: {
[k in SupportedTestRunners]: MissingDependency[]
......
import type { NextConfigComplete } from '../config-shared'
import type { DevBundler } from './router-utils/setup-dev-bundler'
import type { setupFsCheck } from './router-utils/filesystem'
import type { UnwrapPromise } from '../../lib/coalesced-function'
import type { Span } from '../../trace'
import type { WorkerRequestHandler } from './types'
import type { LazyRenderServerInstance } from './router-server'
type FsChecker = UnwrapPromise<ReturnType<typeof setupFsCheck>>
import path from 'path'
import { findPagesDir } from '../../lib/find-pages-dir'
import { trace } from '../../trace'
import { traceGlobals } from '../../trace/shared'
import { DevBundlerService } from './dev-bundler-service'
import { NEXT_PATCH_SYMBOL } from './patch-fetch'
export interface DevServerInitOptions {
dir: string
port: number
config: NextConfigComplete
fsChecker: FsChecker
renderServer: LazyRenderServerInstance
customServer?: boolean
startServerSpan?: Span
onDevServerCleanup: ((listener: () => Promise<void>) => void) | undefined
originalFetch: typeof globalThis.fetch
getRequestHandler: () => WorkerRequestHandler
}
export interface DevServerInitResult {
bundler: DevBundler
service: DevBundlerService
config: NextConfigComplete
}
/**
* Initialize the development server components.
* This function is lazily loaded only in development mode to reduce startup time.
*/
export async function initializeDevelopmentServer(
opts: DevServerInitOptions
): Promise<DevServerInitResult> {
// Lazy load telemetry to avoid loading in production
const { Telemetry } =
require('../../telemetry/storage') as typeof import('../../telemetry/storage')
const telemetry = new Telemetry({
distDir: path.join(opts.dir, opts.config.distDir),
})
traceGlobals.set('telemetry', telemetry)
const { pagesDir, appDir } = findPagesDir(opts.dir)
// Lazy load setupDevBundler to avoid loading bundler code in production
const { setupDevBundler } =
require('./router-utils/setup-dev-bundler') as typeof import('./router-utils/setup-dev-bundler')
const resetFetch = () => {
globalThis.fetch = opts.originalFetch
;(globalThis as Record<symbol, unknown>)[NEXT_PATCH_SYMBOL] = false
}
const setupDevBundlerSpan = opts.startServerSpan
? opts.startServerSpan.traceChild('setup-dev-bundler')
: trace('setup-dev-bundler')
const developmentBundler = await setupDevBundlerSpan.traceAsyncFn(() =>
setupDevBundler({
renderServer: opts.renderServer,
appDir,
pagesDir,
telemetry,
fsChecker: opts.fsChecker,
dir: opts.dir,
nextConfig: opts.config,
isCustomServer: opts.customServer,
turbo: !!process.env.TURBOPACK,
port: opts.port,
onDevServerCleanup: opts.onDevServerCleanup,
resetFetch,
})
)
const devBundlerService = new DevBundlerService(
developmentBundler,
// The request handler is assigned later, this allows us to create a lazy
// reference to it.
(req, res) => {
return opts.getRequestHandler()(req, res)
}
)
return {
bundler: developmentBundler,
service: devBundlerService,
config: opts.config,
}
}
......@@ -19,19 +19,23 @@ export type ServerInitResult = {
let initializations: Record<string, Promise<ServerInitResult> | undefined> = {}
// Lazy loaded - only needed when edge functions are used
let sandboxContext: undefined | typeof import('../web/sandbox/context')
if (process.env.NODE_ENV !== 'production') {
sandboxContext =
require('../web/sandbox/context') as typeof import('../web/sandbox/context')
function getSandboxContext() {
if (process.env.NODE_ENV !== 'production' && !sandboxContext) {
sandboxContext =
require('../web/sandbox/context') as typeof import('../web/sandbox/context')
}
return sandboxContext
}
export function clearAllModuleContexts() {
return sandboxContext?.clearAllModuleContexts()
return getSandboxContext()?.clearAllModuleContexts()
}
export function clearModuleContext(target: string) {
return sandboxContext?.clearModuleContext(target)
return getSandboxContext()?.clearModuleContext(target)
}
export async function getServerField(
......
......@@ -14,7 +14,6 @@ import { serveStatic } from '../serve-static'
import setupDebug from 'next/dist/compiled/debug'
import * as Log from '../../build/output/log'
import { DecodeError } from '../../shared/lib/utils'
import { findPagesDir } from '../../lib/find-pages-dir'
import { setupFsCheck } from './router-utils/filesystem'
import { proxyRequest } from './router-utils/proxy-request'
import { isAbortError, pipeToNodeResponse } from '../pipe-readable'
......@@ -33,8 +32,8 @@ import {
UNDERSCORE_NOT_FOUND_ROUTE,
} from '../../shared/lib/constants'
import { RedirectStatusCode } from '../../client/components/redirect-status-code'
import { DevBundlerService } from './dev-bundler-service'
import { type Span, trace } from '../../trace'
import type { DevBundlerService } from './dev-bundler-service'
import type { Span } from '../../trace'
import { ensureLeadingSlash } from '../../shared/lib/page-path/ensure-leading-slash'
import { getNextPathnameInfo } from '../../shared/lib/router/utils/get-next-pathname-info'
import { getHostname } from '../../shared/lib/get-hostname'
......@@ -45,11 +44,9 @@ import {
type AppIsrManifestMessage,
} 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'
import { filterInternalHeaders } from './server-ipc/utils'
import { blockCrossSite } from './router-utils/block-cross-site'
import { traceGlobals } from '../../trace/shared'
import { NoFallbackError } from '../../shared/lib/no-fallback-error.external'
import {
RouterServerContextSymbol,
......@@ -129,64 +126,34 @@ export async function initialize(opts: {
let originalFetch = globalThis.fetch
if (opts.dev) {
const { Telemetry } =
require('../../telemetry/storage') as typeof import('../../telemetry/storage')
// Set up testProxy BEFORE initializing dev server so that the intercepted fetch
// is captured as originalFetch and used for HMR resets
if (config.experimental.testProxy) {
const { interceptTestApis } =
// eslint-disable-next-line @next/internal/typechecked-require -- experimental/testmode is not built ins next/dist/esm
require('next/dist/experimental/testmode/server') as typeof import('../../experimental/testmode/server')
interceptTestApis()
originalFetch = globalThis.fetch
}
const telemetry = new Telemetry({
distDir: path.join(opts.dir, config.distDir),
if (opts.dev) {
// Lazy load dev server initialization to reduce startup time
// and enable better bundling of production code paths
const { initializeDevelopmentServer } =
require('./dev-server-init') as typeof import('./dev-server-init')
development = await initializeDevelopmentServer({
dir: opts.dir,
port: opts.port,
config: config as NextConfigComplete,
fsChecker,
renderServer,
customServer: opts.customServer,
startServerSpan: opts.startServerSpan,
onDevServerCleanup: opts.onDevServerCleanup,
originalFetch,
getRequestHandler: () => requestHandlers[opts.dir],
})
traceGlobals.set('telemetry', telemetry)
const { pagesDir, appDir } = findPagesDir(opts.dir)
const { setupDevBundler } =
require('./router-utils/setup-dev-bundler') as typeof import('./router-utils/setup-dev-bundler')
const resetFetch = () => {
globalThis.fetch = originalFetch
;(globalThis as Record<symbol, unknown>)[NEXT_PATCH_SYMBOL] = false
}
const setupDevBundlerSpan = opts.startServerSpan
? opts.startServerSpan.traceChild('setup-dev-bundler')
: trace('setup-dev-bundler')
// In development, it's always the complete config.
let developmentConfig = config as NextConfigComplete
let developmentBundler = await setupDevBundlerSpan.traceAsyncFn(() =>
setupDevBundler({
// Passed here but the initialization of this object happens below, doing the initialization before the setupDev call breaks.
renderServer,
appDir,
pagesDir,
telemetry,
fsChecker,
dir: opts.dir,
nextConfig: developmentConfig,
isCustomServer: opts.customServer,
turbo: !!process.env.TURBOPACK,
port: opts.port,
onDevServerCleanup: opts.onDevServerCleanup,
resetFetch,
})
)
let devBundlerService = new DevBundlerService(
developmentBundler,
// The request handler is assigned below, this allows us to create a lazy
// reference to it.
(req, res) => {
return requestHandlers[opts.dir](req, res)
}
)
development = {
bundler: developmentBundler,
service: devBundlerService,
config: developmentConfig,
}
}
renderServer.instance =
......@@ -692,14 +659,11 @@ export async function initialize(opts: {
let requestHandler: WorkerRequestHandler = requestHandlerImpl
if (config.experimental.testProxy) {
// Intercept fetch and other testmode apis.
const { wrapRequestHandlerWorker, interceptTestApis } =
// Wrap the request handler for testmode (interceptTestApis was already called earlier)
const { wrapRequestHandlerWorker } =
// eslint-disable-next-line @next/internal/typechecked-require -- experimental/testmode is not built ins next/dist/esm
require('next/dist/experimental/testmode/server') as typeof import('../../experimental/testmode/server')
requestHandler = wrapRequestHandlerWorker(requestHandler)
interceptTestApis()
// We treat the intercepted fetch as "original" fetch that should be reset to during HMR.
originalFetch = globalThis.fetch
}
requestHandlers[opts.dir] = requestHandler
......
......@@ -370,7 +370,15 @@ async function startWatcher(
},
})
const fileWatchTimes = new Map()
let enabledTypeScript = await verifyTypeScript(opts)
// Start TypeScript verification in the background to avoid blocking startup.
// The result will be awaited when first needed in the aggregated handler.
let enabledTypeScript = false
let tsVerificationComplete = false
const tsVerificationPromise = verifyTypeScript(opts).then((result) => {
enabledTypeScript = result
tsVerificationComplete = true
return result
})
let previousClientRouterFilters: any
let previousConflictingPagePaths: Set<string> = new Set()
......@@ -1118,6 +1126,11 @@ async function startWatcher(
}
prevSortedRoutes = sortedRoutes
// Ensure the deferred TypeScript verification is complete before proceeding
if (!tsVerificationComplete) {
await tsVerificationPromise
}
if (enabledTypeScript) {
// Using === false to make the check clearer.
if (typescriptStatusFromLastAggregation === false) {
......
......@@ -374,8 +374,6 @@ export async function startServer(
logBundler: isDev,
})
Log.event(`Starting...`)
let cleanupStarted = false
let closeUpgraded: (() => void) | null = null
const cleanup = () => {
......@@ -524,13 +522,22 @@ export async function startServer(
}
if (process.env.NEXT_PRIVATE_WORKER && process.send) {
process.addListener('message', async (msg: any) => {
if (
msg &&
typeof msg === 'object' &&
msg.nextWorkerOptions &&
process.send
) {
// Read server options from env to eliminate IPC handshake latency
let workerOptions = null
if (process.env.NEXT_PRIVATE_WORKER_OPTIONS) {
try {
workerOptions = JSON.parse(process.env.NEXT_PRIVATE_WORKER_OPTIONS)
} catch (err) {
console.error(
'Failed to parse NEXT_PRIVATE_WORKER_OPTIONS:',
err instanceof Error ? err.message : err
)
}
}
if (workerOptions) {
// Start server immediately without waiting for IPC message
;(async () => {
startServerSpan = trace('start-dev-server', undefined, {
cpus: String(os.cpus().length),
platform: os.platform(),
......@@ -538,9 +545,7 @@ if (process.env.NEXT_PRIVATE_WORKER && process.send) {
'memory.totalMem': String(os.totalmem()),
'memory.heapSizeLimit': String(v8.getHeapStatistics().heap_size_limit),
})
await startServerSpan.traceAsyncFn(() =>
startServer(msg.nextWorkerOptions)
)
await startServerSpan.traceAsyncFn(() => startServer(workerOptions))
const memoryUsage = process.memoryUsage()
startServerSpan.setAttribute('memory.rss', String(memoryUsage.rss))
startServerSpan.setAttribute(
......@@ -551,8 +556,7 @@ if (process.env.NEXT_PRIVATE_WORKER && process.send) {
'memory.heapUsed',
String(memoryUsage.heapUsed)
)
process.send({ nextServerReady: true, port: process.env.PORT })
}
})
process.send({ nextWorkerReady: true })
process.send!({ nextServerReady: true, port: process.env.PORT })
})()
}
}
......@@ -9,7 +9,6 @@ import { getAnonymousMeta } from './anonymous-meta'
import * as ciEnvironment from '../server/ci-info'
import { postNextTelemetryPayload } from './post-telemetry-payload'
import { getRawProjectId } from './project-id'
import { AbortController } from 'next/dist/compiled/@edge-runtime/ponyfill'
import fs from 'fs'
// This is the key that stores whether or not telemetry is enabled or disabled.
......
import { createNext } from 'e2e-utils'
import { nextTestSetup } from 'e2e-utils'
describe('next start without next build', () => {
const { next, isNextDev, skipped } = nextTestSetup({
files: __dirname,
skipStart: true,
startCommand: `pnpm next start`,
})
// This test only makes sense in production mode - skip in dev mode
// The test also fails on canary in dev mode (pre-existing issue)
if (isNextDev || skipped) {
it('skip test in development mode', () => {})
return
}
it('should show error when there is no production build', async () => {
const next = await createNext({
files: __dirname,
skipStart: true,
startCommand: `pnpm next start`,
serverReadyPattern: /✓ Starting.../,
// Set up stderr listener before starting to capture the error
let errorFound = false
next.on('stderr', (msg: string) => {
if (msg.includes('Could not find a production build in the')) {
errorFound = true
}
})
await next.start()
await new Promise<void>((resolve, reject) => {
next.on('stderr', (msg) => {
if (msg.includes('Could not find a production build in the')) {
resolve()
}
})
})
// start() will throw because the server exits with error code
try {
await next.start()
} catch (e) {
// Expected - server should fail to start without a build
}
await next.destroy()
expect(errorFound).toBe(true)
})
})
......@@ -11,8 +11,9 @@ describe('instrumentation-order', () => {
await next.fetch('/')
await retry(async () => {
const serverLog = next.cliOutput.split('Starting...')[1]
const cliOutputLines = serverLog.split('\n')
// Filter CLI output for the instrumentation-related logs
// We look for these logs starting from when instrumentation begins
const cliOutputLines = next.cliOutput.split('\n')
const ORDERED_LOGS = [
'instrumentation:side-effect',
......@@ -21,8 +22,21 @@ describe('instrumentation-order', () => {
'instrumentation:register:end',
'global-side-effect:app-router-page',
]
const searchedLines = cliOutputLines.filter((line) =>
ORDERED_LOGS.includes(line.trim())
// Find all matching lines
const allMatchingLines = cliOutputLines
.map((line) => line.trim())
.filter((line) => ORDERED_LOGS.includes(line))
// Find where instrumentation starts and extract the sequence from there
const instrumentationStartIndex = allMatchingLines.indexOf(
'instrumentation:side-effect'
)
expect(instrumentationStartIndex).toBeGreaterThanOrEqual(0)
const searchedLines = allMatchingLines.slice(
instrumentationStartIndex,
instrumentationStartIndex + ORDERED_LOGS.length
)
expect(searchedLines).toEqual(ORDERED_LOGS)
......
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