Unverified Commit 4e6c27be authored by JJ Kasper's avatar JJ Kasper Committed by GitHub

Migrate pages API routes to handler interface (#78166)

This is one of the intial migrations for
https://github.com/vercel/next.js/discussions/77740 which starts by
adding the `handler` signature for pages API routes. This new `handler`
signature will be added for all route types and allows a consistent way
to invoke entries.

This also aims to align all logic to no longer be special cased between
minimal mode, standalone mode, and just normal `next start`. All modes
will behave and process requests the same at the entry level after this
is done.

Once all route types are migrated `next-server`, `web-server`, and
`base-server` will no longer be necessary and a minimal matching layer
will be provided to process rewrites/dynamic routes to the related entry
instead.

The changes here we validated against the `vercel/vercel` [test suite
here](https://github.com/vercel/vercel/pull/13231) and

---------
Co-authored-by: 's avatarSebastian Sebbie Silbermann <sebastian.silbermann@vercel.com>
parent 4282bc6f
...@@ -673,5 +673,6 @@ ...@@ -673,5 +673,6 @@
"672": "Expected `telemetry` to be set in globals", "672": "Expected `telemetry` to be set in globals",
"673": "Thrown value was ignored. This is a bug in Next.js.", "673": "Thrown value was ignored. This is a bug in Next.js.",
"674": "Route \"%s\" has a \\`generateViewport\\` that depends on Request data (\\`cookies()\\`, etc...) or uncached external data (\\`fetch(...)\\`, etc...) without explicitly allowing fully dynamic rendering. See more info here: https://nextjs.org/docs/messages/next-prerender-dynamic-viewport", "674": "Route \"%s\" has a \\`generateViewport\\` that depends on Request data (\\`cookies()\\`, etc...) or uncached external data (\\`fetch(...)\\`, etc...) without explicitly allowing fully dynamic rendering. See more info here: https://nextjs.org/docs/messages/next-prerender-dynamic-viewport",
"675": "Expected `generateMetadata` not to block the application shell but it did." "675": "Expected `generateMetadata` not to block the application shell but it did.",
"676": "Invariant: missing internal router-server-methods this is an internal bug"
} }
import { PagesAPIRouteModule } from '../../server/route-modules/pages-api/module.compiled' import type { NextApiResponse } from '../../types'
import type { IncomingMessage, ServerResponse } from 'node:http'
import { RouteKind } from '../../server/route-kind' import { RouteKind } from '../../server/route-kind'
import { sendError } from '../../server/api-utils'
import { PagesAPIRouteModule } from '../../server/route-modules/pages-api/module.compiled'
import fs from 'node:fs'
import path from 'node:path'
import { parse } from 'node:url'
import { hoist } from './helpers' import { hoist } from './helpers'
// Import the userland code. // Import the userland code.
import * as userland from 'VAR_USERLAND' import * as userland from 'VAR_USERLAND'
import { getTracer, SpanKind } from '../../server/lib/trace/tracer'
import type { Span } from '../../server/lib/trace/tracer'
import { BaseServerSpan } from '../../server/lib/trace/constants'
import {
ensureInstrumentationRegistered,
instrumentationOnRequestError,
} from '../../server/lib/router-utils/instrumentation-globals.external'
import type { InstrumentationOnRequestError } from '../../server/instrumentation/types'
import { getUtils } from '../../server/server-utils'
import { PRERENDER_MANIFEST, ROUTES_MANIFEST } from '../../api/constants'
import { isDynamicRoute } from '../../shared/lib/router/utils'
import type { BaseNextRequest } from '../../server/base-http'
import {
RouterServerContextSymbol,
routerServerGlobal,
} from '../../server/lib/router-utils/router-server-context'
import { removePathPrefix } from '../../shared/lib/router/utils/remove-path-prefix'
import { normalizeLocalePath } from '../../shared/lib/i18n/normalize-locale-path'
import type { PrerenderManifest, RoutesManifest } from '..'
// Re-export the handler (should be the default export). // Re-export the handler (should be the default export).
export default hoist(userland, 'default') export default hoist(userland, 'default')
...@@ -13,7 +39,7 @@ export default hoist(userland, 'default') ...@@ -13,7 +39,7 @@ export default hoist(userland, 'default')
export const config = hoist(userland, 'config') export const config = hoist(userland, 'config')
// Create and export the route module that will be consumed. // Create and export the route module that will be consumed.
export const routeModule = new PagesAPIRouteModule({ const routeModule = new PagesAPIRouteModule({
definition: { definition: {
kind: RouteKind.PAGES_API, kind: RouteKind.PAGES_API,
page: 'VAR_DEFINITION_PAGE', page: 'VAR_DEFINITION_PAGE',
...@@ -24,3 +50,230 @@ export const routeModule = new PagesAPIRouteModule({ ...@@ -24,3 +50,230 @@ export const routeModule = new PagesAPIRouteModule({
}, },
userland, userland,
}) })
const loadedManifests = new Map<string, any>()
async function loadManifest(key: string, loader: () => Promise<any>) {
const cached = loadedManifests.get(key)
if (cached) {
return cached
}
const currentManifest = await loader()
loadedManifests.set(key, currentManifest)
return currentManifest
}
export async function handler(
req: IncomingMessage,
res: ServerResponse,
ctx: {
waitUntil?: (prom: Promise<void>) => void
}
): Promise<void> {
const dir =
routerServerGlobal[RouterServerContextSymbol]?.dir || process.cwd()
const distDir = process.env.__NEXT_RELATIVE_DIST_DIR || ''
const isDev = process.env.NODE_ENV === 'development'
const routesManifest: RoutesManifest = await loadManifest(
ROUTES_MANIFEST,
async () =>
JSON.parse(
await fs.promises.readFile(
path.join(dir, distDir, ROUTES_MANIFEST),
'utf8'
)
)
)
const prerenderManifest: PrerenderManifest = await loadManifest(
PRERENDER_MANIFEST,
async () =>
JSON.parse(
await fs.promises.readFile(
path.join(dir, distDir, PRERENDER_MANIFEST),
'utf8'
)
)
)
let srcPage = 'VAR_DEFINITION_PAGE'
// turbopack doesn't normalize `/index` in the page name
// so we need to to process dynamic routes properly
if (process.env.TURBOPACK) {
srcPage = srcPage.replace(/\/index$/, '')
}
// We need to parse dynamic route params
// and do URL normalization here.
// TODO: move this into server-utils for re-use
const { basePath, i18n, rewrites } = routesManifest
if (basePath) {
req.url = removePathPrefix(req.url || '/', basePath)
}
if (i18n) {
const urlParts = (req.url || '/').split('?')
const localeResult = normalizeLocalePath(urlParts[0] || '/', i18n.locales)
if (localeResult.detectedLocale) {
req.url = `${localeResult.pathname}${
urlParts[1] ? `?${urlParts[1]}` : ''
}`
}
}
const parsedUrl = parse(req.url || '/', true)
const pageIsDynamic = isDynamicRoute(srcPage)
const serverUtils = getUtils({
page: srcPage,
i18n,
basePath,
rewrites: Array.isArray(rewrites)
? { beforeFiles: [], afterFiles: rewrites, fallback: [] }
: rewrites || {
beforeFiles: [],
afterFiles: [],
fallback: [],
},
pageIsDynamic,
trailingSlash: process.env.__NEXT_TRAILING_SLASH as any as boolean,
caseSensitive: Boolean(routesManifest.caseSensitive),
})
const rewriteParamKeys = Object.keys(
serverUtils.handleRewrites(req as any as BaseNextRequest, parsedUrl)
)
serverUtils.normalizeCdnUrl(req as any as BaseNextRequest, [
...rewriteParamKeys,
...Object.keys(serverUtils.defaultRouteRegex?.groups || {}),
])
const params: Record<string, undefined | string | string[]> =
serverUtils.dynamicRouteMatcher
? serverUtils.dynamicRouteMatcher(parsedUrl.pathname || '') || {}
: {}
const query = {
...parsedUrl.query,
...params,
}
serverUtils.normalizeQueryParams(query)
if (pageIsDynamic) {
const result = serverUtils.normalizeDynamicRouteParams(query, true)
if (result.hasValidParams) {
Object.assign(query, result.params)
}
}
// ensure instrumentation is registered and pass
// onRequestError below
const absoluteDistDir = path.join(dir, distDir)
await ensureInstrumentationRegistered(absoluteDistDir)
try {
const method = req.method || 'GET'
const tracer = getTracer()
const activeSpan = tracer.getActiveScopeSpan()
const invokeRouteModule = async (span?: Span) => {
await routeModule
.render(req, res, {
query,
params,
allowedRevalidateHeaderKeys: process.env
.__NEXT_ALLOWED_REVALIDATE_HEADERS as any as string[],
multiZoneDraftMode: Boolean(process.env.__NEXT_MULTI_ZONE_DRAFT_MODE),
trustHostHeader: process.env
.__NEXT_TRUST_HOST_HEADER as any as boolean,
// TODO: get this from from runtime env so manifest
// doesn't need to load
previewProps: prerenderManifest.preview,
propagateError: false,
dev: isDev,
page: 'VAR_DEFINITION_PAGE',
onError: (...args: Parameters<InstrumentationOnRequestError>) =>
instrumentationOnRequestError(absoluteDistDir, ...args),
})
.finally(() => {
if (!span) return
span.setAttributes({
'http.status_code': res.statusCode,
'next.rsc': false,
})
const rootSpanAttributes = tracer.getRootSpanAttributes()
// We were unable to get attributes, probably OTEL is not enabled
if (!rootSpanAttributes) {
return
}
if (
rootSpanAttributes.get('next.span_type') !==
BaseServerSpan.handleRequest
) {
console.warn(
`Unexpected root span type '${rootSpanAttributes.get(
'next.span_type'
)}'. Please report this Next.js issue https://github.com/vercel/next.js`
)
return
}
const route = rootSpanAttributes.get('next.route')
if (route) {
const name = `${method} ${route}`
span.setAttributes({
'next.route': route,
'http.route': route,
'next.span_name': name,
})
span.updateName(name)
} else {
span.updateName(`${method} ${req.url}`)
}
})
}
// TODO: activeSpan code path is for when wrapped by
// next-server can be removed when this is no longer used
if (activeSpan) {
await invokeRouteModule(activeSpan)
} else {
await tracer.withPropagatedContext(req.headers, () =>
tracer.trace(
BaseServerSpan.handleRequest,
{
spanName: `${method} ${req.url}`,
kind: SpanKind.SERVER,
attributes: {
'http.method': method,
'http.target': req.url,
},
},
invokeRouteModule
)
)
}
} catch (err) {
// we re-throw in dev to show the error overlay
if (isDev) {
throw err
}
// this is technically an invariant as error handling
// should be done inside of api-resolver onError
sendError(res as NextApiResponse, 500, 'Internal Server Error')
} finally {
// We don't allow any waitUntil work in pages API routes currently
// so if callback is present return with resolved promise since no
// pending work
ctx.waitUntil?.(Promise.resolve())
}
}
...@@ -120,7 +120,7 @@ export async function webpackBuildImpl( ...@@ -120,7 +120,7 @@ export async function webpackBuildImpl(
reactProductionProfiling: NextBuildContext.reactProductionProfiling!, reactProductionProfiling: NextBuildContext.reactProductionProfiling!,
noMangling: NextBuildContext.noMangling!, noMangling: NextBuildContext.noMangling!,
clientRouterFilters: NextBuildContext.clientRouterFilters!, clientRouterFilters: NextBuildContext.clientRouterFilters!,
previewModeId: NextBuildContext.previewModeId!, previewProps: NextBuildContext.previewProps!,
allowedRevalidateHeaderKeys: NextBuildContext.allowedRevalidateHeaderKeys!, allowedRevalidateHeaderKeys: NextBuildContext.allowedRevalidateHeaderKeys!,
fetchCacheKeyPrefix: NextBuildContext.fetchCacheKeyPrefix!, fetchCacheKeyPrefix: NextBuildContext.fetchCacheKeyPrefix!,
} }
...@@ -156,14 +156,6 @@ export async function webpackBuildImpl( ...@@ -156,14 +156,6 @@ export async function webpackBuildImpl(
middlewareMatchers: entrypoints.middlewareMatchers, middlewareMatchers: entrypoints.middlewareMatchers,
compilerType: COMPILER_NAMES.edgeServer, compilerType: COMPILER_NAMES.edgeServer,
entrypoints: entrypoints.edgeServer, entrypoints: entrypoints.edgeServer,
edgePreviewProps: {
__NEXT_PREVIEW_MODE_ID:
NextBuildContext.previewProps!.previewModeId,
__NEXT_PREVIEW_MODE_ENCRYPTION_KEY:
NextBuildContext.previewProps!.previewModeEncryptionKey,
__NEXT_PREVIEW_MODE_SIGNING_KEY:
NextBuildContext.previewProps!.previewModeSigningKey,
},
...info, ...info,
}), }),
]) ])
......
...@@ -96,6 +96,7 @@ import { ...@@ -96,6 +96,7 @@ import {
import { getRspackCore, getRspackReactRefresh } from '../shared/lib/get-rspack' import { getRspackCore, getRspackReactRefresh } from '../shared/lib/get-rspack'
import { RspackProfilingPlugin } from './webpack/plugins/rspack-profiling-plugin' import { RspackProfilingPlugin } from './webpack/plugins/rspack-profiling-plugin'
import getWebpackBundler from '../shared/lib/get-webpack-bundler' import getWebpackBundler from '../shared/lib/get-webpack-bundler'
import type { NextBuildContext } from './build-context'
type ExcludesFalse = <T>(x: T | false) => x is T type ExcludesFalse = <T>(x: T | false) => x is T
type ClientEntries = { type ClientEntries = {
...@@ -312,9 +313,10 @@ export default async function getBaseWebpackConfig( ...@@ -312,9 +313,10 @@ export default async function getBaseWebpackConfig(
supportedBrowsers, supportedBrowsers,
clientRouterFilters, clientRouterFilters,
fetchCacheKeyPrefix, fetchCacheKeyPrefix,
edgePreviewProps,
isCompileMode, isCompileMode,
previewProps,
}: { }: {
previewProps: NonNullable<(typeof NextBuildContext)['previewProps']>
isCompileMode?: boolean isCompileMode?: boolean
buildId: string buildId: string
encryptionKey: string encryptionKey: string
...@@ -336,7 +338,6 @@ export default async function getBaseWebpackConfig( ...@@ -336,7 +338,6 @@ export default async function getBaseWebpackConfig(
jsConfigPath?: string jsConfigPath?: string
resolvedBaseUrl: ResolvedBaseUrl resolvedBaseUrl: ResolvedBaseUrl
supportedBrowsers: string[] | undefined supportedBrowsers: string[] | undefined
edgePreviewProps?: Record<string, string>
clientRouterFilters?: { clientRouterFilters?: {
staticFilter: ReturnType< staticFilter: ReturnType<
import('../shared/lib/bloom-filter').BloomFilter['export'] import('../shared/lib/bloom-filter').BloomFilter['export']
...@@ -2010,7 +2011,10 @@ export default async function getBaseWebpackConfig( ...@@ -2010,7 +2011,10 @@ export default async function getBaseWebpackConfig(
edgeEnvironments: { edgeEnvironments: {
__NEXT_BUILD_ID: buildId, __NEXT_BUILD_ID: buildId,
NEXT_SERVER_ACTIONS_ENCRYPTION_KEY: encryptionKey, NEXT_SERVER_ACTIONS_ENCRYPTION_KEY: encryptionKey,
...edgePreviewProps, __NEXT_PREVIEW_MODE_ID: previewProps.previewModeId,
__NEXT_PREVIEW_MODE_SIGNING_KEY: previewProps.previewModeSigningKey,
__NEXT_PREVIEW_MODE_ENCRYPTION_KEY:
previewProps.previewModeEncryptionKey,
}, },
}), }),
isClient && isClient &&
......
...@@ -265,6 +265,21 @@ export function getDefineEnv({ ...@@ -265,6 +265,21 @@ export function getDefineEnv({
needsExperimentalReact(config), needsExperimentalReact(config),
} }
: undefined), : undefined),
'process.env.__NEXT_MULTI_ZONE_DRAFT_MODE': JSON.stringify(
config.experimental.multiZoneDraftMode
),
'process.env.__NEXT_TRUST_HOST_HEADER': JSON.stringify(
config.experimental.trustHostHeader
),
'process.env.__NEXT_ALLOWED_REVALIDATE_HEADERS': JSON.stringify(
config.experimental.allowedRevalidateHeaderKeys
),
...(isNodeServer
? {
'process.env.__NEXT_RELATIVE_DIST_DIR': config.distDir,
}
: {}),
} }
const userDefines = config.compiler?.define ?? {} const userDefines = config.compiler?.define ?? {}
......
...@@ -3,7 +3,6 @@ import type { NextApiRequest, NextApiResponse } from '../../../shared/lib/utils' ...@@ -3,7 +3,6 @@ import type { NextApiRequest, NextApiResponse } from '../../../shared/lib/utils'
import type { PageConfig, ResponseLimit } from '../../../types' import type { PageConfig, ResponseLimit } from '../../../types'
import type { __ApiPreviewProps } from '../.' import type { __ApiPreviewProps } from '../.'
import type { CookieSerializeOptions } from 'next/dist/compiled/cookie' import type { CookieSerializeOptions } from 'next/dist/compiled/cookie'
import type { ServerOnInstrumentationRequestError } from '../../app-render/types'
import bytes from 'next/dist/compiled/bytes' import bytes from 'next/dist/compiled/bytes'
import { generateETag } from '../../lib/etag' import { generateETag } from '../../lib/etag'
...@@ -30,18 +29,16 @@ import { ...@@ -30,18 +29,16 @@ import {
} from '../../../lib/constants' } from '../../../lib/constants'
import { tryGetPreviewData } from './try-get-preview-data' import { tryGetPreviewData } from './try-get-preview-data'
import { parseBody } from './parse-body' import { parseBody } from './parse-body'
import {
type RevalidateFn = (config: { RouterServerContextSymbol,
urlPath: string routerServerGlobal,
revalidateHeaders: { [key: string]: string | string[] } } from '../../lib/router-utils/router-server-context'
opts: { unstable_onlyGenerated?: boolean } import type { InstrumentationOnRequestError } from '../../instrumentation/types'
}) => Promise<void>
type ApiContext = __ApiPreviewProps & { type ApiContext = __ApiPreviewProps & {
trustHostHeader?: boolean trustHostHeader?: boolean
allowedRevalidateHeaderKeys?: string[] allowedRevalidateHeaderKeys?: string[]
hostname?: string hostname?: string
revalidate?: RevalidateFn
multiZoneDraftMode?: boolean multiZoneDraftMode?: boolean
dev: boolean dev: boolean
} }
...@@ -288,7 +285,21 @@ async function revalidate( ...@@ -288,7 +285,21 @@ async function revalidate(
} }
} }
const internalRevalidate =
routerServerGlobal[RouterServerContextSymbol]?.revalidate
try { try {
// We use the revalidate in router-server if available.
// If we are operating without router-server (serverless)
// we must go through network layer with fetch request
if (internalRevalidate) {
return await internalRevalidate({
urlPath,
revalidateHeaders,
opts,
})
}
if (context.trustHostHeader) { if (context.trustHostHeader) {
const res = await fetch(`https://${req.headers.host}${urlPath}`, { const res = await fetch(`https://${req.headers.host}${urlPath}`, {
method: 'HEAD', method: 'HEAD',
...@@ -307,15 +318,9 @@ async function revalidate( ...@@ -307,15 +318,9 @@ async function revalidate(
) { ) {
throw new Error(`Invalid response ${res.status}`) throw new Error(`Invalid response ${res.status}`)
} }
} else if (context.revalidate) {
await context.revalidate({
urlPath,
revalidateHeaders,
opts,
})
} else { } else {
throw new Error( throw new Error(
`Invariant: required internal revalidate method not passed to api-utils` `Invariant: missing internal router-server-methods this is an internal bug`
) )
} }
} catch (err: unknown) { } catch (err: unknown) {
...@@ -334,7 +339,7 @@ export async function apiResolver( ...@@ -334,7 +339,7 @@ export async function apiResolver(
propagateError: boolean, propagateError: boolean,
dev?: boolean, dev?: boolean,
page?: string, page?: string,
onError?: ServerOnInstrumentationRequestError onError?: InstrumentationOnRequestError
): Promise<void> { ): Promise<void> {
const apiReq = req as NextApiRequest const apiReq = req as NextApiRequest
const apiRes = res as NextApiResponse const apiRes = res as NextApiResponse
...@@ -445,12 +450,20 @@ export async function apiResolver( ...@@ -445,12 +450,20 @@ export async function apiResolver(
} }
} }
} catch (err) { } catch (err) {
onError?.(err, req, { await onError?.(
err,
{
method: req.method || 'GET',
headers: req.headers,
path: req.url || '/',
},
{
routerKind: 'Pages Router', routerKind: 'Pages Router',
routePath: page || '', routePath: page || '',
routeType: 'route', routeType: 'route',
revalidateReason: undefined, revalidateReason: undefined,
}) }
)
if (err instanceof ApiError) { if (err instanceof ApiError) {
sendError(apiRes, err.statusCode, err.message) sendError(apiRes, err.statusCode, err.message)
......
...@@ -1389,7 +1389,7 @@ export default abstract class Server< ...@@ -1389,7 +1389,7 @@ export default abstract class Server<
} }
if (pageIsDynamic || didRewrite) { if (pageIsDynamic || didRewrite) {
utils.normalizeVercelUrl(req, [ utils.normalizeCdnUrl(req, [
...rewriteParamKeys, ...rewriteParamKeys,
...Object.keys(utils.defaultRouteRegex?.groups || {}), ...Object.keys(utils.defaultRouteRegex?.groups || {}),
]) ])
......
...@@ -3,6 +3,7 @@ import { basename, extname, join, relative, isAbsolute, resolve } from 'path' ...@@ -3,6 +3,7 @@ import { basename, extname, join, relative, isAbsolute, resolve } from 'path'
import { pathToFileURL } from 'url' import { pathToFileURL } from 'url'
import findUp from 'next/dist/compiled/find-up' import findUp from 'next/dist/compiled/find-up'
import * as Log from '../build/output/log' import * as Log from '../build/output/log'
import * as ciEnvironment from '../server/ci-info'
import { CONFIG_FILES, PHASE_DEVELOPMENT_SERVER } from '../shared/lib/constants' import { CONFIG_FILES, PHASE_DEVELOPMENT_SERVER } from '../shared/lib/constants'
import { defaultConfig, normalizeConfig } from './config-shared' import { defaultConfig, normalizeConfig } from './config-shared'
import type { import type {
...@@ -234,7 +235,19 @@ function assignDefaults( ...@@ -234,7 +235,19 @@ function assignDefaults(
) )
} }
const result = { ...defaultConfig, ...config } const result = {
...defaultConfig,
...config,
experimental: {
...defaultConfig.experimental,
...config.experimental,
},
}
// ensure correct default is set for api-resolver revalidate handling
if (!result.experimental?.trustHostHeader && ciEnvironment.hasNextSupport) {
result.experimental.trustHostHeader = true
}
if ( if (
result.experimental?.allowDevelopmentBuild && result.experimental?.allowDevelopmentBuild &&
...@@ -976,9 +989,6 @@ function assignDefaults( ...@@ -976,9 +989,6 @@ function assignDefaults(
const userProvidedOptimizePackageImports = const userProvidedOptimizePackageImports =
result.experimental?.optimizePackageImports || [] result.experimental?.optimizePackageImports || []
if (!result.experimental) {
result.experimental = {}
}
result.experimental.optimizePackageImports = [ result.experimental.optimizePackageImports = [
...new Set([ ...new Set([
......
...@@ -653,6 +653,7 @@ export default class HotReloaderWebpack implements NextJsHotReloaderInterface { ...@@ -653,6 +653,7 @@ export default class HotReloaderWebpack implements NextJsHotReloaderInterface {
originalRedirects: this.config._originalRedirects, originalRedirects: this.config._originalRedirects,
runWebpackSpan: this.hotReloaderSpan, runWebpackSpan: this.hotReloaderSpan,
appDir: this.appDir, appDir: this.appDir,
previewProps: this.previewProps,
} }
return webpackConfigSpan return webpackConfigSpan
...@@ -697,6 +698,7 @@ export default class HotReloaderWebpack implements NextJsHotReloaderInterface { ...@@ -697,6 +698,7 @@ export default class HotReloaderWebpack implements NextJsHotReloaderInterface {
dev: true, dev: true,
}) })
const fallbackConfig = await getBaseWebpackConfig(this.dir, { const fallbackConfig = await getBaseWebpackConfig(this.dir, {
previewProps: this.previewProps,
runWebpackSpan: this.hotReloaderSpan, runWebpackSpan: this.hotReloaderSpan,
dev: true, dev: true,
compilerType: COMPILER_NAMES.client, compilerType: COMPILER_NAMES.client,
......
...@@ -51,6 +51,10 @@ import type { ServerInitResult } from './render-server' ...@@ -51,6 +51,10 @@ import type { ServerInitResult } from './render-server'
import { filterInternalHeaders } from './server-ipc/utils' import { filterInternalHeaders } from './server-ipc/utils'
import { blockCrossSite } from './router-utils/block-cross-site' import { blockCrossSite } from './router-utils/block-cross-site'
import { traceGlobals } from '../../trace/shared' import { traceGlobals } from '../../trace/shared'
import {
RouterServerContextSymbol,
routerServerGlobal,
} from './router-utils/router-server-context'
const debug = setupDebug('next:router-server:main') const debug = setupDebug('next:router-server:main')
const isNextFont = (pathname: string | null) => const isNextFont = (pathname: string | null) =>
...@@ -646,6 +650,14 @@ export async function initialize(opts: { ...@@ -646,6 +650,14 @@ export async function initialize(opts: {
// pre-initialize workers // pre-initialize workers
const handlers = await renderServer.instance.initialize(renderServerOpts) const handlers = await renderServer.instance.initialize(renderServerOpts)
// this must come after initialize of render server since it's
// using initialized methods
routerServerGlobal[RouterServerContextSymbol] = {
dir: opts.dir,
hostname: handlers.server.hostname,
revalidate: handlers.server.revalidate.bind(handlers.server),
}
const logError = async ( const logError = async (
type: 'uncaughtException' | 'unhandledRejection', type: 'uncaughtException' | 'unhandledRejection',
err: Error | undefined err: Error | undefined
......
...@@ -73,24 +73,30 @@ export const buildCustomRoute = <T>( ...@@ -73,24 +73,30 @@ export const buildCustomRoute = <T>(
item: T & { source: string }, item: T & { source: string },
basePath?: string, basePath?: string,
caseSensitive?: boolean caseSensitive?: boolean
): T & { match: PatchMatcher; check?: boolean } => { ): T & { match: PatchMatcher; check?: boolean; regex: string } => {
const restrictedRedirectPaths = ['/_next'].map((p) => const restrictedRedirectPaths = ['/_next'].map((p) =>
basePath ? `${basePath}${p}` : p basePath ? `${basePath}${p}` : p
) )
let builtRegex = ''
const match = getPathMatch(item.source, { const match = getPathMatch(item.source, {
strict: true, strict: true,
removeUnnamedParams: true, removeUnnamedParams: true,
regexModifier: !(item as any).internal regexModifier: (regex: string) => {
? (regex: string) => if (!(item as any).internal) {
modifyRouteRegex( regex = modifyRouteRegex(
regex, regex,
type === 'redirect' ? restrictedRedirectPaths : undefined type === 'redirect' ? restrictedRedirectPaths : undefined
) )
: undefined, }
builtRegex = regex
return builtRegex
},
sensitive: caseSensitive, sensitive: caseSensitive,
}) })
return { return {
...item, ...item,
regex: builtRegex,
...(type === 'rewrite' ? { check: true } : {}), ...(type === 'rewrite' ? { check: true } : {}),
match, match,
} }
......
import path from 'node:path'
import isError from '../../../lib/is-error'
import { INSTRUMENTATION_HOOK_FILENAME } from '../../../lib/constants'
import type {
InstrumentationModule,
InstrumentationOnRequestError,
} from '../../instrumentation/types'
import { interopDefault } from '../../../lib/interop-default'
let cachedInstrumentationModule: InstrumentationModule
export async function getInstrumentationModule(
distDir: string
): Promise<InstrumentationModule | undefined> {
if (cachedInstrumentationModule && process.env.NODE_ENV === 'production') {
return cachedInstrumentationModule
}
try {
cachedInstrumentationModule = interopDefault(
await require(
path.join(distDir, 'server', `${INSTRUMENTATION_HOOK_FILENAME}.js`)
)
)
return cachedInstrumentationModule
} catch (err: unknown) {
if (
isError(err) &&
err.code !== 'ENOENT' &&
err.code !== 'MODULE_NOT_FOUND' &&
err.code !== 'ERR_MODULE_NOT_FOUND'
) {
throw err
}
}
}
let instrumentationModulePromise: Promise<any> | null = null
async function registerInstrumentation(distDir: string) {
// Ensure registerInstrumentation is not called in production build
if (process.env.NEXT_PHASE === 'phase-production-build') return
if (!instrumentationModulePromise) {
instrumentationModulePromise = getInstrumentationModule(distDir)
}
const instrumentation = await instrumentationModulePromise
if (instrumentation?.register) {
try {
await instrumentation.register()
} catch (err: any) {
err.message = `An error occurred while loading instrumentation hook: ${err.message}`
throw err
}
}
}
export async function instrumentationOnRequestError(
distDir: string,
...args: Parameters<InstrumentationOnRequestError>
) {
const instrumentation = await getInstrumentationModule(distDir)
try {
await instrumentation?.onRequestError?.(...args)
} catch (err) {
// Log the soft error and continue, since the original error has already been thrown
console.error('Error in instrumentation.onRequestError:', err)
}
}
let registerInstrumentationPromise: Promise<void> | null = null
export function ensureInstrumentationRegistered(distDir: string) {
if (!registerInstrumentationPromise) {
registerInstrumentationPromise = registerInstrumentation(distDir)
}
return registerInstrumentationPromise
}
export type RevalidateFn = (config: {
urlPath: string
revalidateHeaders: { [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
export interface RouterServerContext {
dir?: 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
}
export const RouterServerContextSymbol = Symbol.for(
'@next/router-server-methods'
)
export const routerServerGlobal = globalThis as typeof globalThis & {
[RouterServerContextSymbol]?: RouterServerContext
}
...@@ -5,6 +5,7 @@ import { ...@@ -5,6 +5,7 @@ import {
getPageStaticInfo, getPageStaticInfo,
type MiddlewareMatcher, type MiddlewareMatcher,
} from '../../../build/analysis/get-page-static-info' } from '../../../build/analysis/get-page-static-info'
import type { RoutesManifest } from '../../../build'
import type { MiddlewareRouteMatch } from '../../../shared/lib/router/utils/middleware-route-matcher' import type { MiddlewareRouteMatch } from '../../../shared/lib/router/utils/middleware-route-matcher'
import type { PropagateToWorkersField } from './types' import type { PropagateToWorkersField } from './types'
import type { NextJsHotReloaderInterface } from '../../dev/hot-reloader-types' import type { NextJsHotReloaderInterface } from '../../dev/hot-reloader-types'
...@@ -53,6 +54,8 @@ import { ...@@ -53,6 +54,8 @@ import {
DEV_CLIENT_MIDDLEWARE_MANIFEST, DEV_CLIENT_MIDDLEWARE_MANIFEST,
PHASE_DEVELOPMENT_SERVER, PHASE_DEVELOPMENT_SERVER,
TURBOPACK_CLIENT_MIDDLEWARE_MANIFEST, TURBOPACK_CLIENT_MIDDLEWARE_MANIFEST,
ROUTES_MANIFEST,
PRERENDER_MANIFEST,
} from '../../../shared/lib/constants' } from '../../../shared/lib/constants'
import { getMiddlewareRouteMatcher } from '../../../shared/lib/router/utils/middleware-route-matcher' import { getMiddlewareRouteMatcher } from '../../../shared/lib/router/utils/middleware-route-matcher'
...@@ -194,6 +197,31 @@ async function startWatcher(opts: SetupOpts) { ...@@ -194,6 +197,31 @@ async function startWatcher(opts: SetupOpts) {
await hotReloader.start() await hotReloader.start()
// have to write this after starting hot-reloader since that
// cleans the dist dir
const routesManifestPath = path.join(distDir, ROUTES_MANIFEST)
const routesManifest: Partial<RoutesManifest> = {
version: 3,
caseSensitive: !!nextConfig.experimental.caseSensitiveRoutes,
basePath: nextConfig.basePath,
rewrites: opts.fsChecker.rewrites,
redirects: opts.fsChecker.redirects,
headers: opts.fsChecker.headers,
dataRoutes: [],
i18n: nextConfig.i18n || undefined,
skipMiddlewareUrlNormalize: nextConfig.skipMiddlewareUrlNormalize,
}
await fs.promises.writeFile(
routesManifestPath,
JSON.stringify(routesManifest)
)
const prerenderManifestPath = path.join(distDir, PRERENDER_MANIFEST)
await fs.promises.writeFile(
prerenderManifestPath,
JSON.stringify(opts.fsChecker.prerenderManifest, null, 2)
)
if (opts.nextConfig.experimental.nextScriptWorkers) { if (opts.nextConfig.experimental.nextScriptWorkers) {
await verifyPartytownSetup( await verifyPartytownSetup(
opts.dir, opts.dir,
......
...@@ -18,7 +18,6 @@ import type { Params } from './request/params' ...@@ -18,7 +18,6 @@ import type { Params } from './request/params'
import type { MiddlewareRouteMatch } from '../shared/lib/router/utils/middleware-route-matcher' import type { MiddlewareRouteMatch } from '../shared/lib/router/utils/middleware-route-matcher'
import type { RouteMatch } from './route-matches/route-match' import type { RouteMatch } from './route-matches/route-match'
import type { IncomingMessage, ServerResponse } from 'http' import type { IncomingMessage, ServerResponse } from 'http'
import type { PagesAPIRouteModule } from './route-modules/pages-api/module'
import type { UrlWithParsedQuery } from 'url' import type { UrlWithParsedQuery } from 'url'
import type { ParsedUrlQuery } from 'querystring' import type { ParsedUrlQuery } from 'querystring'
import type { ParsedUrl } from '../shared/lib/router/utils/parse-url' import type { ParsedUrl } from '../shared/lib/router/utils/parse-url'
...@@ -39,7 +38,6 @@ import { ...@@ -39,7 +38,6 @@ import {
APP_PATHS_MANIFEST, APP_PATHS_MANIFEST,
SERVER_DIRECTORY, SERVER_DIRECTORY,
NEXT_FONT_MANIFEST, NEXT_FONT_MANIFEST,
PHASE_PRODUCTION_BUILD,
UNDERSCORE_NOT_FOUND_ROUTE_ENTRY, UNDERSCORE_NOT_FOUND_ROUTE_ENTRY,
FUNCTIONS_CONFIG_MANIFEST, FUNCTIONS_CONFIG_MANIFEST,
} from '../shared/lib/constants' } from '../shared/lib/constants'
...@@ -96,7 +94,6 @@ import { pipeToNodeResponse } from './pipe-readable' ...@@ -96,7 +94,6 @@ import { pipeToNodeResponse } from './pipe-readable'
import { createRequestResponseMocks } from './lib/mock-request' import { createRequestResponseMocks } from './lib/mock-request'
import { NEXT_RSC_UNION_QUERY } from '../client/components/app-router-headers' import { NEXT_RSC_UNION_QUERY } from '../client/components/app-router-headers'
import { signalFromNodeResponse } from './web/spec-extension/adapters/next-request' import { signalFromNodeResponse } from './web/spec-extension/adapters/next-request'
import { RouteModuleLoader } from './lib/module-loader/route-module-loader'
import { loadManifest } from './load-manifest' import { loadManifest } from './load-manifest'
import { lazyRenderAppPage } from './route-modules/app-page/module.render' import { lazyRenderAppPage } from './route-modules/app-page/module.render'
import { lazyRenderPagesPage } from './route-modules/pages/module.render' import { lazyRenderPagesPage } from './route-modules/pages/module.render'
...@@ -113,6 +110,7 @@ import { initializeCacheHandlers, setCacheHandler } from './use-cache/handlers' ...@@ -113,6 +110,7 @@ import { initializeCacheHandlers, setCacheHandler } from './use-cache/handlers'
import type { UnwrapPromise } from '../lib/coalesced-function' import type { UnwrapPromise } from '../lib/coalesced-function'
import { populateStaticEnv } from '../lib/static-env' import { populateStaticEnv } from '../lib/static-env'
import { isPostpone } from './lib/router-utils/is-postpone' import { isPostpone } from './lib/router-utils/is-postpone'
import { NodeModuleLoader } from './lib/module-loader/node-module-loader'
export * from './base-server' export * from './base-server'
...@@ -654,30 +652,24 @@ export default class NextNodeServer extends BaseServer< ...@@ -654,30 +652,24 @@ export default class NextNodeServer extends BaseServer<
} }
} }
} }
// The module supports minimal mode, load the minimal module. // The module supports minimal mode, load the minimal module.
const module = await RouteModuleLoader.load<PagesAPIRouteModule>( // Restore original URL as the handler handles it's own parsing
match.definition.filename const parsedInitUrl = parseUrl(getRequestMeta(req, 'initURL') || req.url)
) req.url = `${parsedInitUrl.pathname}${parsedInitUrl.search || ''}`
query = { ...query, ...match.params }
await module.render(req.originalRequest, res.originalResponse, { const loader = new NodeModuleLoader()
previewProps: this.renderOpts.previewProps, const module = (await loader.load(match.definition.filename)) as {
revalidate: this.revalidate.bind(this), handler: (
trustHostHeader: this.nextConfig.experimental.trustHostHeader, req: IncomingMessage,
allowedRevalidateHeaderKeys: res: ServerResponse,
this.nextConfig.experimental.allowedRevalidateHeaderKeys, ctx: {
hostname: this.fetchHostname, waitUntil: ReturnType<BaseServer['getWaitUntil']>
minimalMode: this.minimalMode, }
dev: this.renderOpts.dev === true, ) => Promise<void>
query, }
params: match.params, await module.handler(req.originalRequest, res.originalResponse, {
page: match.definition.pathname, waitUntil: this.getWaitUntil(),
onError: this.instrumentationOnRequestError.bind(this),
multiZoneDraftMode: this.nextConfig.experimental.multiZoneDraftMode,
}) })
return true return true
} }
...@@ -1849,29 +1841,6 @@ export default class NextNodeServer extends BaseServer< ...@@ -1849,29 +1841,6 @@ export default class NextNodeServer extends BaseServer<
if (this._cachedPreviewManifest) { if (this._cachedPreviewManifest) {
return this._cachedPreviewManifest return this._cachedPreviewManifest
} }
if (
this.renderOpts?.dev ||
this.serverOptions?.dev ||
process.env.NODE_ENV === 'development' ||
process.env.NEXT_PHASE === PHASE_PRODUCTION_BUILD
) {
this._cachedPreviewManifest = {
version: 4,
routes: {},
dynamicRoutes: {},
notFoundRoutes: [],
preview: {
previewModeId: require('crypto').randomBytes(16).toString('hex'),
previewModeSigningKey: require('crypto')
.randomBytes(32)
.toString('hex'),
previewModeEncryptionKey: require('crypto')
.randomBytes(32)
.toString('hex'),
},
}
return this._cachedPreviewManifest
}
this._cachedPreviewManifest = loadManifest( this._cachedPreviewManifest = loadManifest(
join(this.distDir, PRERENDER_MANIFEST) join(this.distDir, PRERENDER_MANIFEST)
......
...@@ -78,6 +78,10 @@ interface NextWrapperServer { ...@@ -78,6 +78,10 @@ interface NextWrapperServer {
logError(...args: Parameters<NextNodeServer['logError']>): void logError(...args: Parameters<NextNodeServer['logError']>): void
revalidate(
...args: Parameters<NextNodeServer['revalidate']>
): ReturnType<NextNodeServer['revalidate']>
render( render(
...args: Parameters<NextNodeServer['render']> ...args: Parameters<NextNodeServer['render']>
): ReturnType<NextNodeServer['render']> ): ReturnType<NextNodeServer['render']>
...@@ -157,6 +161,11 @@ export class NextServer implements NextWrapperServer { ...@@ -157,6 +161,11 @@ export class NextServer implements NextWrapperServer {
} }
} }
async revalidate(...args: Parameters<NextWrapperServer['revalidate']>) {
const server = await this.getServer()
return server.revalidate(...args)
}
async render(...args: Parameters<NextWrapperServer['render']>) { async render(...args: Parameters<NextWrapperServer['render']>) {
const server = await this.getServer() const server = await this.getServer()
return server.render(...args) return server.render(...args)
...@@ -424,6 +433,10 @@ class NextCustomServer implements NextWrapperServer { ...@@ -424,6 +433,10 @@ class NextCustomServer implements NextWrapperServer {
this.server.logError(...args) this.server.logError(...args)
} }
async revalidate(...args: Parameters<NextWrapperServer['revalidate']>) {
return this.server.revalidate(...args)
}
async renderToHTML(...args: Parameters<NextWrapperServer['renderToHTML']>) { async renderToHTML(...args: Parameters<NextWrapperServer['renderToHTML']>) {
return this.server.renderToHTML(...args) return this.server.renderToHTML(...args)
} }
......
...@@ -42,17 +42,6 @@ type PagesAPIRouteHandlerContext = RouteModuleHandleContext & { ...@@ -42,17 +42,6 @@ type PagesAPIRouteHandlerContext = RouteModuleHandleContext & {
*/ */
res?: ServerResponse res?: ServerResponse
/**
* The revalidate method used by the `revalidate` API.
*
* @param config the configuration for the revalidation
*/
revalidate: (config: {
urlPath: string
revalidateHeaders: { [key: string]: string | string[] }
opts: { unstable_onlyGenerated?: boolean }
}) => Promise<void>
/** /**
* The hostname for the request. * The hostname for the request.
*/ */
...@@ -84,9 +73,10 @@ type PagesAPIRouteHandlerContext = RouteModuleHandleContext & { ...@@ -84,9 +73,10 @@ type PagesAPIRouteHandlerContext = RouteModuleHandleContext & {
dev: boolean dev: boolean
/** /**
* True if the server is in minimal mode. * Whether errors should be left uncaught to handle
* higher up
*/ */
minimalMode: boolean propagateError: boolean
/** /**
* The page that's being rendered. * The page that's being rendered.
...@@ -149,14 +139,13 @@ export class PagesAPIRouteModule extends RouteModule< ...@@ -149,14 +139,13 @@ export class PagesAPIRouteModule extends RouteModule<
this.userland, this.userland,
{ {
...context.previewProps, ...context.previewProps,
revalidate: context.revalidate,
trustHostHeader: context.trustHostHeader, trustHostHeader: context.trustHostHeader,
allowedRevalidateHeaderKeys: context.allowedRevalidateHeaderKeys, allowedRevalidateHeaderKeys: context.allowedRevalidateHeaderKeys,
hostname: context.hostname, hostname: context.hostname,
multiZoneDraftMode: context.multiZoneDraftMode, multiZoneDraftMode: context.multiZoneDraftMode,
dev: context.dev, dev: context.dev,
}, },
context.minimalMode, context.propagateError,
context.dev, context.dev,
context.page, context.page,
context.onError context.onError
......
...@@ -24,13 +24,14 @@ import { ...@@ -24,13 +24,14 @@ import {
} from '../lib/constants' } from '../lib/constants'
import { normalizeNextQueryParam } from './web/utils' import { normalizeNextQueryParam } from './web/utils'
import type { IncomingHttpHeaders } from 'http' import type { IncomingHttpHeaders } from 'http'
import { decodeQueryPathParameter } from './lib/decode-query-path-parameter'
export function normalizeVercelUrl( export function normalizeCdnUrl(
req: BaseNextRequest, req: BaseNextRequest,
paramKeys: string[], paramKeys: string[],
defaultRouteRegex: ReturnType<typeof getNamedRouteRegex> | undefined defaultRouteRegex: ReturnType<typeof getNamedRouteRegex> | undefined
) { ) {
// make sure to normalize req.url on Vercel to strip dynamic and rewrite // make sure to normalize req.url from CDNs to strip dynamic and rewrite
// params from the query which are added during routing // params from the query which are added during routing
const _parsedUrl = parseUrl(req.url!, true) const _parsedUrl = parseUrl(req.url!, true)
delete (_parsedUrl as any).search delete (_parsedUrl as any).search
...@@ -362,11 +363,35 @@ export function getUtils({ ...@@ -362,11 +363,35 @@ export function getUtils({
return routeMatches return routeMatches
} }
function normalizeQueryParams(
query: Record<string, string | string[] | undefined>
) {
// this is used to pass query information in rewrites
// but should not be exposed in final query
delete query['nextInternalLocale']
for (const [key, value] of Object.entries(query)) {
const normalizedKey = normalizeNextQueryParam(key)
if (!normalizedKey) continue
// Remove the prefixed key from the query params because we want
// to consume it for the dynamic route matcher.
delete query[key]
if (typeof value === 'undefined') continue
query[normalizedKey] = Array.isArray(value)
? value.map((v) => decodeQueryPathParameter(v))
: decodeQueryPathParameter(value)
}
}
return { return {
handleRewrites, handleRewrites,
defaultRouteRegex, defaultRouteRegex,
dynamicRouteMatcher, dynamicRouteMatcher,
defaultRouteMatches, defaultRouteMatches,
normalizeQueryParams,
getParamsFromRouteMatches, getParamsFromRouteMatches,
/** /**
* Normalize dynamic route params. * Normalize dynamic route params.
...@@ -390,8 +415,8 @@ export function getUtils({ ...@@ -390,8 +415,8 @@ export function getUtils({
ignoreMissingOptional ignoreMissingOptional
) )
}, },
normalizeVercelUrl: (req: BaseNextRequest, paramKeys: string[]) => normalizeCdnUrl: (req: BaseNextRequest, paramKeys: string[]) =>
normalizeVercelUrl(req, paramKeys, defaultRouteRegex), normalizeCdnUrl(req, paramKeys, defaultRouteRegex),
interpolateDynamicPath: ( interpolateDynamicPath: (
pathname: string, pathname: string,
params: Record<string, undefined | string | string[]> params: Record<string, undefined | string | string[]>
......
...@@ -21,7 +21,7 @@ import { removeTrailingSlash } from '../shared/lib/router/utils/remove-trailing- ...@@ -21,7 +21,7 @@ import { removeTrailingSlash } from '../shared/lib/router/utils/remove-trailing-
import { isDynamicRoute } from '../shared/lib/router/utils' import { isDynamicRoute } from '../shared/lib/router/utils'
import { import {
interpolateDynamicPath, interpolateDynamicPath,
normalizeVercelUrl, normalizeCdnUrl,
normalizeDynamicRouteParams, normalizeDynamicRouteParams,
} from './server-utils' } from './server-utils'
import { getNamedRouteRegex } from '../shared/lib/router/utils/route-regex' import { getNamedRouteRegex } from '../shared/lib/router/utils/route-regex'
...@@ -186,7 +186,7 @@ export default class NextWebServer extends BaseServer< ...@@ -186,7 +186,7 @@ export default class NextWebServer extends BaseServer<
normalizedParams, normalizedParams,
routeRegex routeRegex
) )
normalizeVercelUrl(req, Object.keys(routeRegex.routeKeys), routeRegex) normalizeCdnUrl(req, Object.keys(routeRegex.routeKeys), routeRegex)
} }
} }
......
...@@ -5,10 +5,7 @@ ...@@ -5,10 +5,7 @@
*/ */
export function getEdgePreviewProps() { export function getEdgePreviewProps() {
return { return {
previewModeId: previewModeId: process.env.__NEXT_PREVIEW_MODE_ID || '',
process.env.NODE_ENV === 'production'
? process.env.__NEXT_PREVIEW_MODE_ID!
: 'development-id',
previewModeSigningKey: process.env.__NEXT_PREVIEW_MODE_SIGNING_KEY || '', previewModeSigningKey: process.env.__NEXT_PREVIEW_MODE_SIGNING_KEY || '',
previewModeEncryptionKey: previewModeEncryptionKey:
process.env.__NEXT_PREVIEW_MODE_ENCRYPTION_KEY || '', process.env.__NEXT_PREVIEW_MODE_ENCRYPTION_KEY || '',
......
...@@ -209,7 +209,7 @@ describe('server-side dev errors', () => { ...@@ -209,7 +209,7 @@ describe('server-side dev errors', () => {
}) })
const stderrOutput = stripAnsi(stderr.slice(stderrIdx)).trim() const stderrOutput = stripAnsi(stderr.slice(stderrIdx)).trim()
if (isTurbopack) {
expect(stderrOutput).toStartWith( expect(stderrOutput).toStartWith(
'⨯ ReferenceError: missingVar is not defined' + '⨯ ReferenceError: missingVar is not defined' +
'\n at handler (../../test/integration/server-side-dev-errors/pages/api/hello.js:2:2)' + '\n at handler (../../test/integration/server-side-dev-errors/pages/api/hello.js:2:2)' +
...@@ -217,6 +217,19 @@ describe('server-side dev errors', () => { ...@@ -217,6 +217,19 @@ describe('server-side dev errors', () => {
"\n> 2 | missingVar;res.status(200).json({ hello: 'world' })" + "\n> 2 | missingVar;res.status(200).json({ hello: 'world' })" +
'\n | ^' '\n | ^'
) )
} else {
expect(stderrOutput).toStartWith(
'⨯ ReferenceError: missingVar is not defined' +
'\n at handler (../../test/integration/server-side-dev-errors/pages/api/hello.js:2:2)' +
// TODO(veil): Why not ignore-listed?
'\n at '
)
expect(stderrOutput).toContain(
'\n 1 | export default function handler(req, res) {' +
"\n> 2 | missingVar;res.status(200).json({ hello: 'world' })" +
'\n | ^'
)
}
await expect(browser).toDisplayRedbox(` await expect(browser).toDisplayRedbox(`
{ {
...@@ -270,6 +283,7 @@ describe('server-side dev errors', () => { ...@@ -270,6 +283,7 @@ describe('server-side dev errors', () => {
}) })
const stderrOutput = stripAnsi(stderr.slice(stderrIdx)).trim() const stderrOutput = stripAnsi(stderr.slice(stderrIdx)).trim()
if (isTurbopack) {
expect(stderrOutput).toStartWith( expect(stderrOutput).toStartWith(
'⨯ ReferenceError: missingVar is not defined' + '⨯ ReferenceError: missingVar is not defined' +
'\n at handler (../../test/integration/server-side-dev-errors/pages/api/blog/[slug].js:2:2)' + '\n at handler (../../test/integration/server-side-dev-errors/pages/api/blog/[slug].js:2:2)' +
...@@ -277,6 +291,19 @@ describe('server-side dev errors', () => { ...@@ -277,6 +291,19 @@ describe('server-side dev errors', () => {
'\n> 2 | missingVar;res.status(200).json({ slug: req.query.slug })' + '\n> 2 | missingVar;res.status(200).json({ slug: req.query.slug })' +
'\n | ^' '\n | ^'
) )
} else {
expect(stderrOutput).toStartWith(
'⨯ ReferenceError: missingVar is not defined' +
'\n at handler (../../test/integration/server-side-dev-errors/pages/api/blog/[slug].js:2:2)' +
// TODO(veil): Why not ignore-listed?
'\n at'
)
expect(stderrOutput).toContain(
'\n 1 | export default function handler(req, res) {' +
'\n> 2 | missingVar;res.status(200).json({ slug: req.query.slug })' +
'\n | ^'
)
}
await expect(browser).toDisplayRedbox(` await expect(browser).toDisplayRedbox(`
{ {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment