Home / File/ prerender.ts — astro Source File

prerender.ts — astro Source File

Architecture documentation for prerender.ts, a typescript file in the astro codebase. 5 imports, 0 dependents.

File typescript CoreAstro RenderingEngine 5 imports 4 functions

Entity Profile

Dependency Diagram

graph LR
  48b50da0_daf1_dfed_c56f_65e6f20889c9["prerender.ts"]
  88f8942a_0ab8_2f50_3bcb_dd20bdb8a6be["../prerender-types.js"]
  48b50da0_daf1_dfed_c56f_65e6f20889c9 --> 88f8942a_0ab8_2f50_3bcb_dd20bdb8a6be
  eaea7968_cb26_203a_633b_da660ec7d738["./prerender-constants.js"]
  48b50da0_daf1_dfed_c56f_65e6f20889c9 --> eaea7968_cb26_203a_633b_da660ec7d738
  7f3c1bdb_3a0f_c4d2_17d3_31a407595fc8["app"]
  48b50da0_daf1_dfed_c56f_65e6f20889c9 --> 7f3c1bdb_3a0f_c4d2_17d3_31a407595fc8
  0875b428_b364_946e_8628_525c58afbb71["manifest"]
  48b50da0_daf1_dfed_c56f_65e6f20889c9 --> 0875b428_b364_946e_8628_525c58afbb71
  c07dbc11_5d12_f9b9_142f_43f4327ad9b8["astro:static-paths"]
  48b50da0_daf1_dfed_c56f_65e6f20889c9 --> c07dbc11_5d12_f9b9_142f_43f4327ad9b8
  style 48b50da0_daf1_dfed_c56f_65e6f20889c9 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

/**
 * Prerender utilities for Cloudflare adapter
 *
 * During the build process, Astro prerenders pages by making requests to internal endpoints
 * served by the Cloudflare worker running in workerd. These endpoints are:
 *
 * - `/__astro_static_paths`: Returns all static paths that need to be prerendered.
 *   The prerenderer calls this to discover which routes/pages need to be generated.
 *
 * - `/__astro_prerender`: Renders a specific page given its URL and route data.
 *   The prerenderer calls this for each path to generate the static HTML.
 *
 * These endpoints are only active during the prerender build phase and are not
 * available in production or development.
 */

import type { BaseApp } from 'astro/app';
import { serializeRouteData, deserializeRouteData } from 'astro/app/manifest';
import { StaticPaths } from 'astro:static-paths';
import type { StaticPathsResponse, PrerenderRequest } from '../prerender-types.js';
import { STATIC_PATHS_ENDPOINT, PRERENDER_ENDPOINT } from './prerender-constants.js';

/**
 * Checks if the request is for the static paths prerender endpoint.
 * This endpoint returns all paths that need to be prerendered.
 */
export function isStaticPathsRequest(request: Request): boolean {
	const { pathname } = new URL(request.url);
	return pathname === STATIC_PATHS_ENDPOINT && request.method === 'POST';
}

/**
 * Checks if the request is for the prerender endpoint.
 * This endpoint renders a specific page during the prerender phase.
 */
export function isPrerenderRequest(request: Request): boolean {
	const { pathname } = new URL(request.url);
	return pathname === PRERENDER_ENDPOINT && request.method === 'POST';
}

/**
 * Handles the static paths request, returning all paths that need prerendering.
 */
export async function handleStaticPathsRequest(app: BaseApp): Promise<Response> {
	const staticPaths = new StaticPaths(app);
	const paths = await staticPaths.getAll();
	const response: StaticPathsResponse = {
		paths: paths.map(({ pathname, route }) => ({
			pathname,
			route: serializeRouteData(route, app.manifest.trailingSlash),
		})),
	};
	return new Response(JSON.stringify(response), {
		headers: { 'Content-Type': 'application/json' },
	});
}

/**
 * Handles a prerender request, rendering the specified page.
 */
export async function handlePrerenderRequest(app: BaseApp, request: Request): Promise<Response> {
	const headers = new Headers();
	for (const [key, value] of request.headers.entries()) {
		headers.append(key, value);
	}
	const body: PrerenderRequest = await request.json();
	const routeData = deserializeRouteData(body.routeData);
	const prerenderRequest = new Request(body.url, {
		method: 'GET',
		headers,
	});
	return app.render(prerenderRequest, { routeData });
}

Domain

Subdomains

Dependencies

  • ../prerender-types.js
  • ./prerender-constants.js
  • app
  • astro:static-paths
  • manifest

Frequently Asked Questions

What does prerender.ts do?
prerender.ts is a source file in the astro codebase, written in typescript. It belongs to the CoreAstro domain, RenderingEngine subdomain.
What functions are defined in prerender.ts?
prerender.ts defines 4 function(s): handlePrerenderRequest, handleStaticPathsRequest, isPrerenderRequest, isStaticPathsRequest.
What does prerender.ts depend on?
prerender.ts imports 5 module(s): ../prerender-types.js, ./prerender-constants.js, app, astro:static-paths, manifest.
Where is prerender.ts in the architecture?
prerender.ts is located at packages/integrations/cloudflare/src/utils/prerender.ts (domain: CoreAstro, subdomain: RenderingEngine, directory: packages/integrations/cloudflare/src/utils).

Analyze Your Own Codebase

Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.

Try Supermodel Free