next
Version:
The React Framework
319 lines (318 loc) • 12.4 kB
TypeScript
import type { IncomingMessage, ServerResponse } from 'http';
import type { UrlObject } from 'url';
import type { Duplex } from 'stream';
import type { webpack } from 'next/dist/compiled/webpack/webpack';
import type getBaseWebpackConfig from '../../build/webpack-config';
import type { RouteDefinition } from '../route-definitions/route-definition';
import type { Project, Update as TurbopackUpdate } from '../../build/swc/types';
import type { VersionInfo } from './parse-version-info';
import type { DebugInfo } from '../../next-devtools/shared/types';
import type { DevIndicatorServerState } from './dev-indicator-server-state';
import type { CacheIndicatorState, ServerCacheStatus } from '../../next-devtools/dev-overlay/cache-indicator';
import type { DevToolsConfig } from '../../next-devtools/dev-overlay/shared';
import type { RequestInsight, RequestInsightsSnapshot } from '../../next-devtools/shared/request-insights';
import type { ReactDebugChannelForBrowser } from './debug-channel';
import type { AnyStream } from '../app-render/stream-ops';
export declare const enum HMR_MESSAGE_SENT_TO_BROWSER {
ADDED_PAGE = "addedPage",
REMOVED_PAGE = "removedPage",
RELOAD_PAGE = "reloadPage",
SERVER_COMPONENT_CHANGES = "serverComponentChanges",
STATIC_PARAMS_CHANGED = "staticParamsChanged",
MIDDLEWARE_CHANGES = "middlewareChanges",
CLIENT_CHANGES = "clientChanges",
SERVER_ONLY_CHANGES = "serverOnlyChanges",
SYNC = "sync",
BUILT = "built",
BUILDING = "building",
DEV_PAGES_MANIFEST_UPDATE = "devPagesManifestUpdate",
TURBOPACK_MESSAGE = "turbopack-message",
SERVER_ERROR = "serverError",
TURBOPACK_CONNECTED = "turbopack-connected",
ISR_MANIFEST = "isrManifest",
CACHE_INDICATOR = "cacheIndicator",
DEV_INDICATOR = "devIndicator",
DEVTOOLS_CONFIG = "devtoolsConfig",
VULNERABILITY_INSIGHT = "vulnerabilityInsight",
REQUEST_CURRENT_ERROR_STATE = "requestCurrentErrorState",
RUNTIME_ERRORS = "runtimeErrors",
REQUEST_PAGE_METADATA = "requestPageMetadata",
REQUEST_INSIGHTS_UPDATE = "requestInsightsUpdate",
REACT_DEBUG_CHUNK = 0,
ERRORS_TO_SHOW_IN_BROWSER = 1
}
export declare const enum HMR_MESSAGE_SENT_TO_SERVER {
MCP_ERROR_STATE_RESPONSE = "mcp-error-state-response",
RUNTIME_ERRORS = "runtimeErrors",
MCP_PAGE_METADATA_RESPONSE = "mcp-page-metadata-response",
PING = "ping"
}
export interface ServerErrorMessage {
type: HMR_MESSAGE_SENT_TO_BROWSER.SERVER_ERROR;
errorJSON: string;
}
export interface TurbopackMessage {
type: HMR_MESSAGE_SENT_TO_BROWSER.TURBOPACK_MESSAGE;
data: TurbopackUpdate | TurbopackUpdate[];
hmrVersion: string;
}
export interface BuildingMessage {
type: HMR_MESSAGE_SENT_TO_BROWSER.BUILDING;
}
export interface CompilationError {
moduleName?: string;
message: string;
details?: string;
moduleTrace?: Array<{
moduleName?: string;
}>;
stack?: string;
}
export interface SyncMessage {
type: HMR_MESSAGE_SENT_TO_BROWSER.SYNC;
hash: string;
errors: ReadonlyArray<CompilationError>;
warnings: ReadonlyArray<CompilationError>;
versionInfo: VersionInfo;
hasVulnerabilityInsight: boolean;
updatedModules?: ReadonlyArray<string>;
debug?: DebugInfo;
devIndicator: DevIndicatorServerState;
devToolsConfig?: DevToolsConfig;
requestInsights?: RequestInsightsSnapshot;
}
export interface BuiltMessage {
type: HMR_MESSAGE_SENT_TO_BROWSER.BUILT;
hash: string;
errors: ReadonlyArray<CompilationError>;
warnings: ReadonlyArray<CompilationError>;
updatedModules?: ReadonlyArray<string>;
}
export interface AddedPageMessage {
type: HMR_MESSAGE_SENT_TO_BROWSER.ADDED_PAGE;
data: [page: string | null];
}
export interface RemovedPageMessage {
type: HMR_MESSAGE_SENT_TO_BROWSER.REMOVED_PAGE;
data: [page: string | null];
}
export interface ReloadPageMessage {
type: HMR_MESSAGE_SENT_TO_BROWSER.RELOAD_PAGE;
data: string;
}
export interface ServerComponentChangesMessage {
type: HMR_MESSAGE_SENT_TO_BROWSER.SERVER_COMPONENT_CHANGES;
hmrVersion?: string;
}
/**
* Sent in dev when a route's set of statically-known params changed, e.g.
* because `generateStaticParams` was added, removed, or edited.
*/
export interface StaticParamsChangedMessage {
type: HMR_MESSAGE_SENT_TO_BROWSER.STATIC_PARAMS_CHANGED;
}
export interface MiddlewareChangesMessage {
type: HMR_MESSAGE_SENT_TO_BROWSER.MIDDLEWARE_CHANGES;
}
export interface ClientChangesMessage {
type: HMR_MESSAGE_SENT_TO_BROWSER.CLIENT_CHANGES;
}
export interface ServerOnlyChangesMessage {
type: HMR_MESSAGE_SENT_TO_BROWSER.SERVER_ONLY_CHANGES;
pages: ReadonlyArray<string>;
}
export interface DevPagesManifestUpdateMessage {
type: HMR_MESSAGE_SENT_TO_BROWSER.DEV_PAGES_MANIFEST_UPDATE;
data: [
{
devPagesManifest: true;
}
];
}
export interface TurbopackConnectedMessage {
type: HMR_MESSAGE_SENT_TO_BROWSER.TURBOPACK_CONNECTED;
data: {
sessionId: number;
hmrVersion: string;
};
}
export interface AppIsrManifestMessage {
type: HMR_MESSAGE_SENT_TO_BROWSER.ISR_MANIFEST;
data: Record<string, boolean>;
}
export interface DevToolsConfigMessage {
type: HMR_MESSAGE_SENT_TO_BROWSER.DEVTOOLS_CONFIG;
data: DevToolsConfig;
}
export interface ReactDebugChunkMessage {
type: HMR_MESSAGE_SENT_TO_BROWSER.REACT_DEBUG_CHUNK;
requestId: string;
/**
* A null chunk signals to the browser that no more chunks will be sent.
*/
chunk: Uint8Array | null;
}
export interface ErrorsToShowInBrowserMessage {
type: HMR_MESSAGE_SENT_TO_BROWSER.ERRORS_TO_SHOW_IN_BROWSER;
serializedErrors: Uint8Array;
}
export interface RequestCurrentErrorStateMessage {
type: HMR_MESSAGE_SENT_TO_BROWSER.REQUEST_CURRENT_ERROR_STATE;
requestId: string;
}
export type RuntimeErrorBoundary = {
kind: 'default-global' | 'custom-global' | 'custom';
name?: string;
};
export interface RuntimeErrorMetadata {
fatal: boolean;
boundary?: RuntimeErrorBoundary;
}
export interface FormattedRuntimeError {
type: string;
errorName: string;
message: string;
/** A React root failure or a Next.js unrecoverable rendering path. */
fatal: boolean;
boundary?: RuntimeErrorBoundary;
stack: Array<{
file: string;
methodName: string;
line: number | null;
column: number | null;
}>;
}
export interface RuntimeErrorStateError {
id: number;
error: {
name?: string;
message?: string;
stack?: string;
source: 'server' | 'edge-server' | null;
} | null;
frames: readonly {
file: string | null;
methodName: string;
line1: number | null;
column1: number | null;
}[];
type: 'runtime' | 'recoverable' | 'console';
fatal: boolean;
boundary?: RuntimeErrorBoundary;
}
export interface RuntimeErrorStateUpdate {
event: HMR_MESSAGE_SENT_TO_SERVER.RUNTIME_ERRORS;
pathname: string;
errorState: {
errors: readonly RuntimeErrorStateError[];
routerType: 'app' | 'pages';
};
}
export interface RuntimeErrorStateMessage {
type: HMR_MESSAGE_SENT_TO_BROWSER.RUNTIME_ERRORS;
clientId: string;
/** The producer document's existing HMR request ID, when available. */
htmlRequestId?: string | null;
pathname: string;
errors: FormattedRuntimeError[];
}
export interface RequestPageMetadataMessage {
type: HMR_MESSAGE_SENT_TO_BROWSER.REQUEST_PAGE_METADATA;
requestId: string;
}
export interface CacheIndicatorMessage {
type: HMR_MESSAGE_SENT_TO_BROWSER.CACHE_INDICATOR;
state: CacheIndicatorState;
}
export interface RequestInsightsUpdateMessage {
type: HMR_MESSAGE_SENT_TO_BROWSER.REQUEST_INSIGHTS_UPDATE;
insight: RequestInsight;
}
export interface VulnerabilityInsightMessage {
type: HMR_MESSAGE_SENT_TO_BROWSER.VULNERABILITY_INSIGHT;
hasVulnerabilityInsight: boolean;
}
export type HmrMessageSentToBrowser = TurbopackMessage | TurbopackConnectedMessage | BuildingMessage | SyncMessage | BuiltMessage | AddedPageMessage | RemovedPageMessage | ReloadPageMessage | ServerComponentChangesMessage | StaticParamsChangedMessage | ClientChangesMessage | MiddlewareChangesMessage | ServerOnlyChangesMessage | DevPagesManifestUpdateMessage | ServerErrorMessage | AppIsrManifestMessage | DevToolsConfigMessage | ErrorsToShowInBrowserMessage | ReactDebugChunkMessage | RequestCurrentErrorStateMessage | RuntimeErrorStateMessage | RequestPageMetadataMessage | CacheIndicatorMessage | RequestInsightsUpdateMessage | VulnerabilityInsightMessage;
export type BinaryHmrMessageSentToBrowser = Extract<HmrMessageSentToBrowser, {
type: number;
}>;
export type TurbopackMessageSentToBrowser = {
type: HMR_MESSAGE_SENT_TO_BROWSER.TURBOPACK_MESSAGE;
data: any;
hmrVersion: string;
} | {
type: HMR_MESSAGE_SENT_TO_BROWSER.TURBOPACK_CONNECTED;
data: {
sessionId: number;
hmrVersion: string;
};
} | {
type: HMR_MESSAGE_SENT_TO_BROWSER.SERVER_COMPONENT_CHANGES;
hmrVersion?: string;
};
export interface NextJsHotReloaderInterface {
turbopackProject?: Project;
activeWebpackConfigs?: Array<Awaited<ReturnType<typeof getBaseWebpackConfig>>>;
serverStats: webpack.Stats | null;
edgeServerStats: webpack.Stats | null;
run(req: IncomingMessage, res: ServerResponse, parsedUrl: UrlObject): Promise<{
finished?: true;
}>;
setHmrServerError(error: Error | null): void;
clearHmrServerError(): void;
start(): Promise<void>;
send(action: HmrMessageSentToBrowser): void;
/**
* Send the given action only to legacy clients, i.e. Pages Router clients,
* and App Router clients that don't have Cache Components enabled.
*/
sendToLegacyClients(action: HmrMessageSentToBrowser): void;
/**
* Identifies the current generation of the compiled server components. It is
* included in `"use cache"` cache keys so that cached entries are revalidated
* after an edit, for every client, regardless of whether it runs the HMR
* client. It is present from the first request on, so that entries created
* before the first edit are keyed by it too, and it differs between dev
* server runs, so that a cache handler that persists entries doesn't serve
* them for code that changed while the server was down.
*/
getServerComponentsHmrRefreshHash(): string;
setCacheStatus(status: ServerCacheStatus, htmlRequestId: string): void;
setReactDebugChannel(debugChannel: ReactDebugChannelForBrowser, htmlRequestId: string, requestId: string): void;
sendErrorsToBrowser(errorsRscStream: AnyStream, htmlRequestId: string): void;
getCompilationErrors(page: string): Promise<any[]>;
onHMR(req: IncomingMessage, _socket: Duplex, head: Buffer, onUpgrade: (client: {
send(data: string): void;
}, context: {
isLegacyClient: boolean;
}) => void): void;
/**
* Rebuilds so that a changed configuration reaches the bundles. Pass
* `reloadAfterInvalidation` only when the change can also affect what a
* render produces: it makes connected clients refetch server components, and
* advances `getServerComponentsHmrRefreshHash`, discarding `"use cache"`
* entries.
*/
invalidate({ reloadAfterInvalidation, }: {
reloadAfterInvalidation: boolean;
}): Promise<void> | void;
buildFallbackError(): Promise<void>;
ensurePage({ page, clientOnly, appPaths, definition, isApp, url, subscribeToChanges, }: {
page: string;
clientOnly: boolean;
appPaths?: ReadonlyArray<string> | null;
isApp?: boolean;
definition?: RouteDefinition;
url?: string;
/**
* Whether to wire HMR change subscriptions for the compiled entry.
* Defaults to true (the dev server uses these to push updates to
* connected browsers). Pass false for one-shot compilations (e.g.
* the `compile_route` MCP tool) where there is no client to receive
* HMR updates — without this, repeated calls leak subscriptions that
* keep firing on every file change for the life of the dev server.
*/
subscribeToChanges?: boolean;
}): Promise<void>;
close(): void;
}