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 { ...@@ -19,10 +19,8 @@ import {
parseValidPositiveInteger, parseValidPositiveInteger,
type DebugAddress, type DebugAddress,
} from '../server/lib/utils' } from '../server/lib/utils'
import { import { SUPPORTED_TEST_RUNNERS_LIST } from '../cli/next-test-constants.js'
SUPPORTED_TEST_RUNNERS_LIST, import type { NextTestOptions } from '../cli/next-test.js'
type NextTestOptions,
} from '../cli/next-test.js'
import type { NextTelemetryOptions } from '../cli/next-telemetry.js' import type { NextTelemetryOptions } from '../cli/next-telemetry.js'
import type { NextStartOptions } from '../cli/next-start.js' import type { NextStartOptions } from '../cli/next-start.js'
import type { NextInfoOptions } from '../cli/next-info.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 { loadEnvConfig } from '@next/env'
import { resolve, join } from 'path' import { resolve, join } from 'path'
import loadConfig from '../../server/config' import loadConfig from '../../server/config'
......
...@@ -6,7 +6,8 @@ import * as Log from '../output/log' ...@@ -6,7 +6,8 @@ import * as Log from '../output/log'
import { getParserOptions } from './options' import { getParserOptions } from './options'
import { eventSwcLoadFailure } from '../../telemetry/events/swc-load-failure' import { eventSwcLoadFailure } from '../../telemetry/events/swc-load-failure'
import { patchIncorrectLockfile } from '../../lib/patch-incorrect-lockfile' 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 { import type {
NextConfigComplete, NextConfigComplete,
TurbopackLoaderBuiltinCondition, TurbopackLoaderBuiltinCondition,
...@@ -315,6 +316,8 @@ async function tryLoadNativeWithFallback(attempts: Array<string>) { ...@@ -315,6 +316,8 @@ async function tryLoadNativeWithFallback(attempts: Array<string>) {
) )
if (!downloadNativeBindingsPromise) { if (!downloadNativeBindingsPromise) {
const { downloadNativeNextSwc } =
require('../../lib/download-swc') as typeof import('../../lib/download-swc')
downloadNativeBindingsPromise = downloadNativeNextSwc( downloadNativeBindingsPromise = downloadNativeNextSwc(
nextVersion, nextVersion,
nativeBindingsDirectory, nativeBindingsDirectory,
...@@ -358,6 +361,8 @@ async function tryLoadWasmWithFallback( ...@@ -358,6 +361,8 @@ async function tryLoadWasmWithFallback(
'wasm' 'wasm'
) )
if (!downloadWasmPromise) { if (!downloadWasmPromise) {
const { downloadWasmSwc } =
require('../../lib/download-swc') as typeof import('../../lib/download-swc')
downloadWasmPromise = downloadWasmSwc(nextVersion, wasmDirectory) downloadWasmPromise = downloadWasmSwc(nextVersion, wasmDirectory)
} }
await downloadWasmPromise await downloadWasmPromise
......
...@@ -7,7 +7,10 @@ import type { ...@@ -7,7 +7,10 @@ import type {
StyledComponentsConfig, StyledComponentsConfig,
} from '../../server/config-shared' } from '../../server/config-shared'
import type { ResolvedBaseUrl } from '../load-jsconfig' import type { ResolvedBaseUrl } from '../load-jsconfig'
import { shouldUseReactServerCondition, isWebpackAppPagesLayer } from '../utils' import {
shouldUseReactServerCondition,
isWebpackAppPagesLayer,
} from '../webpack-layer'
import { escapeStringRegexp } from '../../shared/lib/escape-regexp' import { escapeStringRegexp } from '../../shared/lib/escape-regexp'
const nextDirname = path.dirname(require.resolve('next/package.json')) const nextDirname = path.dirname(require.resolve('next/package.json'))
......
...@@ -18,7 +18,6 @@ import type { ...@@ -18,7 +18,6 @@ import type {
EdgeFunctionDefinition, EdgeFunctionDefinition,
MiddlewareManifest, MiddlewareManifest,
} from './webpack/plugins/middleware-plugin' } from './webpack/plugins/middleware-plugin'
import type { WebpackLayerName } from '../lib/constants'
import { import {
INSTRUMENTATION_HOOK_FILENAME, INSTRUMENTATION_HOOK_FILENAME,
MIDDLEWARE_FILENAME, MIDDLEWARE_FILENAME,
...@@ -1554,45 +1553,14 @@ export function getSupportedBrowsers( ...@@ -1554,45 +1553,14 @@ export function getSupportedBrowsers(
return MODERN_BROWSERSLIST_TARGET return MODERN_BROWSERSLIST_TARGET
} }
export function shouldUseReactServerCondition( // Re-export webpack layer utilities from dedicated module (for backwards compatibility)
layer: WebpackLayerName | null | undefined export {
): boolean { shouldUseReactServerCondition,
return Boolean( isWebpackClientOnlyLayer,
layer && WEBPACK_LAYERS.GROUP.serverOnly.includes(layer as any) isWebpackDefaultLayer,
) isWebpackBundledLayer,
} isWebpackAppPagesLayer,
} from './webpack-layer'
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))
}
export function collectMeta({ export function collectMeta({
status, 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' ...@@ -19,14 +19,15 @@ import { PHASE_DEVELOPMENT_SERVER } from '../shared/lib/constants'
import path from 'path' import path from 'path'
import type { NextConfigComplete } from '../server/config-shared' import type { NextConfigComplete } from '../server/config-shared'
import { traceGlobals } from '../trace/shared' import { traceGlobals } from '../trace/shared'
import { Telemetry } from '../telemetry/storage' // Heavy modules - lazy loaded to speed up CLI startup
import loadConfig from '../server/config' // import { Telemetry } from '../telemetry/storage' - loaded in handleSessionStop
import { findPagesDir } from '../lib/find-pages-dir' // import loadConfig from '../server/config' - loaded in handleSessionStop
import { fileExists, FileType } from '../lib/file-exists' // import { findPagesDir } from '../lib/find-pages-dir' - loaded in handleSessionStop
import { getNpxCommand } from '../lib/helpers/get-npx-command' // import { getNpxCommand } from '../lib/helpers/get-npx-command' - loaded in preflight
import { createSelfSignedCertificate } from '../lib/mkcert' // 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 type { SelfSignedCertificate } from '../lib/mkcert'
import uploadTrace from '../trace/upload-trace' import { fileExists, FileType } from '../lib/file-exists'
import { initialEnv } from '@next/env' import { initialEnv } from '@next/env'
import { fork } from 'child_process' import { fork } from 'child_process'
import type { ChildProcess } from 'child_process' import type { ChildProcess } from 'child_process'
...@@ -117,15 +118,22 @@ const handleSessionStop = async (signal: NodeJS.Signals | number | null) => { ...@@ -117,15 +118,22 @@ const handleSessionStop = async (signal: NodeJS.Signals | number | null) => {
typeof traceGlobals.get('pagesDir') === 'undefined' || typeof traceGlobals.get('pagesDir') === 'undefined' ||
typeof traceGlobals.get('appDir') === 'undefined' typeof traceGlobals.get('appDir') === 'undefined'
) { ) {
const { findPagesDir } =
require('../lib/find-pages-dir') as typeof import('../lib/find-pages-dir')
const pagesResult = findPagesDir(dir) const pagesResult = findPagesDir(dir)
appDir = !!pagesResult.appDir appDir = !!pagesResult.appDir
pagesDir = !!pagesResult.pagesDir pagesDir = !!pagesResult.pagesDir
} }
const loadConfig = (
require('../server/config') as typeof import('../server/config')
).default
config = config =
config || config ||
(await loadConfig(PHASE_DEVELOPMENT_SERVER, dir, { silent: true })) (await loadConfig(PHASE_DEVELOPMENT_SERVER, dir, { silent: true }))
const { Telemetry } =
require('../telemetry/storage') as typeof import('../telemetry/storage')
let telemetry = let telemetry =
(traceGlobals.get('telemetry') as InstanceType< (traceGlobals.get('telemetry') as InstanceType<
typeof import('../telemetry/storage').Telemetry typeof import('../telemetry/storage').Telemetry
...@@ -153,6 +161,9 @@ const handleSessionStop = async (signal: NodeJS.Signals | number | null) => { ...@@ -153,6 +161,9 @@ const handleSessionStop = async (signal: NodeJS.Signals | number | null) => {
} }
if (traceUploadUrl) { if (traceUploadUrl) {
const uploadTrace = (
require('../trace/upload-trace') as typeof import('../trace/upload-trace')
).default
uploadTrace({ uploadTrace({
traceUploadUrl, traceUploadUrl,
mode: 'dev', mode: 'dev',
...@@ -217,6 +228,8 @@ const nextDev = async ( ...@@ -217,6 +228,8 @@ const nextDev = async (
(devDependencies['@next/font'] && (devDependencies['@next/font'] &&
devDependencies['@next/font'] !== 'workspace:*') devDependencies['@next/font'] !== 'workspace:*')
) { ) {
const { getNpxCommand } =
require('../lib/helpers/get-npx-command') as typeof import('../lib/helpers/get-npx-command')
const command = getNpxCommand(dir) const command = getNpxCommand(dir)
Log.warn( Log.warn(
'Your project has `@next/font` installed as a dependency, please use the built-in `next/font` instead. ' + 'Your project has `@next/font` installed as a dependency, please use the built-in `next/font` instead. ' +
...@@ -309,6 +322,8 @@ const nextDev = async ( ...@@ -309,6 +322,8 @@ const nextDev = async (
: undefined), : undefined),
NEXT_PRIVATE_WORKER: '1', NEXT_PRIVATE_WORKER: '1',
NEXT_PRIVATE_TRACE_ID: traceId, 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 NODE_EXTRA_CA_CERTS: startServerOptions.selfSignedCertificate
? startServerOptions.selfSignedCertificate.rootCA ? startServerOptions.selfSignedCertificate.rootCA
: defaultEnv.NODE_EXTRA_CA_CERTS, : defaultEnv.NODE_EXTRA_CA_CERTS,
...@@ -323,9 +338,7 @@ const nextDev = async ( ...@@ -323,9 +338,7 @@ const nextDev = async (
child.on('message', (msg: any) => { child.on('message', (msg: any) => {
if (msg && typeof msg === 'object') { if (msg && typeof msg === 'object') {
if (msg.nextWorkerReady) { if (msg.nextServerReady && !resolved) {
child?.send({ nextWorkerOptions: startServerOptions })
} else if (msg.nextServerReady && !resolved) {
if (msg.port) { if (msg.port) {
// Store the used port in case a random one was selected, so that // Store the used port in case a random one was selected, so that
// it can be re-used on automatic dev server restarts. // it can be re-used on automatic dev server restarts.
...@@ -349,12 +362,18 @@ const nextDev = async ( ...@@ -349,12 +362,18 @@ const nextDev = async (
if (traceUploadUrl) { if (traceUploadUrl) {
// Postpone loading next config when we need to get // Postpone loading next config when we need to get
// config.distDir for upload trace. // config.distDir for upload trace.
const loadConfig = (
require('../server/config') as typeof import('../server/config')
).default
config = config =
config || config ||
(await loadConfig(PHASE_DEVELOPMENT_SERVER, dir, { (await loadConfig(PHASE_DEVELOPMENT_SERVER, dir, {
silent: true, silent: true,
})) }))
bundler = finalizeBundlerFromConfig(bundler) bundler = finalizeBundlerFromConfig(bundler)
const uploadTrace = (
require('../trace/upload-trace') as typeof import('../trace/upload-trace')
).default
uploadTrace({ uploadTrace({
traceUploadUrl, traceUploadUrl,
mode: 'dev', mode: 'dev',
...@@ -394,6 +413,8 @@ const nextDev = async ( ...@@ -394,6 +413,8 @@ const nextDev = async (
rootCA: rootCA ? path.resolve(rootCA) : undefined, rootCA: rootCA ? path.resolve(rootCA) : undefined,
} }
} else { } else {
const { createSelfSignedCertificate } =
require('../lib/mkcert') as typeof import('../lib/mkcert')
certificate = await createSelfSignedCertificate(host) 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' ...@@ -15,12 +15,16 @@ import { verifyTypeScriptSetup } from '../lib/verify-typescript-setup'
import path from 'path' import path from 'path'
import spawn from 'next/dist/compiled/cross-spawn' import spawn from 'next/dist/compiled/cross-spawn'
import type { SupportedTestRunners } from './next-test-constants.js'
export interface NextTestOptions { export interface NextTestOptions {
testRunner?: string testRunner?: string
} }
export const SUPPORTED_TEST_RUNNERS_LIST = ['playwright'] as const export {
export type SupportedTestRunners = (typeof SUPPORTED_TEST_RUNNERS_LIST)[number] SUPPORTED_TEST_RUNNERS_LIST,
type SupportedTestRunners,
} from './next-test-constants.js'
const requiredPackagesByTestRunner: { const requiredPackagesByTestRunner: {
[k in SupportedTestRunners]: MissingDependency[] [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 = { ...@@ -19,19 +19,23 @@ export type ServerInitResult = {
let initializations: Record<string, Promise<ServerInitResult> | undefined> = {} let initializations: Record<string, Promise<ServerInitResult> | undefined> = {}
// Lazy loaded - only needed when edge functions are used
let sandboxContext: undefined | typeof import('../web/sandbox/context') let sandboxContext: undefined | typeof import('../web/sandbox/context')
if (process.env.NODE_ENV !== 'production') { function getSandboxContext() {
sandboxContext = if (process.env.NODE_ENV !== 'production' && !sandboxContext) {
require('../web/sandbox/context') as typeof import('../web/sandbox/context') sandboxContext =
require('../web/sandbox/context') as typeof import('../web/sandbox/context')
}
return sandboxContext
} }
export function clearAllModuleContexts() { export function clearAllModuleContexts() {
return sandboxContext?.clearAllModuleContexts() return getSandboxContext()?.clearAllModuleContexts()
} }
export function clearModuleContext(target: string) { export function clearModuleContext(target: string) {
return sandboxContext?.clearModuleContext(target) return getSandboxContext()?.clearModuleContext(target)
} }
export async function getServerField( export async function getServerField(
......
...@@ -14,7 +14,6 @@ import { serveStatic } from '../serve-static' ...@@ -14,7 +14,6 @@ import { serveStatic } from '../serve-static'
import setupDebug from 'next/dist/compiled/debug' import setupDebug from 'next/dist/compiled/debug'
import * as Log from '../../build/output/log' import * as Log from '../../build/output/log'
import { DecodeError } from '../../shared/lib/utils' import { DecodeError } from '../../shared/lib/utils'
import { findPagesDir } from '../../lib/find-pages-dir'
import { setupFsCheck } from './router-utils/filesystem' import { setupFsCheck } from './router-utils/filesystem'
import { proxyRequest } from './router-utils/proxy-request' import { proxyRequest } from './router-utils/proxy-request'
import { isAbortError, pipeToNodeResponse } from '../pipe-readable' import { isAbortError, pipeToNodeResponse } from '../pipe-readable'
...@@ -33,8 +32,8 @@ import { ...@@ -33,8 +32,8 @@ import {
UNDERSCORE_NOT_FOUND_ROUTE, UNDERSCORE_NOT_FOUND_ROUTE,
} from '../../shared/lib/constants' } from '../../shared/lib/constants'
import { RedirectStatusCode } from '../../client/components/redirect-status-code' import { RedirectStatusCode } from '../../client/components/redirect-status-code'
import { DevBundlerService } from './dev-bundler-service' import type { DevBundlerService } from './dev-bundler-service'
import { type Span, trace } from '../../trace' import type { Span } from '../../trace'
import { ensureLeadingSlash } from '../../shared/lib/page-path/ensure-leading-slash' import { ensureLeadingSlash } from '../../shared/lib/page-path/ensure-leading-slash'
import { getNextPathnameInfo } from '../../shared/lib/router/utils/get-next-pathname-info' import { getNextPathnameInfo } from '../../shared/lib/router/utils/get-next-pathname-info'
import { getHostname } from '../../shared/lib/get-hostname' import { getHostname } from '../../shared/lib/get-hostname'
...@@ -45,11 +44,9 @@ import { ...@@ -45,11 +44,9 @@ import {
type AppIsrManifestMessage, type AppIsrManifestMessage,
} from '../dev/hot-reloader-types' } from '../dev/hot-reloader-types'
import { normalizedAssetPrefix } from '../../shared/lib/normalized-asset-prefix' import { normalizedAssetPrefix } from '../../shared/lib/normalized-asset-prefix'
import { NEXT_PATCH_SYMBOL } from './patch-fetch'
import type { ServerInitResult } from './render-server' import type { ServerInitResult } from './render-server'
import { filterInternalHeaders } from './server-ipc/utils' import { filterInternalHeaders } from './server-ipc/utils'
import { blockCrossSite } from './router-utils/block-cross-site' import { blockCrossSite } from './router-utils/block-cross-site'
import { traceGlobals } from '../../trace/shared'
import { NoFallbackError } from '../../shared/lib/no-fallback-error.external' import { NoFallbackError } from '../../shared/lib/no-fallback-error.external'
import { import {
RouterServerContextSymbol, RouterServerContextSymbol,
...@@ -129,64 +126,34 @@ export async function initialize(opts: { ...@@ -129,64 +126,34 @@ export async function initialize(opts: {
let originalFetch = globalThis.fetch let originalFetch = globalThis.fetch
if (opts.dev) { // Set up testProxy BEFORE initializing dev server so that the intercepted fetch
const { Telemetry } = // is captured as originalFetch and used for HMR resets
require('../../telemetry/storage') as typeof import('../../telemetry/storage') 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({ if (opts.dev) {
distDir: path.join(opts.dir, config.distDir), // 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 = renderServer.instance =
...@@ -692,14 +659,11 @@ export async function initialize(opts: { ...@@ -692,14 +659,11 @@ export async function initialize(opts: {
let requestHandler: WorkerRequestHandler = requestHandlerImpl let requestHandler: WorkerRequestHandler = requestHandlerImpl
if (config.experimental.testProxy) { if (config.experimental.testProxy) {
// Intercept fetch and other testmode apis. // Wrap the request handler for testmode (interceptTestApis was already called earlier)
const { wrapRequestHandlerWorker, interceptTestApis } = const { wrapRequestHandlerWorker } =
// eslint-disable-next-line @next/internal/typechecked-require -- experimental/testmode is not built ins next/dist/esm // 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') require('next/dist/experimental/testmode/server') as typeof import('../../experimental/testmode/server')
requestHandler = wrapRequestHandlerWorker(requestHandler) 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 requestHandlers[opts.dir] = requestHandler
......
...@@ -370,7 +370,15 @@ async function startWatcher( ...@@ -370,7 +370,15 @@ async function startWatcher(
}, },
}) })
const fileWatchTimes = new Map() 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 previousClientRouterFilters: any
let previousConflictingPagePaths: Set<string> = new Set() let previousConflictingPagePaths: Set<string> = new Set()
...@@ -1118,6 +1126,11 @@ async function startWatcher( ...@@ -1118,6 +1126,11 @@ async function startWatcher(
} }
prevSortedRoutes = sortedRoutes prevSortedRoutes = sortedRoutes
// Ensure the deferred TypeScript verification is complete before proceeding
if (!tsVerificationComplete) {
await tsVerificationPromise
}
if (enabledTypeScript) { if (enabledTypeScript) {
// Using === false to make the check clearer. // Using === false to make the check clearer.
if (typescriptStatusFromLastAggregation === false) { if (typescriptStatusFromLastAggregation === false) {
......
...@@ -374,8 +374,6 @@ export async function startServer( ...@@ -374,8 +374,6 @@ export async function startServer(
logBundler: isDev, logBundler: isDev,
}) })
Log.event(`Starting...`)
let cleanupStarted = false let cleanupStarted = false
let closeUpgraded: (() => void) | null = null let closeUpgraded: (() => void) | null = null
const cleanup = () => { const cleanup = () => {
...@@ -524,13 +522,22 @@ export async function startServer( ...@@ -524,13 +522,22 @@ export async function startServer(
} }
if (process.env.NEXT_PRIVATE_WORKER && process.send) { if (process.env.NEXT_PRIVATE_WORKER && process.send) {
process.addListener('message', async (msg: any) => { // Read server options from env to eliminate IPC handshake latency
if ( let workerOptions = null
msg && if (process.env.NEXT_PRIVATE_WORKER_OPTIONS) {
typeof msg === 'object' && try {
msg.nextWorkerOptions && workerOptions = JSON.parse(process.env.NEXT_PRIVATE_WORKER_OPTIONS)
process.send } 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, { startServerSpan = trace('start-dev-server', undefined, {
cpus: String(os.cpus().length), cpus: String(os.cpus().length),
platform: os.platform(), platform: os.platform(),
...@@ -538,9 +545,7 @@ if (process.env.NEXT_PRIVATE_WORKER && process.send) { ...@@ -538,9 +545,7 @@ if (process.env.NEXT_PRIVATE_WORKER && process.send) {
'memory.totalMem': String(os.totalmem()), 'memory.totalMem': String(os.totalmem()),
'memory.heapSizeLimit': String(v8.getHeapStatistics().heap_size_limit), 'memory.heapSizeLimit': String(v8.getHeapStatistics().heap_size_limit),
}) })
await startServerSpan.traceAsyncFn(() => await startServerSpan.traceAsyncFn(() => startServer(workerOptions))
startServer(msg.nextWorkerOptions)
)
const memoryUsage = process.memoryUsage() const memoryUsage = process.memoryUsage()
startServerSpan.setAttribute('memory.rss', String(memoryUsage.rss)) startServerSpan.setAttribute('memory.rss', String(memoryUsage.rss))
startServerSpan.setAttribute( startServerSpan.setAttribute(
...@@ -551,8 +556,7 @@ if (process.env.NEXT_PRIVATE_WORKER && process.send) { ...@@ -551,8 +556,7 @@ if (process.env.NEXT_PRIVATE_WORKER && process.send) {
'memory.heapUsed', 'memory.heapUsed',
String(memoryUsage.heapUsed) String(memoryUsage.heapUsed)
) )
process.send({ nextServerReady: true, port: process.env.PORT }) process.send!({ nextServerReady: true, port: process.env.PORT })
} })()
}) }
process.send({ nextWorkerReady: true })
} }
...@@ -9,7 +9,6 @@ import { getAnonymousMeta } from './anonymous-meta' ...@@ -9,7 +9,6 @@ import { getAnonymousMeta } from './anonymous-meta'
import * as ciEnvironment from '../server/ci-info' import * as ciEnvironment from '../server/ci-info'
import { postNextTelemetryPayload } from './post-telemetry-payload' import { postNextTelemetryPayload } from './post-telemetry-payload'
import { getRawProjectId } from './project-id' import { getRawProjectId } from './project-id'
import { AbortController } from 'next/dist/compiled/@edge-runtime/ponyfill'
import fs from 'fs' import fs from 'fs'
// This is the key that stores whether or not telemetry is enabled or disabled. // 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', () => { 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 () => { it('should show error when there is no production build', async () => {
const next = await createNext({ // Set up stderr listener before starting to capture the error
files: __dirname, let errorFound = false
skipStart: true, next.on('stderr', (msg: string) => {
startCommand: `pnpm next start`, if (msg.includes('Could not find a production build in the')) {
serverReadyPattern: /✓ Starting.../, errorFound = true
}
}) })
await next.start() // start() will throw because the server exits with error code
await new Promise<void>((resolve, reject) => { try {
next.on('stderr', (msg) => { await next.start()
if (msg.includes('Could not find a production build in the')) { } catch (e) {
resolve() // Expected - server should fail to start without a build
} }
})
})
await next.destroy() expect(errorFound).toBe(true)
}) })
}) })
...@@ -11,8 +11,9 @@ describe('instrumentation-order', () => { ...@@ -11,8 +11,9 @@ describe('instrumentation-order', () => {
await next.fetch('/') await next.fetch('/')
await retry(async () => { await retry(async () => {
const serverLog = next.cliOutput.split('Starting...')[1] // Filter CLI output for the instrumentation-related logs
const cliOutputLines = serverLog.split('\n') // We look for these logs starting from when instrumentation begins
const cliOutputLines = next.cliOutput.split('\n')
const ORDERED_LOGS = [ const ORDERED_LOGS = [
'instrumentation:side-effect', 'instrumentation:side-effect',
...@@ -21,8 +22,21 @@ describe('instrumentation-order', () => { ...@@ -21,8 +22,21 @@ describe('instrumentation-order', () => {
'instrumentation:register:end', 'instrumentation:register:end',
'global-side-effect:app-router-page', '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) 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