|
|
import type { AppPageRouteDefinition } from '../../route-definitions/app-page-route-definition' |
|
|
import type RenderResult from '../../render-result' |
|
|
import type { RenderOpts } from '../../app-render/types' |
|
|
import type { NextParsedUrlQuery } from '../../request-meta' |
|
|
import type { LoaderTree } from '../../lib/app-dir-module' |
|
|
import type { PrerenderManifest } from '../../../build' |
|
|
|
|
|
import { |
|
|
renderToHTMLOrFlight, |
|
|
type AppSharedContext, |
|
|
} from '../../app-render/app-render' |
|
|
import { |
|
|
RouteModule, |
|
|
type RouteModuleOptions, |
|
|
type RouteModuleHandleContext, |
|
|
} from '../route-module' |
|
|
import * as vendoredContexts from './vendored/contexts/entrypoints' |
|
|
import type { BaseNextRequest, BaseNextResponse } from '../../base-http' |
|
|
import type { ServerComponentsHmrCache } from '../../response-cache' |
|
|
import type { FallbackRouteParams } from '../../request/fallback-params' |
|
|
import { PrerenderManifestMatcher } from './helpers/prerender-manifest-matcher' |
|
|
import type { DeepReadonly } from '../../../shared/lib/deep-readonly' |
|
|
import { |
|
|
NEXT_ROUTER_PREFETCH_HEADER, |
|
|
NEXT_ROUTER_SEGMENT_PREFETCH_HEADER, |
|
|
NEXT_ROUTER_STATE_TREE_HEADER, |
|
|
NEXT_URL, |
|
|
RSC_HEADER, |
|
|
} from '../../../client/components/app-router-headers' |
|
|
import { isInterceptionRouteAppPath } from '../../../shared/lib/router/utils/interception-routes' |
|
|
|
|
|
let vendoredReactRSC |
|
|
let vendoredReactSSR |
|
|
|
|
|
|
|
|
if (process.env.NEXT_RUNTIME !== 'edge') { |
|
|
vendoredReactRSC = |
|
|
require('./vendored/rsc/entrypoints') as typeof import('./vendored/rsc/entrypoints') |
|
|
vendoredReactSSR = |
|
|
require('./vendored/ssr/entrypoints') as typeof import('./vendored/ssr/entrypoints') |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export type AppPageModule = typeof import('../../../build/templates/app-page') |
|
|
|
|
|
type AppPageUserlandModule = { |
|
|
|
|
|
|
|
|
|
|
|
loaderTree: LoaderTree |
|
|
} |
|
|
|
|
|
export interface AppPageRouteHandlerContext extends RouteModuleHandleContext { |
|
|
page: string |
|
|
query: NextParsedUrlQuery |
|
|
fallbackRouteParams: FallbackRouteParams | null |
|
|
renderOpts: RenderOpts |
|
|
serverComponentsHmrCache?: ServerComponentsHmrCache |
|
|
sharedContext: AppSharedContext |
|
|
} |
|
|
|
|
|
export type AppPageRouteModuleOptions = RouteModuleOptions< |
|
|
AppPageRouteDefinition, |
|
|
AppPageUserlandModule |
|
|
> |
|
|
|
|
|
export class AppPageRouteModule extends RouteModule< |
|
|
AppPageRouteDefinition, |
|
|
AppPageUserlandModule |
|
|
> { |
|
|
constructor( |
|
|
options: RouteModuleOptions<AppPageRouteDefinition, AppPageUserlandModule> |
|
|
) { |
|
|
super(options) |
|
|
this.isAppRouter = true |
|
|
} |
|
|
|
|
|
private matchers = new WeakMap< |
|
|
DeepReadonly<PrerenderManifest>, |
|
|
PrerenderManifestMatcher |
|
|
>() |
|
|
public match( |
|
|
pathname: string, |
|
|
prerenderManifest: DeepReadonly<PrerenderManifest> |
|
|
) { |
|
|
|
|
|
let matcher = this.matchers.get(prerenderManifest) |
|
|
if (!matcher) { |
|
|
matcher = new PrerenderManifestMatcher( |
|
|
this.definition.pathname, |
|
|
prerenderManifest |
|
|
) |
|
|
this.matchers.set(prerenderManifest, matcher) |
|
|
} |
|
|
|
|
|
|
|
|
return matcher.match(pathname) |
|
|
} |
|
|
|
|
|
public render( |
|
|
req: BaseNextRequest, |
|
|
res: BaseNextResponse, |
|
|
context: AppPageRouteHandlerContext |
|
|
): Promise<RenderResult> { |
|
|
return renderToHTMLOrFlight( |
|
|
req, |
|
|
res, |
|
|
context.page, |
|
|
context.query, |
|
|
context.fallbackRouteParams, |
|
|
context.renderOpts, |
|
|
context.serverComponentsHmrCache, |
|
|
false, |
|
|
context.sharedContext |
|
|
) |
|
|
} |
|
|
|
|
|
public warmup( |
|
|
req: BaseNextRequest, |
|
|
res: BaseNextResponse, |
|
|
context: AppPageRouteHandlerContext |
|
|
): Promise<RenderResult> { |
|
|
return renderToHTMLOrFlight( |
|
|
req, |
|
|
res, |
|
|
context.page, |
|
|
context.query, |
|
|
context.fallbackRouteParams, |
|
|
context.renderOpts, |
|
|
context.serverComponentsHmrCache, |
|
|
true, |
|
|
context.sharedContext |
|
|
) |
|
|
} |
|
|
|
|
|
private pathCouldBeIntercepted( |
|
|
resolvedPathname: string, |
|
|
interceptionRoutePatterns: RegExp[] |
|
|
): boolean { |
|
|
return ( |
|
|
isInterceptionRouteAppPath(resolvedPathname) || |
|
|
interceptionRoutePatterns.some((regexp) => { |
|
|
return regexp.test(resolvedPathname) |
|
|
}) |
|
|
) |
|
|
} |
|
|
|
|
|
public getVaryHeader( |
|
|
resolvedPathname: string, |
|
|
interceptionRoutePatterns: RegExp[] |
|
|
): string { |
|
|
const baseVaryHeader = `${RSC_HEADER}, ${NEXT_ROUTER_STATE_TREE_HEADER}, ${NEXT_ROUTER_PREFETCH_HEADER}, ${NEXT_ROUTER_SEGMENT_PREFETCH_HEADER}` |
|
|
|
|
|
if ( |
|
|
this.pathCouldBeIntercepted(resolvedPathname, interceptionRoutePatterns) |
|
|
) { |
|
|
|
|
|
|
|
|
return `${baseVaryHeader}, ${NEXT_URL}` |
|
|
} else { |
|
|
|
|
|
|
|
|
return baseVaryHeader |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
const vendored = { |
|
|
'react-rsc': vendoredReactRSC, |
|
|
'react-ssr': vendoredReactSSR, |
|
|
contexts: vendoredContexts, |
|
|
} |
|
|
|
|
|
export { renderToHTMLOrFlight, vendored } |
|
|
|
|
|
export default AppPageRouteModule |
|
|
|