Skip to content

Commit

Permalink
separate server error source util to reduce bundle size
Browse files Browse the repository at this point in the history
  • Loading branch information
huozhi authored and ForsakenHarmony committed Feb 14, 2024
1 parent df0c0a8 commit 093e27b
Show file tree
Hide file tree
Showing 10 changed files with 34 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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 }
Expand All @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down
5 changes: 3 additions & 2 deletions packages/next/src/server/dev/next-dev-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,6 @@ import {
} from '../../../build/utils'
import {
createOriginalStackFrame,
getErrorSource,
getSourceById,
parseStack,
} from '../../../client/components/react-dev-overlay/server/middleware'
Expand All @@ -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
Expand Down
3 changes: 1 addition & 2 deletions packages/next/src/server/lib/server-ipc/request-utils.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { decorateServerError } from '../../../shared/lib/error-source'
import { PageNotFoundError } from '../../../shared/lib/utils'
import { invokeRequest } from './invoke-request'

Expand All @@ -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
Expand Down
4 changes: 1 addition & 3 deletions packages/next/src/server/render.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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'
}

Expand Down
7 changes: 4 additions & 3 deletions packages/next/src/server/web/sandbox/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {}
}

/**
Expand Down
16 changes: 16 additions & 0 deletions packages/next/src/shared/lib/error-source.ts
Original file line number Diff line number Diff line change
@@ -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,
})
}

0 comments on commit 093e27b

Please sign in to comment.