From 3113aa5780c2c2a8cc5a4e01da07fe356188219d Mon Sep 17 00:00:00 2001 From: Jiachi Liu Date: Wed, 14 Feb 2024 01:09:27 +0100 Subject: [PATCH] separate server error source util to reduce bundle size --- .../internal/container/Errors.tsx | 2 +- .../internal/helpers/getErrorByType.ts | 2 +- .../internal/helpers/nodeStackFrames.ts | 23 ++++--------------- .../react-dev-overlay/server/middleware.ts | 7 +----- .../next/src/server/dev/next-dev-server.ts | 5 ++-- .../lib/router-utils/setup-dev-bundler.ts | 2 +- .../server/lib/server-ipc/request-utils.ts | 3 +-- packages/next/src/server/render.tsx | 4 +--- .../next/src/server/web/sandbox/context.ts | 7 +++--- packages/next/src/shared/lib/error-source.ts | 16 +++++++++++++ 10 files changed, 34 insertions(+), 37 deletions(-) create mode 100644 packages/next/src/shared/lib/error-source.ts diff --git a/packages/next/src/client/components/react-dev-overlay/internal/container/Errors.tsx b/packages/next/src/client/components/react-dev-overlay/internal/container/Errors.tsx index 858848be7501c6..58426aa606ba1c 100644 --- a/packages/next/src/client/components/react-dev-overlay/internal/container/Errors.tsx +++ b/packages/next/src/client/components/react-dev-overlay/internal/container/Errors.tsx @@ -18,12 +18,12 @@ import { Overlay } from '../components/Overlay' import { Toast } from '../components/Toast' import { getErrorByType } from '../helpers/getErrorByType' import type { ReadyRuntimeError } from '../helpers/getErrorByType' -import { getErrorSource } from '../helpers/nodeStackFrames' import { noop as css } from '../helpers/noop-template' import { CloseIcon } from '../icons/CloseIcon' import { RuntimeError } from './RuntimeError' import { VersionStalenessInfo } from '../components/VersionStalenessInfo' import type { VersionInfo } from '../../../../../server/dev/parse-version-info' +import { getErrorSource } from '../../../../../shared/lib/error-source' import { HotlinkedText } from '../components/hot-linked-text' export type SupportedErrorEvent = { diff --git a/packages/next/src/client/components/react-dev-overlay/internal/helpers/getErrorByType.ts b/packages/next/src/client/components/react-dev-overlay/internal/helpers/getErrorByType.ts index cfee460b76a5a2..0d72a1474f9848 100644 --- a/packages/next/src/client/components/react-dev-overlay/internal/helpers/getErrorByType.ts +++ b/packages/next/src/client/components/react-dev-overlay/internal/helpers/getErrorByType.ts @@ -3,10 +3,10 @@ import { ACTION_UNHANDLED_REJECTION, } from '../../app/error-overlay-reducer' import type { SupportedErrorEvent } from '../container/Errors' -import { getErrorSource } from './nodeStackFrames' import { getOriginalStackFrames } from './stack-frame' import type { OriginalStackFrame } from './stack-frame' import type { ComponentStackFrame } from './parse-component-stack' +import { getErrorSource } from '../../../../../shared/lib/error-source' export type ReadyRuntimeError = { id: number diff --git a/packages/next/src/client/components/react-dev-overlay/internal/helpers/nodeStackFrames.ts b/packages/next/src/client/components/react-dev-overlay/internal/helpers/nodeStackFrames.ts index a206cfdbb53031..1dc9bef8e1c548 100644 --- a/packages/next/src/client/components/react-dev-overlay/internal/helpers/nodeStackFrames.ts +++ b/packages/next/src/client/components/react-dev-overlay/internal/helpers/nodeStackFrames.ts @@ -1,5 +1,9 @@ import { parse } from 'next/dist/compiled/stacktrace-parser' import type { StackFrame } from 'next/dist/compiled/stacktrace-parser' +import { + decorateServerError, + type ErrorSourceType, +} from '../../../../../shared/lib/error-source' export function getFilesystemFrame(frame: StackFrame): StackFrame { const f: StackFrame = { ...frame } @@ -20,24 +24,7 @@ export function getFilesystemFrame(frame: StackFrame): StackFrame { return f } -const symbolError = Symbol.for('NextjsError') - -export function getErrorSource(error: Error): 'server' | 'edge-server' | null { - return (error as any)[symbolError] || null -} - -type ErrorType = 'edge-server' | 'server' - -export function decorateServerError(error: Error, type: ErrorType) { - Object.defineProperty(error, symbolError, { - writable: false, - enumerable: false, - configurable: false, - value: type, - }) -} - -export function getServerError(error: Error, type: ErrorType): Error { +export function getServerError(error: Error, type: ErrorSourceType): Error { let n: Error try { throw new Error(error.message) diff --git a/packages/next/src/client/components/react-dev-overlay/server/middleware.ts b/packages/next/src/client/components/react-dev-overlay/server/middleware.ts index 0af9416b0424c3..926f5d2f174ff1 100644 --- a/packages/next/src/client/components/react-dev-overlay/server/middleware.ts +++ b/packages/next/src/client/components/react-dev-overlay/server/middleware.ts @@ -8,12 +8,7 @@ import url from 'url' import type webpack from 'webpack' import { getRawSourceMap } from '../internal/helpers/getRawSourceMap' import { launchEditor } from '../internal/helpers/launchEditor' - -export { getErrorSource } from '../internal/helpers/nodeStackFrames' -export { - decorateServerError, - getServerError, -} from '../internal/helpers/nodeStackFrames' +export { getServerError } from '../internal/helpers/nodeStackFrames' export { parseStack } from '../internal/helpers/parseStack' export type OverlayMiddlewareOptions = { diff --git a/packages/next/src/server/dev/next-dev-server.ts b/packages/next/src/server/dev/next-dev-server.ts index c36f0c36dba142..f3a698ed207426 100644 --- a/packages/next/src/server/dev/next-dev-server.ts +++ b/packages/next/src/server/dev/next-dev-server.ts @@ -34,6 +34,7 @@ import { PHASE_DEVELOPMENT_SERVER, PAGES_MANIFEST, APP_PATHS_MANIFEST, + COMPILER_NAMES, } from '../../shared/lib/constants' import Server, { WrappedBuildError } from '../next-server' import { normalizePagePath } from '../../shared/lib/page-path/normalize-page-path' @@ -64,7 +65,7 @@ import { DetachedPromise } from '../../lib/detached-promise' import { isPostpone } from '../lib/router-utils/is-postpone' import { generateInterceptionRoutesRewrites } from '../../lib/generate-interception-routes-rewrites' import { buildCustomRoute } from '../../lib/build-custom-route' -import { decorateServerError } from '../../client/components/react-dev-overlay/server/middleware' +import { decorateServerError } from '../../shared/lib/error-source' // Load ReactDevOverlay only when needed let ReactDevOverlayImpl: FunctionComponent @@ -398,7 +399,7 @@ export default class DevServer extends Server { } const err = getProperError(error) - decorateServerError(err, 'edge-server') + decorateServerError(err, COMPILER_NAMES.edgeServer) const { request, response, parsedUrl } = params /** diff --git a/packages/next/src/server/lib/router-utils/setup-dev-bundler.ts b/packages/next/src/server/lib/router-utils/setup-dev-bundler.ts index 561cb83f231460..7b6423645fd089 100644 --- a/packages/next/src/server/lib/router-utils/setup-dev-bundler.ts +++ b/packages/next/src/server/lib/router-utils/setup-dev-bundler.ts @@ -61,7 +61,6 @@ import { } from '../../../build/utils' import { createOriginalStackFrame, - getErrorSource, getSourceById, parseStack, } from '../../../client/components/react-dev-overlay/server/middleware' @@ -71,6 +70,7 @@ import type { LazyRenderServerInstance } from '../router-server' import { HMR_ACTIONS_SENT_TO_BROWSER } from '../../dev/hot-reloader-types' import { PAGE_TYPES } from '../../../lib/page-types' import { createHotReloaderTurbopack } from '../../dev/hot-reloader-turbopack' +import { getErrorSource } from '../../../shared/lib/error-source' export type SetupOpts = { renderServer: LazyRenderServerInstance diff --git a/packages/next/src/server/lib/server-ipc/request-utils.ts b/packages/next/src/server/lib/server-ipc/request-utils.ts index 085786ad7a42be..3fd7295073d9c9 100644 --- a/packages/next/src/server/lib/server-ipc/request-utils.ts +++ b/packages/next/src/server/lib/server-ipc/request-utils.ts @@ -1,3 +1,4 @@ +import { decorateServerError } from '../../../shared/lib/error-source' import { PageNotFoundError } from '../../../shared/lib/utils' import { invokeRequest } from './invoke-request' @@ -24,8 +25,6 @@ export const deserializeErr = (serializedErr: any) => { process.env.NODE_ENV === 'development' && process.env.NEXT_RUNTIME !== 'edge' ) { - const { decorateServerError } = - require('../../../client/components/react-dev-overlay/server/middleware') as typeof import('../../../client/components/react-dev-overlay/server/middleware') decorateServerError(err, serializedErr.source || 'server') } return err diff --git a/packages/next/src/server/render.tsx b/packages/next/src/server/render.tsx index c824071be3aab3..2e09962082f9b1 100644 --- a/packages/next/src/server/render.tsx +++ b/packages/next/src/server/render.tsx @@ -105,6 +105,7 @@ import { getTracer } from './lib/trace/tracer' import { RenderSpan } from './lib/trace/constants' import { ReflectAdapter } from './web/spec-extension/adapters/reflect' import { formatRevalidate } from './lib/revalidate' +import { getErrorSource } from '../shared/lib/error-source' let tryGetPreviewData: typeof import('./api-utils/node/try-get-preview-data').tryGetPreviewData let warn: typeof import('../build/output/log').warn @@ -370,9 +371,6 @@ export function errorToJSON(err: Error) { 'server' if (process.env.NEXT_RUNTIME !== 'edge') { - const getErrorSource: typeof import('../client/components/react-dev-overlay/server/middleware').getErrorSource = - require('../client/components/react-dev-overlay/server/middleware').getErrorSource - source = getErrorSource(err) || 'server' } diff --git a/packages/next/src/server/web/sandbox/context.ts b/packages/next/src/server/web/sandbox/context.ts index fd9ca8c70310aa..4bbb7ad2a1423d 100644 --- a/packages/next/src/server/web/sandbox/context.ts +++ b/packages/next/src/server/web/sandbox/context.ts @@ -29,15 +29,16 @@ interface ModuleContext { } let getServerError: typeof import('../../../client/components/react-dev-overlay/server/middleware').getServerError -let decorateServerError: typeof import('../../../client/components/react-dev-overlay/server/middleware').decorateServerError +let decorateServerError: typeof import('../../../shared/lib/error-source').decorateServerError if (process.env.NODE_ENV === 'development') { const middleware = require('../../../client/components/react-dev-overlay/server/middleware') getServerError = middleware.getServerError - decorateServerError = middleware.decorateServerError + decorateServerError = + require('../../../shared/lib/error-source').decorateServerError } else { getServerError = (error: Error, _: string) => error - decorateServerError = (error: Error, _: string) => error + decorateServerError = (_: Error, __: string) => {} } /** diff --git a/packages/next/src/shared/lib/error-source.ts b/packages/next/src/shared/lib/error-source.ts new file mode 100644 index 00000000000000..a6663acb2f7501 --- /dev/null +++ b/packages/next/src/shared/lib/error-source.ts @@ -0,0 +1,16 @@ +const symbolError = Symbol.for('NextjsError') + +export function getErrorSource(error: Error): 'server' | 'edge-server' | null { + return (error as any)[symbolError] || null +} + +export type ErrorSourceType = 'edge-server' | 'server' + +export function decorateServerError(error: Error, type: ErrorSourceType) { + Object.defineProperty(error, symbolError, { + writable: false, + enumerable: false, + configurable: false, + value: type, + }) +}