File size: 5,451 Bytes
1e92f2d |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 |
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
// the vendored Reacts are loaded from their original source in the edge runtime
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')
}
/**
* The AppPageModule is the type of the module exported by the bundled app page
* module.
*/
export type AppPageModule = typeof import('../../../build/templates/app-page')
type AppPageUserlandModule = {
/**
* The tree created in next-app-loader that holds component segments and modules
*/
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>
) {
// Lazily create the matcher based on the provided prerender manifest.
let matcher = this.matchers.get(prerenderManifest)
if (!matcher) {
matcher = new PrerenderManifestMatcher(
this.definition.pathname,
prerenderManifest
)
this.matchers.set(prerenderManifest, matcher)
}
// Match the pathname to the dynamic route.
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)
) {
// Interception route responses can vary based on the `Next-URL` header.
// We use the Vary header to signal this behavior to the client to properly cache the response.
return `${baseVaryHeader}, ${NEXT_URL}`
} else {
// We don't need to include `Next-URL` in the Vary header for non-interception routes since it won't affect the response.
// We also set this header for pages to avoid caching issues when navigating between pages and app.
return baseVaryHeader
}
}
}
const vendored = {
'react-rsc': vendoredReactRSC,
'react-ssr': vendoredReactSSR,
contexts: vendoredContexts,
}
export { renderToHTMLOrFlight, vendored }
export default AppPageRouteModule
|