Unverified Commit 256cd5b5 authored by JJ Kasper's avatar JJ Kasper Committed by GitHub

Add response handling inside handlers (#80189)

This continues the `handler` signature work from
https://github.com/vercel/next.js/pull/78166 and moves the
cache/response handling work inside of the pages, app pages, and app route handlers which allows them
to be fully invoked independent of `next-server`/`router-server`.

Validated against our deploy tests
https://github.com/vercel/vercel/actions/runs/15583209888/job/43883202911?pr=13433
and
https://github.com/vercel/next.js/actions/runs/15583265934/job/43883389321
parent 22ca7677
...@@ -693,10 +693,18 @@ ...@@ -693,10 +693,18 @@
"692": "Expected clientReferenceManifest to be defined.", "692": "Expected clientReferenceManifest to be defined.",
"693": "%s must not be used within a client component. Next.js should be preventing %s from being included in client components statically, but did not in this case.", "693": "%s must not be used within a client component. Next.js should be preventing %s from being included in client components statically, but did not in this case.",
"694": "createPrerenderPathname was called inside a client component scope.", "694": "createPrerenderPathname was called inside a client component scope.",
"695": "Expected workUnitAsyncStorage to have a store.", "695": "Invariant: received non-pages cache entry in pages handler",
"696": "Next DevTools: Can't dispatch %s in this environment. This is a bug in Next.js", "696": "Expected workUnitAsyncStorage to have a store.",
"697": "Next DevTools: Can't render in this environment. This is a bug in Next.js", "697": "Next DevTools: Can't render in this environment. This is a bug in Next.js",
"698": "Next DevTools: App Dev Overlay is already mounted. This is a bug in Next.js", "698": "Next DevTools: Can't dispatch %s in this environment. This is a bug in Next.js",
"699": "Next DevTools: Pages Dev Overlay is already mounted. This is a bug in Next.js", "699": "Next DevTools: App Dev Overlay is already mounted. This is a bug in Next.js",
"700": "Failed to persist Chrome DevTools workspace UUID. The Chrome DevTools Workspace needs to be reconnected after the next page reload." "700": "Next DevTools: Pages Dev Overlay is already mounted. This is a bug in Next.js",
"701": "Invariant: app-route received invalid cache entry %s",
"702": "Invariant: unexpected APP_ROUTE cache data",
"703": "Route is configured with dynamic = error which cannot be statically generated.",
"704": "Route is configured with dynamic = error be statically generated.",
"705": "Route is configured with dynamic = error that cannot be statically generated.",
"706": "Invariant: static responses cannot be streamed %s",
"707": "Invariant app-page handler received invalid cache entry %s",
"708": "Failed to persist Chrome DevTools workspace UUID. The Chrome DevTools Workspace needs to be reconnected after the next page reload."
} }
This diff is collapsed.
...@@ -52,6 +52,7 @@ import type { TemporaryReferenceSet } from 'react-server-dom-webpack/server.edge ...@@ -52,6 +52,7 @@ import type { TemporaryReferenceSet } from 'react-server-dom-webpack/server.edge
import { workUnitAsyncStorage } from '../app-render/work-unit-async-storage.external' import { workUnitAsyncStorage } from '../app-render/work-unit-async-storage.external'
import { InvariantError } from '../../shared/lib/invariant-error' import { InvariantError } from '../../shared/lib/invariant-error'
import { executeRevalidates } from '../revalidation-utils' import { executeRevalidates } from '../revalidation-utils'
import { getRequestMeta } from '../request-meta'
function formDataFromSearchQueryString(query: string) { function formDataFromSearchQueryString(query: string) {
const searchParams = new URLSearchParams(query) const searchParams = new URLSearchParams(query)
...@@ -160,8 +161,7 @@ async function createForwardedActionResponse( ...@@ -160,8 +161,7 @@ async function createForwardedActionResponse(
res: BaseNextResponse, res: BaseNextResponse,
host: Host, host: Host,
workerPathname: string, workerPathname: string,
basePath: string, basePath: string
workStore: WorkStore
) { ) {
if (!host) { if (!host) {
throw new Error( throw new Error(
...@@ -176,7 +176,8 @@ async function createForwardedActionResponse( ...@@ -176,7 +176,8 @@ async function createForwardedActionResponse(
// with the response from the forwarded worker // with the response from the forwarded worker
forwardedHeaders.set('x-action-forwarded', '1') forwardedHeaders.set('x-action-forwarded', '1')
const proto = workStore.incrementalCache?.requestProtocol || 'https' const proto =
getRequestMeta(req, 'initProtocol')?.replace(/:+$/, '') || 'https'
// For standalone or the serverful mode, use the internal origin directly // For standalone or the serverful mode, use the internal origin directly
// other than the host headers from the request. // other than the host headers from the request.
...@@ -306,7 +307,8 @@ async function createRedirectRenderResult( ...@@ -306,7 +307,8 @@ async function createRedirectRenderResult(
const forwardedHeaders = getForwardedHeaders(req, res) const forwardedHeaders = getForwardedHeaders(req, res)
forwardedHeaders.set(RSC_HEADER, '1') forwardedHeaders.set(RSC_HEADER, '1')
const proto = workStore.incrementalCache?.requestProtocol || 'https' const proto =
getRequestMeta(req, 'initProtocol')?.replace(/:+$/, '') || 'https'
// For standalone or the serverful mode, use the internal origin directly // For standalone or the serverful mode, use the internal origin directly
// other than the host headers from the request. // other than the host headers from the request.
...@@ -616,8 +618,7 @@ export async function handleAction({ ...@@ -616,8 +618,7 @@ export async function handleAction({
res, res,
host, host,
forwardedWorker, forwardedWorker,
ctx.renderOpts.basePath, ctx.renderOpts.basePath
workStore
), ),
} }
} }
......
...@@ -202,6 +202,8 @@ export interface RenderOptsPartial { ...@@ -202,6 +202,8 @@ export interface RenderOptsPartial {
cacheLifeProfiles?: { cacheLifeProfiles?: {
[profile: string]: import('../use-cache/cache-life').CacheLife [profile: string]: import('../use-cache/cache-life').CacheLife
} }
isOnDemandRevalidate?: boolean
isPossibleServerAction?: boolean
setIsrStatus?: (key: string, value: boolean | null) => void setIsrStatus?: (key: string, value: boolean | null) => void
isRevalidate?: boolean isRevalidate?: boolean
nextExport?: boolean nextExport?: boolean
......
...@@ -121,10 +121,7 @@ import { getTracer, isBubbledError, SpanKind } from './lib/trace/tracer' ...@@ -121,10 +121,7 @@ import { getTracer, isBubbledError, SpanKind } from './lib/trace/tracer'
import { BaseServerSpan } from './lib/trace/constants' import { BaseServerSpan } from './lib/trace/constants'
import { I18NProvider } from './lib/i18n-provider' import { I18NProvider } from './lib/i18n-provider'
import { sendResponse } from './send-response' import { sendResponse } from './send-response'
import { import { normalizeNextQueryParam } from './web/utils'
fromNodeOutgoingHttpHeaders,
normalizeNextQueryParam,
} from './web/utils'
import { import {
CACHE_ONE_YEAR, CACHE_ONE_YEAR,
MATCHED_PATH_HEADER, MATCHED_PATH_HEADER,
...@@ -169,6 +166,7 @@ import { ...@@ -169,6 +166,7 @@ import {
} from './lib/streaming-metadata' } from './lib/streaming-metadata'
import { InvariantError } from '../shared/lib/invariant-error' import { InvariantError } from '../shared/lib/invariant-error'
import { decodeQueryPathParameter } from './lib/decode-query-path-parameter' import { decodeQueryPathParameter } from './lib/decode-query-path-parameter'
import { NoFallbackError } from '../shared/lib/no-fallback-error.external'
import { getCacheHandlers } from './use-cache/handlers' import { getCacheHandlers } from './use-cache/handlers'
import { fixMojibake } from './lib/fix-mojibake' import { fixMojibake } from './lib/fix-mojibake'
import { computeCacheBustingSearchParam } from '../shared/lib/router/utils/cache-busting-search-param' import { computeCacheBustingSearchParam } from '../shared/lib/router/utils/cache-busting-search-param'
...@@ -302,8 +300,6 @@ export type RequestContext< ...@@ -302,8 +300,6 @@ export type RequestContext<
renderOpts: RenderOpts renderOpts: RenderOpts
} }
export class NoFallbackError extends Error {}
// Internal wrapper around build errors at development // Internal wrapper around build errors at development
// time, to prevent us from propagating or logging them // time, to prevent us from propagating or logging them
export class WrappedBuildError extends Error { export class WrappedBuildError extends Error {
...@@ -417,7 +413,6 @@ export default abstract class Server< ...@@ -417,7 +413,6 @@ export default abstract class Server<
protected abstract getIncrementalCache(options: { protected abstract getIncrementalCache(options: {
requestHeaders: Record<string, undefined | string | string[]> requestHeaders: Record<string, undefined | string | string[]>
requestProtocol: 'http' | 'https'
}): Promise<import('./lib/incremental-cache').IncrementalCache> }): Promise<import('./lib/incremental-cache').IncrementalCache>
protected abstract getResponseCache(options: { protected abstract getResponseCache(options: {
...@@ -1452,21 +1447,8 @@ export default abstract class Server< ...@@ -1452,21 +1447,8 @@ export default abstract class Server<
!(this.serverOptions as any).webServerConfig && !(this.serverOptions as any).webServerConfig &&
!getRequestMeta(req, 'incrementalCache') !getRequestMeta(req, 'incrementalCache')
) { ) {
let protocol: 'http:' | 'https:' = 'https:'
try {
const parsedFullUrl = new URL(
getRequestMeta(req, 'initURL') || '/',
'http://n'
)
protocol = parsedFullUrl.protocol as 'https:' | 'http:'
} catch {}
const incrementalCache = await this.getIncrementalCache({ const incrementalCache = await this.getIncrementalCache({
requestHeaders: Object.assign({}, req.headers), requestHeaders: Object.assign({}, req.headers),
requestProtocol: protocol.substring(0, protocol.length - 1) as
| 'http'
| 'https',
}) })
incrementalCache.resetRequestCache() incrementalCache.resetRequestCache()
...@@ -2495,15 +2477,6 @@ export default abstract class Server< ...@@ -2495,15 +2477,6 @@ export default abstract class Server<
ssgCacheKey = ssgCacheKey =
ssgCacheKey === '/index' && pathname === '/' ? '/' : ssgCacheKey ssgCacheKey === '/index' && pathname === '/' ? '/' : ssgCacheKey
} }
let protocol: 'http:' | 'https:' = 'https:'
try {
const parsedFullUrl = new URL(
getRequestMeta(req, 'initURL') || '/',
'http://n'
)
protocol = parsedFullUrl.protocol as 'https:' | 'http:'
} catch {}
// use existing incrementalCache instance if available // use existing incrementalCache instance if available
const incrementalCache: import('./lib/incremental-cache').IncrementalCache = const incrementalCache: import('./lib/incremental-cache').IncrementalCache =
...@@ -2512,9 +2485,6 @@ export default abstract class Server< ...@@ -2512,9 +2485,6 @@ export default abstract class Server<
? (globalThis as any).__incrementalCache ? (globalThis as any).__incrementalCache
: await this.getIncrementalCache({ : await this.getIncrementalCache({
requestHeaders: Object.assign({}, req.headers), requestHeaders: Object.assign({}, req.headers),
requestProtocol: protocol.substring(0, protocol.length - 1) as
| 'http'
| 'https',
}) })
// TODO: investigate, this is not safe across multiple concurrent requests // TODO: investigate, this is not safe across multiple concurrent requests
...@@ -2667,19 +2637,12 @@ export default abstract class Server< ...@@ -2667,19 +2637,12 @@ export default abstract class Server<
const parsedInitUrl = parseUrl( const parsedInitUrl = parseUrl(
getRequestMeta(req, 'initURL') || req.url getRequestMeta(req, 'initURL') || req.url
) )
request.url = request.url = `${parsedInitUrl.pathname}${parsedInitUrl.search || ''}`
req.url = `${parsedInitUrl.pathname}${parsedInitUrl.search || ''}`
// propagate the request context for dev // propagate the request context for dev
setRequestMeta(request, getRequestMeta(req)) setRequestMeta(request, getRequestMeta(req))
addRequestMeta(request, 'postponed', postponed)
addRequestMeta(request, 'projectDir', this.dir) addRequestMeta(request, 'projectDir', this.dir)
addRequestMeta(request, 'isIsrFallback', pagesFallback) addRequestMeta(request, 'isIsrFallback', pagesFallback)
addRequestMeta(
request,
'renderFallbackShell',
Boolean(fallbackRouteParams)
)
addRequestMeta(request, 'query', query) addRequestMeta(request, 'query', query)
addRequestMeta(request, 'params', opts.params) addRequestMeta(request, 'params', opts.params)
addRequestMeta( addRequestMeta(
...@@ -2687,6 +2650,7 @@ export default abstract class Server< ...@@ -2687,6 +2650,7 @@ export default abstract class Server<
'ampValidator', 'ampValidator',
this.renderOpts.ampValidator this.renderOpts.ampValidator
) )
addRequestMeta(request, 'minimalMode', this.minimalMode)
if (renderOpts.err) { if (renderOpts.err) {
addRequestMeta(request, 'invokeError', renderOpts.err) addRequestMeta(request, 'invokeError', renderOpts.err)
...@@ -2700,20 +2664,35 @@ export default abstract class Server< ...@@ -2700,20 +2664,35 @@ export default abstract class Server<
} }
) => Promise<RenderResult> = components.ComponentMod.handler ) => Promise<RenderResult> = components.ComponentMod.handler
result = await handler(request, response, { const maybeDevRequest =
// we need to capture fetch metrics when they are set
// and can't wait for handler to resolve as the fetch
// metrics are logged on response close which happens
// before handler resolves
process.env.NODE_ENV === 'development'
? new Proxy(request, {
get(target: any, prop) {
if (typeof target[prop] === 'function') {
return target[prop].bind(target)
}
return target[prop]
},
set(target: any, prop, value) {
if (prop === 'fetchMetrics') {
;(req as any).fetchMetrics = value
}
target[prop] = value
return true
},
})
: request
result = await handler(maybeDevRequest, response, {
waitUntil: this.getWaitUntil(), waitUntil: this.getWaitUntil(),
}) })
// this is handled fully in handler // response is handled fully in handler
if (isAppRouteRouteModule(routeModule)) { return null
return result as any as ResponseCacheEntry | null
}
if (!result) {
throw new Error(
`Invariant: missing result from invoking ${pathname} handler`
)
}
} else { } else {
if (isPagesRouteModule(routeModule)) { if (isPagesRouteModule(routeModule)) {
// Due to the way we pass data by mutating `renderOpts`, we can't extend // Due to the way we pass data by mutating `renderOpts`, we can't extend
...@@ -3073,7 +3052,7 @@ export default abstract class Server< ...@@ -3073,7 +3052,7 @@ export default abstract class Server<
} }
) )
} }
// If this is a app router page, PPR is enabled, and PFPR is also // If this is a app router page, PPR is enabled, and PPR is also
// enabled, then we should use the fallback renderer. // enabled, then we should use the fallback renderer.
else if ( else if (
isRoutePPREnabled && isRoutePPREnabled &&
...@@ -3164,6 +3143,34 @@ export default abstract class Server< ...@@ -3164,6 +3143,34 @@ export default abstract class Server<
}) })
} }
if (
process.env.NEXT_RUNTIME !== 'edge' &&
// default _error module in dev doesn't have handler yet
components.ComponentMod.handler &&
(isPagesRouteModule(components.routeModule) ||
isAppRouteRouteModule(components.routeModule) ||
isAppPageRouteModule(components.routeModule))
) {
if (
routeModule?.isDev &&
isDynamicRoute(pathname) &&
(components.getStaticPaths || isAppPath)
) {
await this.getStaticPaths({
pathname,
requestHeaders: req.headers,
page: components.page,
isAppPath,
})
}
await doRender({
postponed: undefined,
pagesFallback: false,
fallbackRouteParams: null,
})
return null
}
const cacheEntry = await this.responseCache.get( const cacheEntry = await this.responseCache.get(
ssgCacheKey, ssgCacheKey,
responseGenerator, responseGenerator,
...@@ -3188,7 +3195,13 @@ export default abstract class Server< ...@@ -3188,7 +3195,13 @@ export default abstract class Server<
} }
if (!cacheEntry) { if (!cacheEntry) {
if (ssgCacheKey && !(isOnDemandRevalidate && revalidateOnlyGenerated)) { if (
ssgCacheKey &&
!(isOnDemandRevalidate && revalidateOnlyGenerated) &&
!isPagesRouteModule(components.routeModule) &&
!isAppRouteRouteModule(components.routeModule) &&
!isAppPageRouteModule(components.routeModule)
) {
// A cache entry might not be generated if a response is written // A cache entry might not be generated if a response is written
// in `getInitialProps` or `getServerSideProps`, but those shouldn't // in `getInitialProps` or `getServerSideProps`, but those shouldn't
// have a cache key. If we do have a cache key but we don't end up // have a cache key. If we do have a cache key but we don't end up
...@@ -3443,34 +3456,8 @@ export default abstract class Server< ...@@ -3443,34 +3456,8 @@ export default abstract class Server<
return null return null
} }
} else if (cachedData.kind === CachedRouteKind.APP_ROUTE) { } else if (cachedData.kind === CachedRouteKind.APP_ROUTE) {
const headers = fromNodeOutgoingHttpHeaders(cachedData.headers) // this is handled inside the app_route handler fully
throw new Error(`Invariant: unexpected APP_ROUTE cache data`)
if (!(this.minimalMode && isSSG)) {
headers.delete(NEXT_CACHE_TAGS_HEADER)
}
// If cache control is already set on the response we don't
// override it to allow users to customize it via next.config
if (
cacheEntry.cacheControl &&
!res.getHeader('Cache-Control') &&
!headers.get('Cache-Control')
) {
headers.set(
'Cache-Control',
getCacheControlHeader(cacheEntry.cacheControl)
)
}
await sendResponse(
req,
res,
new Response(cachedData.body, {
headers,
status: cachedData.status || 200,
})
)
return null
} else if (cachedData.kind === CachedRouteKind.APP_PAGE) { } else if (cachedData.kind === CachedRouteKind.APP_PAGE) {
// If the request has a postponed state and it's a resume request we // If the request has a postponed state and it's a resume request we
// should error. // should error.
......
...@@ -33,6 +33,7 @@ import { ...@@ -33,6 +33,7 @@ import {
PAGES_MANIFEST, PAGES_MANIFEST,
APP_PATHS_MANIFEST, APP_PATHS_MANIFEST,
COMPILER_NAMES, COMPILER_NAMES,
PRERENDER_MANIFEST,
} from '../../shared/lib/constants' } from '../../shared/lib/constants'
import Server, { WrappedBuildError } from '../next-server' import Server, { WrappedBuildError } from '../next-server'
import { normalizePagePath } from '../../shared/lib/page-path/normalize-page-path' import { normalizePagePath } from '../../shared/lib/page-path/normalize-page-path'
...@@ -67,12 +68,13 @@ import { decorateServerError } from '../../shared/lib/error-source' ...@@ -67,12 +68,13 @@ import { decorateServerError } from '../../shared/lib/error-source'
import type { ServerOnInstrumentationRequestError } from '../app-render/types' import type { ServerOnInstrumentationRequestError } from '../app-render/types'
import type { ServerComponentsHmrCache } from '../response-cache' import type { ServerComponentsHmrCache } from '../response-cache'
import { logRequests } from './log-requests' import { logRequests } from './log-requests'
import { FallbackMode } from '../../lib/fallback' import { FallbackMode, fallbackModeToFallbackField } from '../../lib/fallback'
import type { PagesDevOverlayBridgeType } from '../../client/components/react-dev-overlay/pages/pages-dev-overlay-setup' import type { PagesDevOverlayBridgeType } from '../../client/components/react-dev-overlay/pages/pages-dev-overlay-setup'
import { import {
ensureInstrumentationRegistered, ensureInstrumentationRegistered,
getInstrumentationModule, getInstrumentationModule,
} from '../lib/router-utils/instrumentation-globals.external' } from '../lib/router-utils/instrumentation-globals.external'
import type { PrerenderManifest } from '../../build'
// Load ReactDevOverlay only when needed // Load ReactDevOverlay only when needed
let PagesDevOverlayBridgeImpl: PagesDevOverlayBridgeType let PagesDevOverlayBridgeImpl: PagesDevOverlayBridgeType
...@@ -848,9 +850,10 @@ export default class DevServer extends Server { ...@@ -848,9 +850,10 @@ export default class DevServer extends Server {
`staticPaths-${pathname}`, `staticPaths-${pathname}`,
[] []
) )
.then((res) => { .then(async (res) => {
const { prerenderedRoutes: staticPaths, fallbackMode: fallback } = const { prerenderedRoutes: staticPaths, fallbackMode: fallback } =
res.value res.value
if (!isAppPath && this.nextConfig.output === 'export') { if (!isAppPath && this.nextConfig.output === 'export') {
if (fallback === FallbackMode.BLOCKING_STATIC_RENDER) { if (fallback === FallbackMode.BLOCKING_STATIC_RENDER) {
throw new Error( throw new Error(
...@@ -870,6 +873,32 @@ export default class DevServer extends Server { ...@@ -870,6 +873,32 @@ export default class DevServer extends Server {
staticPaths: staticPaths?.map((route) => route.pathname), staticPaths: staticPaths?.map((route) => route.pathname),
fallbackMode: fallback, fallbackMode: fallback,
} }
if (res.value?.fallbackMode !== undefined) {
// we write the static paths to partial manifest for
// fallback handling inside of entry handler's
const rawExistingManifest = await fs.promises.readFile(
pathJoin(this.distDir, PRERENDER_MANIFEST),
'utf8'
)
const existingManifest: PrerenderManifest =
JSON.parse(rawExistingManifest)
for (const staticPath of value.staticPaths || []) {
existingManifest.routes[staticPath] = {} as any
}
existingManifest.dynamicRoutes[pathname] = {
fallback: fallbackModeToFallbackField(res.value.fallbackMode, page),
} as any
const updatedManifest = JSON.stringify(existingManifest)
if (updatedManifest !== rawExistingManifest) {
await fs.promises.writeFile(
pathJoin(this.distDir, PRERENDER_MANIFEST),
updatedManifest
)
}
}
this.staticPathsCache.set(pathname, value) this.staticPathsCache.set(pathname, value)
return value return value
}) })
......
...@@ -85,7 +85,6 @@ export class IncrementalCache implements IncrementalCacheType { ...@@ -85,7 +85,6 @@ export class IncrementalCache implements IncrementalCacheType {
readonly hasCustomCacheHandler: boolean readonly hasCustomCacheHandler: boolean
readonly prerenderManifest: DeepReadonly<PrerenderManifest> readonly prerenderManifest: DeepReadonly<PrerenderManifest>
readonly requestHeaders: Record<string, undefined | string | string[]> readonly requestHeaders: Record<string, undefined | string | string[]>
readonly requestProtocol?: 'http' | 'https'
readonly allowedRevalidateHeaderKeys?: string[] readonly allowedRevalidateHeaderKeys?: string[]
readonly minimalMode?: boolean readonly minimalMode?: boolean
readonly fetchCacheKeyPrefix?: string readonly fetchCacheKeyPrefix?: string
...@@ -107,7 +106,6 @@ export class IncrementalCache implements IncrementalCacheType { ...@@ -107,7 +106,6 @@ export class IncrementalCache implements IncrementalCacheType {
minimalMode, minimalMode,
serverDistDir, serverDistDir,
requestHeaders, requestHeaders,
requestProtocol,
maxMemoryCacheSize, maxMemoryCacheSize,
getPrerenderManifest, getPrerenderManifest,
fetchCacheKeyPrefix, fetchCacheKeyPrefix,
...@@ -119,7 +117,6 @@ export class IncrementalCache implements IncrementalCacheType { ...@@ -119,7 +117,6 @@ export class IncrementalCache implements IncrementalCacheType {
minimalMode?: boolean minimalMode?: boolean
serverDistDir?: string serverDistDir?: string
flushToDisk?: boolean flushToDisk?: boolean
requestProtocol?: 'http' | 'https'
allowedRevalidateHeaderKeys?: string[] allowedRevalidateHeaderKeys?: string[]
requestHeaders: IncrementalCache['requestHeaders'] requestHeaders: IncrementalCache['requestHeaders']
maxMemoryCacheSize?: number maxMemoryCacheSize?: number
...@@ -166,7 +163,6 @@ export class IncrementalCache implements IncrementalCacheType { ...@@ -166,7 +163,6 @@ export class IncrementalCache implements IncrementalCacheType {
const minimalModeKey = 'minimalMode' const minimalModeKey = 'minimalMode'
this[minimalModeKey] = minimalMode this[minimalModeKey] = minimalMode
this.requestHeaders = requestHeaders this.requestHeaders = requestHeaders
this.requestProtocol = requestProtocol
this.allowedRevalidateHeaderKeys = allowedRevalidateHeaderKeys this.allowedRevalidateHeaderKeys = allowedRevalidateHeaderKeys
this.prerenderManifest = getPrerenderManifest() this.prerenderManifest = getPrerenderManifest()
this.cacheControls = new SharedCacheControls(this.prerenderManifest) this.cacheControls = new SharedCacheControls(this.prerenderManifest)
......
...@@ -23,7 +23,6 @@ import { addRequestMeta, getRequestMeta } from '../request-meta' ...@@ -23,7 +23,6 @@ import { addRequestMeta, getRequestMeta } from '../request-meta'
import { pathHasPrefix } from '../../shared/lib/router/utils/path-has-prefix' import { pathHasPrefix } from '../../shared/lib/router/utils/path-has-prefix'
import { removePathPrefix } from '../../shared/lib/router/utils/remove-path-prefix' import { removePathPrefix } from '../../shared/lib/router/utils/remove-path-prefix'
import setupCompression from 'next/dist/compiled/compression' import setupCompression from 'next/dist/compiled/compression'
import { NoFallbackError } from '../base-server'
import { signalFromNodeResponse } from '../web/spec-extension/adapters/next-request' import { signalFromNodeResponse } from '../web/spec-extension/adapters/next-request'
import { isPostpone } from './router-utils/is-postpone' import { isPostpone } from './router-utils/is-postpone'
import { parseUrl as parseUrlUtil } from '../../shared/lib/router/utils/parse-url' import { parseUrl as parseUrlUtil } from '../../shared/lib/router/utils/parse-url'
...@@ -51,6 +50,7 @@ import type { ServerInitResult } from './render-server' ...@@ -51,6 +50,7 @@ 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 { NoFallbackError } from '../../shared/lib/no-fallback-error.external'
import { import {
RouterServerContextSymbol, RouterServerContextSymbol,
routerServerGlobal, routerServerGlobal,
...@@ -686,10 +686,11 @@ export async function initialize(opts: { ...@@ -686,10 +686,11 @@ export async function initialize(opts: {
nextConfig: config, nextConfig: config,
hostname: handlers.server.hostname, hostname: handlers.server.hostname,
revalidate: handlers.server.revalidate.bind(handlers.server), revalidate: handlers.server.revalidate.bind(handlers.server),
render404: handlers.server.render404.bind(handlers.server),
experimentalTestProxy: renderServerOpts.experimentalTestProxy, experimentalTestProxy: renderServerOpts.experimentalTestProxy,
logErrorWithOriginalStack: opts.dev logErrorWithOriginalStack: opts.dev
? handlers.server.logErrorWithOriginalStack.bind(handlers.server) ? handlers.server.logErrorWithOriginalStack.bind(handlers.server)
: (err: unknown) => Log.error(err), : (err: unknown) => !opts.quiet && Log.error(err),
setIsrStatus: devBundlerService?.setIsrStatus.bind(devBundlerService), setIsrStatus: devBundlerService?.setIsrStatus.bind(devBundlerService),
} }
......
...@@ -251,7 +251,7 @@ export function getResolveRoutes( ...@@ -251,7 +251,7 @@ export function getResolveRoutes(
} }
async function checkTrue() { async function checkTrue() {
const pathname = parsedUrl.pathname || '' const pathname = parsedUrl.pathname || '/'
if (checkLocaleApi(pathname)) { if (checkLocaleApi(pathname)) {
return return
...@@ -435,7 +435,7 @@ export function getResolveRoutes( ...@@ -435,7 +435,7 @@ export function getResolveRoutes(
} }
if (route.name === 'check_fs') { if (route.name === 'check_fs') {
const pathname = parsedUrl.pathname || '' const pathname = parsedUrl.pathname || '/'
if (invokedOutputs?.has(pathname) || checkLocaleApi(pathname)) { if (invokedOutputs?.has(pathname) || checkLocaleApi(pathname)) {
return return
......
import type { IncomingMessage, ServerResponse } from 'node:http'
import type { NextConfigComplete } from '../../config-shared' import type { NextConfigComplete } from '../../config-shared'
import type { UrlWithParsedQuery } from 'node:url'
export type RevalidateFn = (config: { export type RevalidateFn = (config: {
urlPath: string urlPath: string
...@@ -19,6 +21,13 @@ export type RouterServerContext = Record< ...@@ -19,6 +21,13 @@ export type RouterServerContext = Record<
// revalidate function to bypass going through network // revalidate function to bypass going through network
// to invoke revalidate request (uses mocked req/res) // to invoke revalidate request (uses mocked req/res)
revalidate?: RevalidateFn revalidate?: RevalidateFn
// function to render the 404 page
render404?: (
req: IncomingMessage,
res: ServerResponse,
parsedUrl?: UrlWithParsedQuery,
setHeaders?: boolean
) => Promise<void>
// current loaded public runtime config // current loaded public runtime config
publicRuntimeConfig?: NextConfigComplete['publicRuntimeConfig'] publicRuntimeConfig?: NextConfigComplete['publicRuntimeConfig']
// exposing nextConfig for dev mode specifically // exposing nextConfig for dev mode specifically
......
...@@ -25,7 +25,7 @@ import type { CacheControl } from './lib/cache-control' ...@@ -25,7 +25,7 @@ import type { CacheControl } from './lib/cache-control'
import type { WaitUntil } from './after/builtin-request-context' import type { WaitUntil } from './after/builtin-request-context'
import fs from 'fs' import fs from 'fs'
import { join } from 'path' import { join, relative } from 'path'
import { getRouteMatcher } from '../shared/lib/router/utils/route-matcher' import { getRouteMatcher } from '../shared/lib/router/utils/route-matcher'
import { addRequestMeta, getRequestMeta } from './request-meta' import { addRequestMeta, getRequestMeta } from './request-meta'
import { import {
...@@ -58,7 +58,7 @@ import type { ...@@ -58,7 +58,7 @@ import type {
NextEnabledDirectories, NextEnabledDirectories,
BaseRequestHandler, BaseRequestHandler,
} from './base-server' } from './base-server'
import BaseServer, { NoFallbackError } from './base-server' import BaseServer from './base-server'
import { getMaybePagePath, getPagePath } from './require' import { getMaybePagePath, getPagePath } from './require'
import { denormalizePagePath } from '../shared/lib/page-path/denormalize-page-path' import { denormalizePagePath } from '../shared/lib/page-path/denormalize-page-path'
import { normalizePagePath } from '../shared/lib/page-path/normalize-page-path' import { normalizePagePath } from '../shared/lib/page-path/normalize-page-path'
...@@ -110,10 +110,15 @@ import type { UnwrapPromise } from '../lib/coalesced-function' ...@@ -110,10 +110,15 @@ 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' import { NodeModuleLoader } from './lib/module-loader/node-module-loader'
import { NoFallbackError } from '../shared/lib/no-fallback-error.external'
import { import {
ensureInstrumentationRegistered, ensureInstrumentationRegistered,
getInstrumentationModule, getInstrumentationModule,
} from './lib/router-utils/instrumentation-globals.external' } from './lib/router-utils/instrumentation-globals.external'
import {
RouterServerContextSymbol,
routerServerGlobal,
} from './lib/router-utils/router-server-context'
export * from './base-server' export * from './base-server'
...@@ -493,10 +498,8 @@ export default class NextNodeServer extends BaseServer< ...@@ -493,10 +498,8 @@ export default class NextNodeServer extends BaseServer<
protected async getIncrementalCache({ protected async getIncrementalCache({
requestHeaders, requestHeaders,
requestProtocol,
}: { }: {
requestHeaders: IncrementalCache['requestHeaders'] requestHeaders: IncrementalCache['requestHeaders']
requestProtocol: 'http' | 'https'
}) { }) {
const dev = !!this.renderOpts.dev const dev = !!this.renderOpts.dev
let CacheHandler: any let CacheHandler: any
...@@ -519,7 +522,6 @@ export default class NextNodeServer extends BaseServer< ...@@ -519,7 +522,6 @@ export default class NextNodeServer extends BaseServer<
fs: this.getCacheFilesystem(), fs: this.getCacheFilesystem(),
dev, dev,
requestHeaders, requestHeaders,
requestProtocol,
allowedRevalidateHeaderKeys: allowedRevalidateHeaderKeys:
this.nextConfig.experimental.allowedRevalidateHeaderKeys, this.nextConfig.experimental.allowedRevalidateHeaderKeys,
minimalMode: this.minimalMode, minimalMode: this.minimalMode,
...@@ -1097,6 +1099,19 @@ export default class NextNodeServer extends BaseServer< ...@@ -1097,6 +1099,19 @@ export default class NextNodeServer extends BaseServer<
// such. // such.
addRequestMeta(req, 'bubbleNoFallback', true) addRequestMeta(req, 'bubbleNoFallback', true)
// TODO: this is only needed until route-module can handle
// rendering/serving the 404 directly with next-server
if (!routerServerGlobal[RouterServerContextSymbol]) {
routerServerGlobal[RouterServerContextSymbol] = {}
}
const relativeProjectDir = relative(process.cwd(), this.dir)
if (!routerServerGlobal[RouterServerContextSymbol][relativeProjectDir]) {
routerServerGlobal[RouterServerContextSymbol][relativeProjectDir] = {
render404: this.render404.bind(this),
}
}
try { try {
// next.js core assumes page path without trailing slash // next.js core assumes page path without trailing slash
pathname = removeTrailingSlash(pathname) pathname = removeTrailingSlash(pathname)
......
...@@ -225,6 +225,12 @@ export interface RequestMeta { ...@@ -225,6 +225,12 @@ export interface RequestMeta {
* ErrorOverlay component to use in development for pages router * ErrorOverlay component to use in development for pages router
*/ */
PagesErrorDebug?: PagesDevOverlayBridgeType PagesErrorDebug?: PagesDevOverlayBridgeType
/**
* Whether server is in minimal mode (this will be replaced with more
* specific flags in future)
*/
minimalMode?: boolean
} }
/** /**
......
...@@ -39,13 +39,13 @@ export default class ResponseCache implements ResponseCacheBase { ...@@ -39,13 +39,13 @@ export default class ResponseCache implements ResponseCacheBase {
expiresAt: number expiresAt: number
} }
private minimalMode?: boolean // we don't use minimal_mode name here as this.minimal_mode is
// statically replace for server runtimes but we need it to
// be dynamic here
private minimal_mode?: boolean
constructor(minimalMode: boolean) { constructor(minimal_mode: boolean) {
// this is a hack to avoid Webpack knowing this is equal to this.minimalMode this.minimal_mode = minimal_mode
// because we replace this.minimalMode to true in production bundles.
const minimalModeKey = 'minimalMode'
this[minimalModeKey] = minimalMode
} }
public async get( public async get(
...@@ -58,6 +58,7 @@ export default class ResponseCache implements ResponseCacheBase { ...@@ -58,6 +58,7 @@ export default class ResponseCache implements ResponseCacheBase {
incrementalCache: IncrementalResponseCache incrementalCache: IncrementalResponseCache
isRoutePPREnabled?: boolean isRoutePPREnabled?: boolean
isFallback?: boolean isFallback?: boolean
waitUntil?: (prom: Promise<any>) => void
} }
): Promise<ResponseCacheEntry | null> { ): Promise<ResponseCacheEntry | null> {
// If there is no key for the cache, we can't possibly look this up in the // If there is no key for the cache, we can't possibly look this up in the
...@@ -71,130 +72,140 @@ export default class ResponseCache implements ResponseCacheBase { ...@@ -71,130 +72,140 @@ export default class ResponseCache implements ResponseCacheBase {
isOnDemandRevalidate = false, isOnDemandRevalidate = false,
isFallback = false, isFallback = false,
isRoutePPREnabled = false, isRoutePPREnabled = false,
waitUntil,
} = context } = context
const response = await this.batcher.batch( const response = await this.batcher.batch(
{ key, isOnDemandRevalidate }, { key, isOnDemandRevalidate },
async (cacheKey, resolve) => { (cacheKey, resolve) => {
// We keep the previous cache entry around to leverage when the const prom = (async () => {
// incremental cache is disabled in minimal mode. // We keep the previous cache entry around to leverage when the
if ( // incremental cache is disabled in minimal mode.
this.minimalMode && if (
this.previousCacheItem?.key === cacheKey && this.minimal_mode &&
this.previousCacheItem.expiresAt > Date.now() this.previousCacheItem?.key === cacheKey &&
) { this.previousCacheItem.expiresAt > Date.now()
return this.previousCacheItem.entry ) {
} return this.previousCacheItem.entry
}
// Coerce the kindHint into a given kind for the incremental cache.
const kind = routeKindToIncrementalCacheKind(context.routeKind)
let resolved = false // Coerce the kindHint into a given kind for the incremental cache.
let cachedResponse: IncrementalResponseCacheEntry | null = null const kind = routeKindToIncrementalCacheKind(context.routeKind)
try {
cachedResponse = !this.minimalMode let resolved = false
? await incrementalCache.get(key, { let cachedResponse: IncrementalResponseCacheEntry | null = null
kind, try {
isRoutePPREnabled: context.isRoutePPREnabled, cachedResponse = !this.minimal_mode
isFallback, ? await incrementalCache.get(key, {
}) kind,
: null isRoutePPREnabled: context.isRoutePPREnabled,
isFallback,
})
: null
if (cachedResponse && !isOnDemandRevalidate) {
resolve(cachedResponse)
resolved = true
if (!cachedResponse.isStale || context.isPrefetch) {
// The cached value is still valid, so we don't need
// to update it yet.
return null
}
}
if (cachedResponse && !isOnDemandRevalidate) { const cacheEntry = await responseGenerator({
resolve(cachedResponse) hasResolved: resolved,
resolved = true previousCacheEntry: cachedResponse,
isRevalidating: true,
})
if (!cachedResponse.isStale || context.isPrefetch) { // If the cache entry couldn't be generated, we don't want to cache
// The cached value is still valid, so we don't need // the result.
// to update it yet. if (!cacheEntry) {
// Unset the previous cache item if it was set.
if (this.minimal_mode) this.previousCacheItem = undefined
return null return null
} }
}
const cacheEntry = await responseGenerator({ const resolveValue = await fromResponseCacheEntry({
hasResolved: resolved, ...cacheEntry,
previousCacheEntry: cachedResponse, isMiss: !cachedResponse,
isRevalidating: true, })
}) if (!resolveValue) {
// Unset the previous cache item if it was set.
// If the cache entry couldn't be generated, we don't want to cache if (this.minimal_mode) this.previousCacheItem = undefined
// the result. return null
if (!cacheEntry) { }
// Unset the previous cache item if it was set.
if (this.minimalMode) this.previousCacheItem = undefined
return null
}
const resolveValue = await fromResponseCacheEntry({
...cacheEntry,
isMiss: !cachedResponse,
})
if (!resolveValue) {
// Unset the previous cache item if it was set.
if (this.minimalMode) this.previousCacheItem = undefined
return null
}
// For on-demand revalidate wait to resolve until cache is set. // For on-demand revalidate wait to resolve until cache is set.
// Otherwise resolve now. // Otherwise resolve now.
if (!isOnDemandRevalidate && !resolved) { if (!isOnDemandRevalidate && !resolved) {
resolve(resolveValue) resolve(resolveValue)
resolved = true resolved = true
} }
// We want to persist the result only if it has a cache control value // We want to persist the result only if it has a cache control value
// defined. // defined.
if (resolveValue.cacheControl) { if (resolveValue.cacheControl) {
if (this.minimalMode) { if (this.minimal_mode) {
this.previousCacheItem = { this.previousCacheItem = {
key: cacheKey, key: cacheKey,
entry: resolveValue, entry: resolveValue,
expiresAt: Date.now() + 1000, expiresAt: Date.now() + 1000,
}
} else {
await incrementalCache.set(key, resolveValue.value, {
cacheControl: resolveValue.cacheControl,
isRoutePPREnabled,
isFallback,
})
} }
} else { }
await incrementalCache.set(key, resolveValue.value, {
cacheControl: resolveValue.cacheControl, return resolveValue
} catch (err) {
// When a path is erroring we automatically re-set the existing cache
// with new revalidate and expire times to prevent non-stop retrying.
if (cachedResponse?.cacheControl) {
const newRevalidate = Math.min(
Math.max(cachedResponse.cacheControl.revalidate || 3, 3),
30
)
const newExpire =
cachedResponse.cacheControl.expire === undefined
? undefined
: Math.max(
newRevalidate + 3,
cachedResponse.cacheControl.expire
)
await incrementalCache.set(key, cachedResponse.value, {
cacheControl: { revalidate: newRevalidate, expire: newExpire },
isRoutePPREnabled, isRoutePPREnabled,
isFallback, isFallback,
}) })
} }
}
return resolveValue // While revalidating in the background we can't reject as we already
} catch (err) { // resolved the cache entry so log the error here.
// When a path is erroring we automatically re-set the existing cache if (resolved) {
// with new revalidate and expire times to prevent non-stop retrying. console.error(err)
if (cachedResponse?.cacheControl) { return null
const newRevalidate = Math.min( }
Math.max(cachedResponse.cacheControl.revalidate || 3, 3),
30
)
const newExpire =
cachedResponse.cacheControl.expire === undefined
? undefined
: Math.max(
newRevalidate + 3,
cachedResponse.cacheControl.expire
)
await incrementalCache.set(key, cachedResponse.value, {
cacheControl: { revalidate: newRevalidate, expire: newExpire },
isRoutePPREnabled,
isFallback,
})
}
// While revalidating in the background we can't reject as we already // We haven't resolved yet, so let's throw to indicate an error.
// resolved the cache entry so log the error here. throw err
if (resolved) {
console.error(err)
return null
} }
})()
// We haven't resolved yet, so let's throw to indicate an error. // we need to ensure background revalidates are
throw err // passed to waitUntil
if (waitUntil) {
waitUntil(prom)
} }
return prom
} }
) )
......
...@@ -187,6 +187,7 @@ export type ResponseGenerator = (state: { ...@@ -187,6 +187,7 @@ export type ResponseGenerator = (state: {
hasResolved: boolean hasResolved: boolean
previousCacheEntry?: IncrementalResponseCacheEntry | null previousCacheEntry?: IncrementalResponseCacheEntry | null
isRevalidating?: boolean isRevalidating?: boolean
span?: any
}) => Promise<ResponseCacheEntry | null> }) => Promise<ResponseCacheEntry | null>
export const enum IncrementalCacheKind { export const enum IncrementalCacheKind {
......
...@@ -724,6 +724,14 @@ export class AppRouteRouteModule extends RouteModule< ...@@ -724,6 +724,14 @@ export class AppRouteRouteModule extends RouteModule<
case 'force-dynamic': { case 'force-dynamic': {
// Routes of generated paths should be dynamic // Routes of generated paths should be dynamic
workStore.forceDynamic = true workStore.forceDynamic = true
if (workStore.isStaticGeneration) {
const err = new DynamicServerError(
'Route is configured with dynamic = error which cannot be statically generated.'
)
workStore.dynamicUsageDescription = err.message
workStore.dynamicUsageStack = err.stack
throw err
}
break break
} }
case 'force-static': case 'force-static':
......
...@@ -13,7 +13,7 @@ import type { ...@@ -13,7 +13,7 @@ import type {
import type { CacheControl } from './lib/cache-control' import type { CacheControl } from './lib/cache-control'
import { byteLength } from './api-utils/web' import { byteLength } from './api-utils/web'
import BaseServer, { NoFallbackError } from './base-server' import BaseServer from './base-server'
import { generateETag } from './lib/etag' import { generateETag } from './lib/etag'
import { addRequestMeta, getRequestMeta } from './request-meta' import { addRequestMeta, getRequestMeta } from './request-meta'
import WebResponseCache from './response-cache/web' import WebResponseCache from './response-cache/web'
...@@ -34,6 +34,7 @@ import { UNDERSCORE_NOT_FOUND_ROUTE } from '../api/constants' ...@@ -34,6 +34,7 @@ import { UNDERSCORE_NOT_FOUND_ROUTE } from '../api/constants'
import { getEdgeInstrumentationModule } from './web/globals' import { getEdgeInstrumentationModule } from './web/globals'
import type { ServerOnInstrumentationRequestError } from './app-render/types' import type { ServerOnInstrumentationRequestError } from './app-render/types'
import { getEdgePreviewProps } from './web/get-edge-preview-props' import { getEdgePreviewProps } from './web/get-edge-preview-props'
import { NoFallbackError } from '../shared/lib/no-fallback-error.external'
interface WebServerOptions extends Options { interface WebServerOptions extends Options {
buildId: string buildId: string
...@@ -79,7 +80,6 @@ export default class NextWebServer extends BaseServer< ...@@ -79,7 +80,6 @@ export default class NextWebServer extends BaseServer<
return new IncrementalCache({ return new IncrementalCache({
dev, dev,
requestHeaders, requestHeaders,
requestProtocol: 'https',
allowedRevalidateHeaderKeys: allowedRevalidateHeaderKeys:
this.nextConfig.experimental.allowedRevalidateHeaderKeys, this.nextConfig.experimental.allowedRevalidateHeaderKeys,
minimalMode: this.minimalMode, minimalMode: this.minimalMode,
......
...@@ -200,7 +200,6 @@ export async function adapter( ...@@ -200,7 +200,6 @@ export async function adapter(
fetchCacheKeyPrefix: process.env.__NEXT_FETCH_CACHE_KEY_PREFIX, fetchCacheKeyPrefix: process.env.__NEXT_FETCH_CACHE_KEY_PREFIX,
dev: process.env.NODE_ENV === 'development', dev: process.env.NODE_ENV === 'development',
requestHeaders: params.request.headers as any, requestHeaders: params.request.headers as any,
requestProtocol: 'https',
getPrerenderManifest: () => { getPrerenderManifest: () => {
return { return {
version: -1 as any, // letting us know this doesn't conform to spec version: -1 as any, // letting us know this doesn't conform to spec
......
export class NoFallbackError extends Error {
constructor() {
super()
this.message = 'Internal: NoFallbackError'
}
}
...@@ -49,6 +49,8 @@ describe('server-side dev errors', () => { ...@@ -49,6 +49,8 @@ describe('server-side dev errors', () => {
function stripInternalHandler(output) { function stripInternalHandler(output) {
return output return output
.replace(/.*at async handler .*next-route-loader.*/, '') .replace(/.*at async handler .*next-route-loader.*/, '')
.replace(/.*at async handleResponse.*/, '')
.replace(/.*at async doRender \(.*/, '')
.split(/\n/) .split(/\n/)
.filter((item) => !!item.trim()) .filter((item) => !!item.trim())
.join('\n') .join('\n')
......
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