next.js/packages/next/src/server/lib/router-utils/router-server-context.ts
router-server-context.ts65 lines2.3 KB
import type { IncomingMessage, ServerResponse } from 'node:http'
import type { NextConfigRuntime } from '../../config-shared'
import type { UrlWithParsedQuery } from 'node:url'
import type { ServerCacheStatus } from '../../../next-devtools/dev-overlay/cache-indicator'
import type { AnyStream } from '../../app-render/stream-ops'

export type RevalidateFn = (config: {
  urlPath: string
  headers: { [key: string]: string | string[] }
  opts: { unstable_onlyGenerated?: boolean }
}) => Promise<void>

// The RouterServerContext contains instance specific
// information that isn't available/relevant when
// deployed in serverless environments, the key is
// the relative project dir this allows separate contexts
// when running multiple next instances in same process
export type RouterServerContext = Record<
  string,
  {
    // hostname the server is started with
    hostname?: string
    // revalidate function to bypass going through network
    // to invoke revalidate request (uses mocked req/res)
    revalidate?: RevalidateFn
    // function to render the 404 page
    render404?: (
      req: IncomingMessage,
      res: ServerResponse,
      parsedUrl?: UrlWithParsedQuery,
      setHeaders?: boolean
    ) => Promise<void>
    // exposing nextConfig for dev mode specifically
    nextConfig?: NextConfigRuntime
    // whether running in custom server mode
    isCustomServer?: boolean
    // whether test proxy is enabled
    experimentalTestProxy?: boolean
    // allow dev server to log with original stack
    logErrorWithOriginalStack?: (err: unknown, type: string) => void
    // allow setting ISR status in dev
    setIsrStatus?: (key: string, value: boolean | undefined) => void
    setReactDebugChannel?: (
      debugChannel: { readable: AnyStream },
      htmlRequestId: string,
      requestId: string
    ) => void
    setCacheStatus?: (status: ServerCacheStatus, htmlRequestId: string) => void
    sendErrorsToBrowser?: (
      errorsRscStream: AnyStream,
      htmlRequestId: string
    ) => void
    // indicates request handlers are already wrapped by next-server tracing
    isWrappedByNextServer?: boolean
  }
>

export const RouterServerContextSymbol = Symbol.for(
  '@next/router-server-methods'
)

export const routerServerGlobal = globalThis as typeof globalThis & {
  [RouterServerContextSymbol]?: RouterServerContext
}
Quest for Codev2.0.0
/
SIGN IN