Home / Function/ createCloudflarePrerenderer() — astro Function Reference

createCloudflarePrerenderer() — astro Function Reference

Architecture documentation for the createCloudflarePrerenderer() function in prerenderer.ts from the astro codebase.

Entity Profile

Dependency Diagram

graph TD
  b3f4b145_93b0_d107_20ee_dc5ed475a6b8["createCloudflarePrerenderer()"]
  ea6368b4_dbad_1087_375b_000717fdb94e["prerenderer.ts"]
  b3f4b145_93b0_d107_20ee_dc5ed475a6b8 -->|defined in| ea6368b4_dbad_1087_375b_000717fdb94e
  style b3f4b145_93b0_d107_20ee_dc5ed475a6b8 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

packages/integrations/cloudflare/src/prerenderer.ts lines 23–119

export function createCloudflarePrerenderer({
	root,
	serverDir,
	clientDir,
	base,
	trailingSlash,
}: CloudflarePrerendererOptions): AstroPrerenderer {
	let previewServer: VitePreviewServer | undefined;
	let serverUrl: string;

	return {
		name: '@astrojs/cloudflare:prerenderer',

		async setup() {
			// Ensure client dir exists (CF plugin expects it for assets)
			await mkdir(clientDir, { recursive: true });

			const cfPluginConfig: PluginConfig = {
				viteEnvironment: { name: 'prerender' },
				config: cloudflareConfigCustomizer(),
			};

			previewServer = await preview({
				configFile: false,
				base,
				appType: 'mpa',
				build: {
					outDir: fileURLToPath(serverDir),
				},
				root: fileURLToPath(root),
				preview: {
					host: 'localhost',
					port: 0, // Let the OS pick a free port
					open: false,
				},
				plugins: [cfVitePlugin(cfPluginConfig)],
			});

			const address = previewServer.httpServer.address();
			if (address && typeof address === 'object') {
				serverUrl = `http://localhost:${address.port}`;
			} else {
				throw new Error(
					'Failed to start the Cloudflare prerender server. The preview server did not return a valid address. ' +
						'This is likely a bug in @astrojs/cloudflare. Please file an issue at https://github.com/withastro/astro/issues',
				);
			}
		},

		async getStaticPaths(): Promise<PathWithRoute[]> {
			// Call the workerd endpoint to get static paths
			const response = await fetch(`${serverUrl}${STATIC_PATHS_ENDPOINT}`, {
				method: 'POST',
				headers: { 'Content-Type': 'application/json' },
			});

			if (!response.ok) {
				throw new Error(
					`Failed to get static paths from the Cloudflare prerender server (${response.status}: ${response.statusText}). ` +
						'This is likely a bug in @astrojs/cloudflare. Please file an issue at https://github.com/withastro/astro/issues',
				);
			}

			const data: StaticPathsResponse = await response.json();

			// Deserialize the routes
			return data.paths.map(({ pathname, route }) => ({
				pathname,
				route: deserializeRouteData(route),
			}));
		},

		async render(request, { routeData }) {
			// Serialize routeData and send to workerd
			const body: PrerenderRequest = {
				url: request.url,
				routeData: serializeRouteData(routeData, trailingSlash),
			};

			const response = await fetch(`${serverUrl}${PRERENDER_ENDPOINT}`, {
				method: 'POST',

Domain

Subdomains

Frequently Asked Questions

What does createCloudflarePrerenderer() do?
createCloudflarePrerenderer() is a function in the astro codebase, defined in packages/integrations/cloudflare/src/prerenderer.ts.
Where is createCloudflarePrerenderer() defined?
createCloudflarePrerenderer() is defined in packages/integrations/cloudflare/src/prerenderer.ts at line 23.

Analyze Your Own Codebase

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

Try Supermodel Free