UNPKG

next

Version:

The React Framework

1,044 lines 82.8 kB
import { mkdir, writeFile } from 'fs/promises'; import { realpathSync } from 'fs'; import * as inspector from 'inspector'; import { join, extname, relative, isAbsolute, sep } from 'path'; import { fileURLToPath, pathToFileURL } from 'url'; import ws from 'next/dist/compiled/ws'; import { store as consoleStore } from '../../build/output/store'; import { HMR_MESSAGE_SENT_TO_BROWSER } from './hot-reloader-types'; import { createDefineEnv, getBindingsSync, HmrTarget } from '../../build/swc'; import * as Log from '../../build/output/log'; import { BLOCKED_PAGES } from '../../shared/lib/constants'; import { getOverlayMiddleware, getSourceMapMiddleware, getOriginalStackFrames } from './middleware-turbopack'; import { PageNotFoundError } from '../../shared/lib/utils'; import { debounce } from '../utils'; import { clearManifestCache } from '../load-manifest.external'; import { deleteCache } from './require-cache'; import { clearAllModuleContexts, clearModuleContext } from '../lib/render-server'; import { denormalizePagePath } from '../../shared/lib/page-path/denormalize-page-path'; import { trace } from '../../trace'; import { AssetMapper, handleEntrypoints, handlePagesErrorRoute, handleRouteType, hasEntrypointForKey, msToNs, processTopLevelIssues, printNonFatalIssue, normalizedPageToTurbopackStructureRoute } from './turbopack-utils'; import { propagateServerField } from '../lib/router-utils/setup-dev-bundler'; import { TurbopackManifestLoader } from '../../shared/lib/turbopack/manifest-loader'; import { findPagePathData } from './on-demand-entry-handler'; import { getEntryKey, splitEntryKey } from '../../shared/lib/turbopack/entry-key'; import { createBinaryHmrMessageData, FAST_REFRESH_RUNTIME_RELOAD } from './messages'; import { generateEncryptionKeyBase64 } from '../app-render/encryption-utils-server'; import { isAppPageRouteDefinition } from '../route-definitions/app-page-route-definition'; import { normalizeAppPath } from '../../shared/lib/router/utils/app-paths'; import { isDeferredEntry } from '../../build/entries'; import { isMetadataRouteFile } from '../../lib/metadata/is-metadata-route'; import { setBundlerFindSourceMapImplementation } from '../patch-error-inspect'; import { setBundlerFindSourceMapURLImplementation } from '../lib/source-maps'; import { getNextErrorFeedbackMiddleware } from '../../next-devtools/server/get-next-error-feedback-middleware'; import { formatIssue, isFileSystemCacheEnabledForDev, isWellKnownError, ModuleBuildError, processIssues, renderStyledStringToErrorAnsi } from '../../shared/lib/turbopack/utils'; import { getDevOverlayFontMiddleware } from '../../next-devtools/server/font/get-dev-overlay-font-middleware'; import { devIndicatorServerState } from './dev-indicator-server-state'; import { getDisableDevIndicatorMiddleware } from '../../next-devtools/server/dev-indicator-middleware'; import { getRestartDevServerMiddleware } from '../../next-devtools/server/restart-dev-server-middleware'; import { backgroundLogCompilationEvents } from '../../shared/lib/turbopack/compilation-events'; import { DeferredEmit } from '../../shared/lib/turbopack/deferred-emit'; import { getSupportedBrowsers } from '../../build/get-supported-browsers'; import { printBuildErrors } from '../../build/print-build-errors'; import { receiveBrowserLogsTurbopack } from './browser-logs/receive-logs'; import { normalizePath } from '../../lib/normalize-path'; import { seedTurbopackCacheIfNeeded } from '../../lib/turbopack-cache-seed'; import { devToolsConfigMiddleware, getDevToolsConfig } from '../../next-devtools/server/devtools-config-middleware'; import { getAttachNodejsDebuggerMiddleware } from '../../next-devtools/server/attach-nodejs-debugger-middleware'; import { connectReactDebugChannel, connectReactDebugChannelForHtmlRequest, deleteReactDebugChannelForHtmlRequest, setReactDebugChannelForHtmlRequest } from './debug-channel'; import { getVersionInfo, matchNextPageBundleRequest } from './hot-reloader-shared-utils'; import { getMcpMiddleware } from '../mcp/get-mcp-middleware'; import { formatCompilationIssues } from '../mcp/tools/utils/format-compilation-issues'; import { getRequestInsightsSnapshot, isRequestInsightsEnabled } from '../lib/trace/request-insights'; import { resolvePathToRoute } from '../mcp/tools/utils/resolve-path-to-route'; import { handleErrorStateResponse } from '../mcp/tools/get-errors'; import { handlePageMetadataResponse } from '../mcp/tools/get-page-metadata'; import { setStackFrameResolver } from '../mcp/tools/utils/format-errors'; import { recordMcpTelemetry } from '../mcp/mcp-telemetry-tracker'; import { getFileLogger } from './browser-logs/file-logger'; import { sendSerializedErrorsToClient, sendSerializedErrorsToClientForHtmlRequest, setErrorsRscStreamForHtmlRequest } from './serialized-errors'; const wsServer = new ws.Server({ noServer: true }); const isTestMode = !!(process.env.NEXT_TEST_MODE || process.env.__NEXT_TEST_MODE || process.env.DEBUG); const sessionId = Math.floor(Number.MAX_SAFE_INTEGER * Math.random()); /** Output directory (relative to `distDir`) of server-HMR-managed chunks. */ const SERVER_HMR_CHUNKS_DIR = join('server', 'chunks'); /** * Collects the output chunk paths touched by a partial HMR update. Both * single-chunk `EcmascriptMergedUpdate`s and `ChunkListUpdate`s (which nest * per-chunk deltas inside `merged`) are flattened so the manifest cache can be * invalidated for every affected chunk after a successful apply. */ function collectUpdatedChunkPaths(instruction) { const paths = new Set(); if (instruction.type === 'EcmascriptMergedUpdate') { for (const chunkPath of Object.keys(instruction.chunks ?? {})){ paths.add(chunkPath); } } else if (instruction.type === 'ChunkListUpdate') { for (const chunkPath of Object.keys(instruction.chunks ?? {})){ paths.add(chunkPath); } for (const merged of instruction.merged ?? []){ for (const chunkPath of Object.keys(merged.chunks ?? {})){ paths.add(chunkPath); } } } else { throw Object.defineProperty(new Error(`[Server HMR] unreachable: unknown HMR instruction type ${instruction.type}`), "__NEXT_ERROR_CODE", { value: "E1459", enumerable: false, configurable: true }); } return Array.from(paths); } function setupServerHmr(project, { reEvaluateAllModulesExpensive, onApplied }) { async function runSubscription() { const subscription = project.allHmrEvents(HmrTarget.Server); // Subscribing immediately emits one event describing the current state. // There's no previous state to diff it against, so it never carries anything // to apply. Drop it; real updates start with the second event. await subscription.next(); for await (const result of subscription){ const update = result; // A 'restart' from the wire protocol means the update can't be applied // incrementally, so we must fully re-evaluate all chunks from disk. This // clears the module cache and notifies browsers to refetch RSC. const requiresFullReEvaluation = update.type === 'restart'; if (requiresFullReEvaluation) { await reEvaluateAllModulesExpensive(); continue; } if (update.type !== 'partial') { continue; } // `EcmascriptMergedUpdate` is the only instruction the Node.js runtime // knows how to apply; `ChunkListUpdate` is browser-only. Anything else is // unknown to us, so ignore it rather than evicting the module cache. const instruction = update.instruction; if (!instruction || instruction.type !== 'EcmascriptMergedUpdate' && instruction.type !== 'ChunkListUpdate') { throw Object.defineProperty(new Error(`[Server HMR] unreachable: unexpected update instruction type ${instruction.type}`), "__NEXT_ERROR_CODE", { value: "E1460", enumerable: false, configurable: true }); } // No handler registered yet (before first request, or right after // reEvaluateAllModulesExpensive()) — nothing live to update, so skip // until the next request. const handlers = globalThis.__turbopack_server_hmr_handlers__; if (!handlers || handlers.size === 0) { continue; } if (typeof __turbopack_server_hmr_apply__ === 'function') { try { __turbopack_server_hmr_apply__(update); } catch { // A matching runtime tried the apply and threw. Evict require.cache // so the next request loads fresh, then skip onApplied. (A no-match // update is a no-op and does not throw.) await reEvaluateAllModulesExpensive(); continue; } const updatedChunkPaths = collectUpdatedChunkPaths(instruction); // An empty partial only advances the version state (e.g. the seed // transition or a new endpoint); nothing changed on disk, so don't // invalidate manifests or ping browsers to refetch RSC. if (updatedChunkPaths.length > 0) { await onApplied(updatedChunkPaths); } } else { await reEvaluateAllModulesExpensive(); } } } // Start listening for changes in background. Re-subscribe on error so // server Fast Refresh continues working for the rest of the dev session. // The delay keeps a persistently-failing subscription (which throws on the // initial read) from hot-looping through reEvaluateAllModulesExpensive(). ; (async ()=>{ for(;;){ try { await runSubscription(); return; } catch (err) { console.error('[Server HMR] Subscription error, resubscribing:', err); await reEvaluateAllModulesExpensive(); await new Promise((resolve)=>setTimeout(resolve, 1000)); } } })(); } function getSourceMapFromTurbopack(project, sourceURL) { let sourceMapJson = null; try { sourceMapJson = project.getSourceMapSync(sourceURL); } catch (err) {} if (sourceMapJson === null) { return undefined; } else { return JSON.parse(sourceMapJson); } } function getSourceMapURLFromTurbopack(distDir, scriptNameOrSourceURL) { // React invokes this with the raw stack-frame filename, which arrives // either as an absolute filesystem path or as a `file:` URL. Anything else // (`file:` URLs with a query are eval'd server HMR modules carrying inline // source maps, `webpack-internal://`, `node:internal/...`, `<anonymous>`, // ...) is not something we have an emitted source map for. let scriptPath = scriptNameOrSourceURL; if (scriptNameOrSourceURL.startsWith('file://')) { if (scriptNameOrSourceURL.includes('?')) { return null; } try { scriptPath = fileURLToPath(scriptNameOrSourceURL); } catch { return null; } } if (!isAbsolute(scriptPath)) { return null; } // Only chunks emitted into `distDir` have an on-disk source map to point at. const relativePath = relative(distDir, scriptPath); if (relativePath.startsWith('..') || // On Windows an absolute path on a different drive is returned unchanged // rather than as a `..`-prefixed relative path. isAbsolute(relativePath)) { return null; } // The emitted source map lives next to its chunk with a `.map` suffix (see // `SourceMapAsset::path`). Encode through `pathToFileURL` so any special // characters in the path are escaped into a well-formed `file:` URL. return pathToFileURL(scriptPath + '.map').href; } export async function createHotReloaderTurbopack(opts, serverFields, distDir, resetFetch, lockfile, serverFastRefresh) { var _opts_nextConfig_turbopack, _nextConfig_watchOptions; const dev = true; const buildId = 'development'; const { nextConfig, dir: projectPath } = opts; const bindings = getBindingsSync(); // Turbopack requires native bindings and cannot run with WASM bindings. // Detect this early and give a clear, actionable error message. if (bindings.isWasm) { throw Object.defineProperty(new Error(`Turbopack is not supported on this platform (${process.platform}/${process.arch}) because native bindings are not available. ` + `Only WebAssembly (WASM) bindings were loaded, and Turbopack requires native bindings.\n\n` + `To use Next.js on this platform, use Webpack instead:\n` + ` next dev --webpack\n\n` + `For more information, see: https://nextjs.org/docs/app/api-reference/turbopack#supported-platforms`), "__NEXT_ERROR_CODE", { value: "E1049", enumerable: false, configurable: true }); } // For the debugging purpose, check if createNext or equivalent next instance setup in test cases // works correctly. Normally `run-test` hides output so only will be visible when `--debug` flag is used. if (isTestMode) { ; require('console').log('Creating turbopack project', { dir: projectPath, testMode: isTestMode }); } const hasRewrites = opts.fsChecker.rewrites.afterFiles.length > 0 || opts.fsChecker.rewrites.beforeFiles.length > 0 || opts.fsChecker.rewrites.fallback.length > 0; const hotReloaderSpan = trace('hot-reloader', undefined, { version: "16.3.0" }); // Ensure the hotReloaderSpan is flushed immediately as it's the parentSpan for all processing // of the current `next dev` invocation. hotReloaderSpan.stop(); // Initialize log monitor for file logging // Enable logging by default in development mode const mcpServerEnabled = !!nextConfig.experimental.mcpServer; const fileLogger = getFileLogger(); fileLogger.initialize(distDir, mcpServerEnabled); const encryptionKey = await generateEncryptionKeyBase64({ isBuild: false, distDir }); // TODO: Implement let clientRouterFilters; if (nextConfig.experimental.clientRouterFilter) { // TODO this need to be set correctly for filesystem cache to work } const supportedBrowsers = getSupportedBrowsers(projectPath, dev); const currentNodeJsVersion = process.versions.node; const rootPath = ((_opts_nextConfig_turbopack = opts.nextConfig.turbopack) == null ? void 0 : _opts_nextConfig_turbopack.root) || opts.nextConfig.outputFileTracingRoot || projectPath; if (nextConfig.experimental.turbopackSeedCacheFromWorktree) { seedTurbopackCacheIfNeeded({ projectDir: projectPath, distDir }); } const project = await bindings.turbo.createProject({ rootPath, projectPath: normalizePath(relative(rootPath, projectPath) || '.'), distDir, nextConfig: opts.nextConfig, watch: { enable: dev, pollIntervalMs: (_nextConfig_watchOptions = nextConfig.watchOptions) == null ? void 0 : _nextConfig_watchOptions.pollIntervalMs }, dev, env: process.env, defineEnv: createDefineEnv({ isTurbopack: true, clientRouterFilters, config: nextConfig, dev, distDir, projectPath, fetchCacheKeyPrefix: opts.nextConfig.experimental.fetchCacheKeyPrefix, hasRewrites, // TODO: Implement middlewareMatchers: undefined, rewrites: opts.fsChecker.rewrites }), buildId, encryptionKey, previewProps: opts.fsChecker.previewProps, browserslistQuery: supportedBrowsers.join(', '), noMangling: false, writeRoutesHashesManifest: false, currentNodeJsVersion, isPersistentCachingEnabled: isFileSystemCacheEnabledForDev(opts.nextConfig), nextVersion: "16.3.0", serverHmr: serverFastRefresh }, { turbopackMemoryEviction: opts.nextConfig.experimental.turbopackMemoryEvictionMode, isShortSession: false }); backgroundLogCompilationEvents(project, { eventTypes: [ 'StartupCacheInvalidationEvent', 'TimingEvent', 'SlowFilesystemEvent', 'TraceEvent' ], parentSpan: hotReloaderSpan }); setBundlerFindSourceMapImplementation(getSourceMapFromTurbopack.bind(null, project)); let canonicalDistDir = distDir; try { canonicalDistDir = realpathSync(distDir); } catch {} setBundlerFindSourceMapURLImplementation(getSourceMapURLFromTurbopack.bind(null, canonicalDistDir)); // Set up code frame renderer using native bindings const { installCodeFrameSupport } = require('../lib/install-code-frame'); installCodeFrameSupport(); opts.onDevServerCleanup == null ? void 0 : opts.onDevServerCleanup.call(opts, async ()=>{ setBundlerFindSourceMapImplementation(()=>undefined); setBundlerFindSourceMapURLImplementation(()=>null); await project.onExit(); await (lockfile == null ? void 0 : lockfile.unlock()); }); const entrypointsSubscription = project.entrypointsSubscribe(); const currentWrittenEntrypoints = new Map(); const currentEntrypoints = { global: { app: undefined, document: undefined, error: undefined, middleware: undefined, instrumentation: undefined }, page: new Map(), app: new Map() }; const currentTopLevelIssues = new Map(); const currentEntryIssues = new Map(); const manifestLoader = new TurbopackManifestLoader({ buildId, distDir, encryptionKey, dev: true, sriEnabled: false }); // Dev specific const changeSubscriptions = new Map(); const serverPathState = new Map(); const readyIds = new Set(); let currentEntriesHandlingResolve; let currentEntriesHandling = new Promise((resolve)=>currentEntriesHandlingResolve = resolve); const assetMapper = new AssetMapper(); // Deferred entries state management const deferredEntriesConfig = nextConfig.experimental.deferredEntries; const hasDeferredEntriesConfig = deferredEntriesConfig && deferredEntriesConfig.length > 0; let onBeforeDeferredEntriesCalled = false; let onBeforeDeferredEntriesPromise = null; // Track non-deferred entries that are currently being built const nonDeferredBuildingEntries = new Set(); // Function to wait for all non-deferred entries to be built async function waitForNonDeferredEntries() { return new Promise((resolve)=>{ const checkEntries = ()=>{ // Check if there are any non-deferred entries that are still building if (nonDeferredBuildingEntries.size === 0) { resolve(); } else { // Check again after a short delay setTimeout(checkEntries, 100); } }; checkEntries(); }); } // Function to handle deferred entry processing async function processDeferredEntry() { if (!hasDeferredEntriesConfig) return; // Wait for all non-deferred entries to be built await waitForNonDeferredEntries(); // Call the onBeforeDeferredEntries callback once if (!onBeforeDeferredEntriesCalled) { onBeforeDeferredEntriesCalled = true; if (nextConfig.experimental.onBeforeDeferredEntries) { if (!onBeforeDeferredEntriesPromise) { onBeforeDeferredEntriesPromise = nextConfig.experimental.onBeforeDeferredEntries(); } await onBeforeDeferredEntriesPromise; } } else if (onBeforeDeferredEntriesPromise) { // Wait for any in-progress callback await onBeforeDeferredEntriesPromise; } } // Track whether HMR is pending - used to call callback once after HMR settles let hmrPendingDeferredCallback = false; // Debounced function to call onBeforeDeferredEntries after HMR // This prevents rapid-fire calls when turbopack fires many update events // Use 500ms debounce to ensure all rapid updates are batched together const callOnBeforeDeferredEntriesAfterHMR = debounce(()=>{ // Only call if HMR triggered a need for the callback if (hasDeferredEntriesConfig && hmrPendingDeferredCallback) { hmrPendingDeferredCallback = false; onBeforeDeferredEntriesCalled = true; if (nextConfig.experimental.onBeforeDeferredEntries) { onBeforeDeferredEntriesPromise = nextConfig.experimental.onBeforeDeferredEntries(); } } }, 500); function clearRequireCache(key, writtenEndpoint, { force } = {}) { if (force) { for (const { path, contentHash } of writtenEndpoint.serverPaths){ // We ignore source maps if (path.endsWith('.map')) continue; const localKey = `${key}:${path}`; serverPathState.set(localKey, contentHash); serverPathState.set(path, contentHash); } } else { // Figure out if the server files have changed let hasChange = false; const currentPaths = new Set(); for (const { path, contentHash } of writtenEndpoint.serverPaths){ // We ignore source maps if (path.endsWith('.map')) continue; currentPaths.add(path); const localKey = `${key}:${path}`; const localHash = serverPathState.get(localKey); const globalHash = serverPathState.get(path); if (localHash && localHash !== contentHash || globalHash && globalHash !== contentHash) { hasChange = true; serverPathState.set(localKey, contentHash); serverPathState.set(path, contentHash); } else { if (!localHash) { serverPathState.set(localKey, contentHash); } if (!globalHash) { serverPathState.set(path, contentHash); } } } const localKeyPrefix = `${key}:`; for (const pathKey of serverPathState.keys()){ if (pathKey.startsWith(localKeyPrefix) && !currentPaths.has(pathKey.slice(localKeyPrefix.length))) { serverPathState.delete(pathKey); hasChange = true; } } if (!hasChange) { return false; } } // Edge does not participate in server HMR. if (writtenEndpoint.type === 'edge') { void clearAllModuleContexts(); } const serverPaths = writtenEndpoint.serverPaths.map(({ path: p })=>join(distDir, p)); const { type: entryType } = splitEntryKey(key); // Server HMR applies to App Router entries built with the Turbopack Node.js // runtime: app pages and route handlers (including metadata routes). Edge // routes, Pages Router pages, and middleware/instrumentation are excluded. const usesServerHmr = serverFastRefresh && entryType === 'app' && writtenEndpoint.type !== 'edge'; const serverChunksPrefix = SERVER_HMR_CHUNKS_DIR + sep; const filesToDelete = []; for (const file of serverPaths){ clearModuleContext(file); const relativePath = relative(distDir, file); if (// For Pages Router, edge routes, middleware, and any entry not // participating in server HMR: clear the sharedCache in // evalManifest(), Node.js require.cache, and edge runtime module // contexts. force || !usesServerHmr || !relativePath.startsWith(serverChunksPrefix)) { filesToDelete.push(file); } } deleteCache(filesToDelete); // Reset the fetch patch so patchFetch() can re-wrap on the next request. if (serverPaths.length > 0) { resetFetch(); } // Clear Turbopack's chunk-loading cache so chunks are re-required from disk on // the next request. // // For App Router with server HMR, this is normally skipped as server HMR // manages module updates in-place. However, it *is* required when force is `true` // (like for .env file or tsconfig changes). if ((!usesServerHmr || force) && typeof __next__clear_chunk_cache__ === 'function') { __next__clear_chunk_cache__(); } return true; } const buildingIds = new Set(); const startBuilding = (id, requestUrl, forceRebuild)=>{ if (!forceRebuild && readyIds.has(id)) { return ()=>{}; } if (buildingIds.size === 0) { consoleStore.setState({ loading: true, trigger: id, url: requestUrl }, true); } buildingIds.add(id); return function finishBuilding() { if (buildingIds.size === 0) { return; } readyIds.add(id); buildingIds.delete(id); if (buildingIds.size === 0) { hmrEventHappened = false; consoleStore.setState({ loading: false }, true); } }; }; let hmrEventHappened = false; // A counter identifying the current version of the compiled output, included // by `"use cache"` in dev cache keys so that cached entries revalidate after // an edit. It advances once per HMR change event (for App Router pages that // is an RSC change, which is what a cached render depends on), independent of // how many clients are connected. It deliberately does not advance on `BUILT` // messages: those are sent per connected client on every compilation, so // advancing there would both churn the hash without an edit and fail to // advance it at all when no client is connected. let hmrHash = 0; // HACK: Defer sending `building` messages. Turbopack emits a compile pass for every // foreground-job cycle, including empty no-op recompiles scheduled by // request/render activity that changed no files. This allows us to prevent // sending them if we quickly get a `built` message after a `building` message. const pendingBuilding = new DeferredEmit(); const clientsWithoutHtmlRequestId = new Set(); const clientsByHtmlRequestId = new Map(); const cacheStatusesByHtmlRequestId = new Map(); const clientStates = new WeakMap(); function sendToClient(client, message) { const data = typeof message.type === 'number' ? createBinaryHmrMessageData(message) : JSON.stringify(message); client.send(data); } let updateInProgress = false; let pendingServerComponentChanges = false; function sendServerComponentChanges() { sendHmr('server-component-changes', { type: HMR_MESSAGE_SENT_TO_BROWSER.SERVER_COMPONENT_CHANGES }); } // Each announcement makes every client refetch its page, so an update's // changes are announced once, on the update's end. function handleServerComponentChanges() { if (updateInProgress) { pendingServerComponentChanges = true; } else { sendServerComponentChanges(); } } function hasCompilationErrors() { for (const [, issueMap] of currentEntryIssues){ if ([ ...issueMap.values() ].filter((i)=>i.severity !== 'warning').length > 0) { return true; } } return false; } function sendEnqueuedMessages() { if (hasCompilationErrors()) { // During compilation errors we want to delay the HMR events until errors are fixed return; } for (const client of [ ...clientsWithoutHtmlRequestId, ...clientsByHtmlRequestId.values() ]){ const state = clientStates.get(client); if (!state) { continue; } for (const [, issueMap] of state.clientIssues){ if ([ ...issueMap.values() ].filter((i)=>i.severity !== 'warning').length > 0) { // During compilation errors we want to delay the HMR events until errors are fixed return; } } for (const message of state.messages.values()){ sendToClient(client, message); } state.messages.clear(); if (state.turbopackUpdates.length > 0) { sendToClient(client, { type: HMR_MESSAGE_SENT_TO_BROWSER.TURBOPACK_MESSAGE, data: state.turbopackUpdates }); state.turbopackUpdates.length = 0; } } } const sendEnqueuedMessagesDebounce = debounce(sendEnqueuedMessages, 2); const sendHmr = (id, message)=>{ pendingBuilding.flush(); for (const client of [ ...clientsWithoutHtmlRequestId, ...clientsByHtmlRequestId.values() ]){ var _clientStates_get; (_clientStates_get = clientStates.get(client)) == null ? void 0 : _clientStates_get.messages.set(id, message); } hmrEventHappened = true; sendEnqueuedMessagesDebounce(); }; function sendTurbopackMessage(payload) { // TODO(PACK-2049): For some reason we end up emitting hundreds of issues messages on bigger apps, // a lot of which are duplicates. // They are currently not handled on the client at all, so might as well not send them for now. payload.diagnostics = []; payload.issues = []; pendingBuilding.flush(); for (const client of [ ...clientsWithoutHtmlRequestId, ...clientsByHtmlRequestId.values() ]){ var _clientStates_get; (_clientStates_get = clientStates.get(client)) == null ? void 0 : _clientStates_get.turbopackUpdates.push(payload); } hmrEventHappened = true; sendEnqueuedMessagesDebounce(); } async function subscribeToClientChanges(key, includeIssues, endpoint, createMessage, onError) { if (changeSubscriptions.has(key)) { return; } const { side } = splitEntryKey(key); const changedPromise = endpoint[`${side}Changed`](includeIssues); changeSubscriptions.set(key, changedPromise); try { const changed = await changedPromise; for await (const change of changed){ processIssues(currentEntryIssues, key, change, false, true); // TODO: Get an actual content hash from Turbopack. const message = await createMessage(change, String(++hmrHash)); if (message) { sendHmr(key, message); } } } catch (e) { changeSubscriptions.delete(key); const payload = await (onError == null ? void 0 : onError(e)); if (payload) { sendHmr(key, payload); } return; } changeSubscriptions.delete(key); } async function unsubscribeFromClientChanges(key) { const subscription = await changeSubscriptions.get(key); if (subscription) { await (subscription.return == null ? void 0 : subscription.return.call(subscription)); changeSubscriptions.delete(key); } currentEntryIssues.delete(key); } async function subscribeToClientHmrEvents(client, id) { const key = getEntryKey('assets', 'client', id); if (!hasEntrypointForKey(currentEntrypoints, key, assetMapper)) { // maybe throw an error / force the client to reload? return; } const state = clientStates.get(client); if (!state || state.subscriptions.has(id)) { return; } const subscription = project.hmrEvents(id, HmrTarget.Client); state.subscriptions.set(id, subscription); // The subscription will always emit once, which is the initial // computation. This is not a change, so swallow it. try { await subscription.next(); for await (const data of subscription){ processIssues(state.clientIssues, key, data, false, true); if (data.type !== 'issues') { sendTurbopackMessage(data); } } } catch (e) { // The client might be using an HMR session from a previous server, tell them // to fully reload the page to resolve the issue. We can't use // `hotReloader.send` since that would force every connected client to // reload, only this client is out of date. const reloadMessage = { type: HMR_MESSAGE_SENT_TO_BROWSER.RELOAD_PAGE, data: `error in HMR event subscription for ${id}: ${e}` }; sendToClient(client, reloadMessage); client.close(); return; } } function unsubscribeFromClientHmrEvents(client, id) { const state = clientStates.get(client); if (!state) { return; } const subscription = state.subscriptions.get(id); subscription == null ? void 0 : subscription.return(); const key = getEntryKey('assets', 'client', id); state.clientIssues.delete(key); } async function handleEntrypointsSubscription() { for await (const entrypoints of entrypointsSubscription){ if (!currentEntriesHandlingResolve) { currentEntriesHandling = new Promise(// eslint-disable-next-line no-loop-func (resolve)=>currentEntriesHandlingResolve = resolve); } // Always process issues/diagnostics, even if there are no entrypoints yet processTopLevelIssues(currentTopLevelIssues, entrypoints); // Certain crtical issues prevent any entrypoints from being constructed so return early if (!('routes' in entrypoints)) { printBuildErrors(entrypoints, true); currentEntriesHandlingResolve(); currentEntriesHandlingResolve = undefined; continue; } const routes = entrypoints.routes; const existingRoutes = [ ...currentEntrypoints.app.keys(), ...currentEntrypoints.page.keys() ]; const newRoutes = [ ...routes.keys() ]; const addedRoutes = newRoutes.filter((route)=>!currentEntrypoints.app.has(route) && !currentEntrypoints.page.has(route)); const removedRoutes = existingRoutes.filter((route)=>!routes.has(route)); await handleEntrypoints({ entrypoints: entrypoints, currentEntrypoints, currentEntryIssues, manifestLoader, devRewrites: opts.fsChecker.rewrites, productionRewrites: undefined, logErrors: true, dev: { assetMapper, changeSubscriptions, clients: [ ...clientsWithoutHtmlRequestId, ...clientsByHtmlRequestId.values() ], clientStates, serverFields, hooks: { handleWrittenEndpoint: (id, result, forceDeleteCache)=>{ currentWrittenEntrypoints.set(id, result); return clearRequireCache(id, result, { force: forceDeleteCache }); }, propagateServerField: propagateServerField.bind(null, opts), sendHmr, startBuilding, subscribeToChanges: subscribeToClientChanges, unsubscribeFromChanges: unsubscribeFromClientChanges, unsubscribeFromHmrEvents: unsubscribeFromClientHmrEvents } } }); // Reload matchers when the files have been compiled await propagateServerField(opts, 'reloadMatchers', undefined); if (addedRoutes.length > 0 || removedRoutes.length > 0) { // When the list of routes changes a new manifest should be fetched for Pages Router. hotReloader.send({ type: HMR_MESSAGE_SENT_TO_BROWSER.DEV_PAGES_MANIFEST_UPDATE, data: [ { devPagesManifest: true } ] }); } for (const route of addedRoutes){ hotReloader.send({ type: HMR_MESSAGE_SENT_TO_BROWSER.ADDED_PAGE, data: [ route ] }); } for (const route of removedRoutes){ hotReloader.send({ type: HMR_MESSAGE_SENT_TO_BROWSER.REMOVED_PAGE, data: [ route ] }); } currentEntriesHandlingResolve(); currentEntriesHandlingResolve = undefined; } } await mkdir(join(distDir, 'server'), { recursive: true }); await mkdir(join(distDir, 'static', buildId), { recursive: true }); await writeFile(join(distDir, 'package.json'), JSON.stringify({ type: 'commonjs' }, null, 2)); const middlewares = [ getOverlayMiddleware({ project, projectPath, isSrcDir: opts.isSrcDir }), getSourceMapMiddleware(project), getNextErrorFeedbackMiddleware(opts.telemetry), getDevOverlayFontMiddleware(), getDisableDevIndicatorMiddleware(), getRestartDevServerMiddleware({ telemetry: opts.telemetry, turbopackProject: project }), devToolsConfigMiddleware({ distDir, sendUpdateSignal: (data)=>{ hotReloader.send({ type: HMR_MESSAGE_SENT_TO_BROWSER.DEVTOOLS_CONFIG, data }); } }), getAttachNodejsDebuggerMiddleware(), ...nextConfig.experimental.mcpServer ? [ getMcpMiddleware({ projectPath, distDir, nextConfig, pagesDir: opts.pagesDir, appDir: opts.appDir, sendHmrMessage: (message)=>hotReloader.send(message), getActiveConnectionCount: ()=>clientsWithoutHtmlRequestId.size + clientsByHtmlRequestId.size, getDevServerUrl: ()=>process.env.__NEXT_PRIVATE_ORIGIN, getTurbopackProject: ()=>project, compileRoute: async ({ routeSpecifier, path })=>{ // Resolve the caller's input to a concrete route specifier. The // path-mode branch reuses the dev router's own live route table // (opts.fsChecker) — the same one resolve-routes.ts consults on // every incoming HTTP request — so first-match ordering and live // route updates are inherited for free. let page; if (routeSpecifier != null) { page = routeSpecifier; } else if (path != null) { const resolved = resolvePathToRoute(path, { appFiles: opts.fsChecker.appFiles, pageFiles: opts.fsChecker.pageFiles, dynamicRoutes: opts.fsChecker.getDynamicRoutes() }); if ('notFound' in resolved) { const err = Object.defineProperty(new Error(`no route matched for path "${resolved.pathname}"`), "__NEXT_ERROR_CODE", { value: "E1277", enumerable: false, configurable: true }); err.code = 'ENOENT'; throw err; } page = resolved.routeSpecifier; } else { // Tool handler rejects the empty case; defend the boundary. throw Object.defineProperty(new Error('compileRoute: either routeSpecifier or path is required'), "__NEXT_ERROR_CODE", { value: "E1278", enumerable: false, configurable: true }); } // ensurePage uses findPagePathData when no definition is provided, // which calls normalizePagePath("/") → "/index" then findPageFile // looking for "index.tsx" — neither of which matches "page.tsx" in // the app dir. Pass a synthetic definition instead. // // currentEntrypoints.app is keyed by originalName which includes the // trailing /page or /route segment (e.g. "/page" for the root route, // "/blog/[slug]/page" for a dynamic page). Use normalizeAppPath to // strip that suffix and find the entry matching the user-facing route. let extraOptions = undefined; for (const [name] of currentEntrypoints.app){ if (normalizeAppPath(name) === page) { extraOptions = { // Synthesize a definition so ensurePage bypasses findPagePathData. // Only page and bundlePath are used from the definition: // - page: the originalName used as the route key for currentEntrypoints lookup // - bundlePath: must start with "app/" to set isInsideAppDir=true definition: { page: name, bundlePath: `app${name}`, filename: '' } }; break; } } const ensureOpts = { page, // Compile both server and client bundles, matching what happens // on a real page navigation. Client-only compilation isn't a // meaningful MCP use case so we don't expose it as a knob. clientOnly: false, // Skip wiring HMR subscriptions: there is no client to receive // updates for routes compiled this way, and these subscriptions // are never unsubscribed (see TODOs in handleRouteType). subscribeToChanges: false, ...extraOptions }; // Snapshot the current issue maps before compilation so we can // identify which entry keys were added or updated by this call. // processIssues always creates a new Map() reference, so identity // comparison detects changes even for re-compilations. const snapshotBefore = new Map(currentEntryIssues); // For app-page routes, processIssues is called with throwIssue=true, // meaning it throws ModuleBuildError when there are compile errors—but // it still writes the issues into currentEntryIssues before throwing. // Catch ModuleBuildError so we can read those issues and return them // as structured output rather than propagating the throw. let moduleBuildError; try { await hotReloader.ensurePage(ensureOpts); } catch (err) { if (err instanceof ModuleBuildError) { moduleBuildError = err; } else { throw err; } } const rawIssues = []; for (const [key, issueMap] of currentEntryIssues){ if (snapshotBefore.get(key) !== issueMap) { rawIssues.push(...issueMap.values()); } } // If ensurePage threw ModuleBuildError but we found no new issues in // the map (shouldn't happen, but be safe), re-surface the original // error so its message and stack are preserved. if (moduleBuildError && rawIssues.length === 0) { throw moduleBuildError; } return { routeSpecifier: page, issues: formatCompilationIssues(rawIssues) }; } }) ] : [] ]; setStackFrameResolver(async (request)=>{ return getOriginalStackFrames({ project, projectPath, isServer: request.isServer, isEdgeServer: request.isEdgeServer, isAppDirectory: request.isAppDirectory, frames: request.frames }); }); let versionInfoCached; // This fetch, even though not awaited, is not kicked off eagerly because the first `fetch()` in // Node.js adds roughly 20ms main-thread blocking to load the SSL certificate cache // We don't want that blocking time to be in the hot path for the `ready in` logging. // Instead, the fetch is kicked off lazily when the first `getVersionInfoCached()` is called. const getVersionInfoCached = ()=>{ if (!versionInfoCached) { versionInfoCached = getVersionInfo(); } return versionInfoCached; }; let devtoolsFrontendUrl; const inspectorURLRaw = inspector.url(); if (inspectorURLRaw !== undefined) { const inspectorURL = new URL(inspectorURLRaw); let debugInfo; try { const debugInfoList = await fetch(`http://${inspectorURL.host}/json/list`).then((res)=>res.json()); debugInfo = debugInfoList[0]; } catch {} if (debugInfo) { devtoolsFrontendUrl = debugInfo.devtoolsFrontendUrl; } } const hotReloader = { turbopackProject: project, activeWebpackConfigs: undefined, serverStats: null, edgeServerStats: null, async run (req, res, _parsedUrl) { var _req_url; // intercept page chunks request and ensure them with turbopack if ((_req_url = req.url) == null ? void 0 : _req_url.startsWith('/_next/static/chunks/pages/')) { const params = matchNextPageBundleRequest(req.url); if (params) { const decodedPagePath = `/${params.path.map((param)=>decodeURIComponent(param)).join('/')}`; const denormalizedPagePath = denormalizePagePath(decodedPagePath); await hotReloader.ensurePage({ page: denormalizedPagePath, clientOnly: false, definition: undefined, url: req.url }).catch(console.error); } } for (const middleware of middlewares){ let calledNext = false; await middleware(req, res, ()=>{ calledNext = true; }); if (!calledNext) { return { finished: true }; } } // Request was not finished. return { finished: undefined }; }, // TODO: Figure out if socket type can match the NextJsHotReloaderInterface onHMR (req, socket, head, onUpgrade) { wsServer.handleUpgrade(req, socket, head, (client)=>{ const clientIssues = new Map(); const subscriptions = new Map(); const htmlRequestId = req.url ? new URL(req.url, 'http://n').searchParams.get('id') : null; // Clients with a request ID ar