* Handles serving a prerendered static asset if available for the matched route. * * This method only supports `GET` and `HEAD` requests. * * @param request - The incoming HTTP request for serving a static page. * @param matchedRoute - The metadata of the matched route for rendering.
(
request: Request,
matchedRoute: RouteTreeNodeMetadata,
)
| 227 | * @returns A promise that resolves to a `Response` object if the prerendered page is found, or `null`. |
| 228 | */ |
| 229 | private async handleServe( |
| 230 | request: Request, |
| 231 | matchedRoute: RouteTreeNodeMetadata, |
| 232 | ): Promise<Response | null> { |
| 233 | const { headers, renderMode } = matchedRoute; |
| 234 | if (renderMode !== RenderMode.Prerender) { |
| 235 | return null; |
| 236 | } |
| 237 | |
| 238 | const { method } = request; |
| 239 | if (method !== 'GET' && method !== 'HEAD') { |
| 240 | return null; |
| 241 | } |
| 242 | |
| 243 | const assetPath = this.buildServerAssetPathFromRequest(request); |
| 244 | const { |
| 245 | manifest: { locale }, |
| 246 | assets, |
| 247 | } = this; |
| 248 | |
| 249 | if (!assets.hasServerAsset(assetPath)) { |
| 250 | return null; |
| 251 | } |
| 252 | |
| 253 | const { text, hash, size } = assets.getServerAsset(assetPath); |
| 254 | const etag = `"${hash}"`; |
| 255 | |
| 256 | return request.headers.get('if-none-match') === etag |
| 257 | ? new Response(undefined, { status: 304, statusText: 'Not Modified' }) |
| 258 | : new Response(await text(), { |
| 259 | headers: { |
| 260 | 'Content-Length': size.toString(), |
| 261 | 'ETag': etag, |
| 262 | 'Content-Type': 'text/html;charset=UTF-8', |
| 263 | ...(locale !== undefined ? { 'Content-Language': locale } : {}), |
| 264 | ...headers, |
| 265 | }, |
| 266 | }); |
| 267 | } |
| 268 | |
| 269 | /** |
| 270 | * Handles the server-side rendering process for the given HTTP request. |
no test coverage detected