Home / Function/ handle() — astro Function Reference

handle() — astro Function Reference

Architecture documentation for the handle() function in handler.ts from the astro codebase.

Entity Profile

Dependency Diagram

graph TD
  a6b1e1ea_eacf_0569_8504_350c48d33bae["handle()"]
  fe5cee05_3968_5de4_5445_575c0c8017d8["handler.ts"]
  a6b1e1ea_eacf_0569_8504_350c48d33bae -->|defined in| fe5cee05_3968_5de4_5445_575c0c8017d8
  style a6b1e1ea_eacf_0569_8504_350c48d33bae fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

packages/integrations/cloudflare/src/utils/handler.ts lines 29–123

export async function handle(
	request: Request,
	env: Env,
	context: ExecutionContext,
): Promise<CfResponse> {
	// Handle prerender endpoints (only active during build prerender phase)
	if (isPrerender) {
		if (isStaticPathsRequest(request)) {
			return handleStaticPathsRequest(app) as unknown as CfResponse;
		}
		if (isPrerenderRequest(request)) {
			return handlePrerenderRequest(app, request) as unknown as CfResponse;
		}
	}

	const { pathname: requestPathname } = new URL(request.url);

	if (env[sessionKVBindingName]) {
		const sessionConfigOptions = app.manifest.sessionConfig?.options ?? {};
		Object.assign(sessionConfigOptions, {
			binding: env[sessionKVBindingName],
		});
	}

	// static assets fallback, in case default _routes.json is not used
	if (app.manifest.assets.has(requestPathname)) {
		return env.ASSETS.fetch(request.url.replace(/\.html$/, ''));
	}

	let routeData: RouteData | undefined = undefined;
	if (app.isDev()) {
		const result = await app.devMatch(app.getPathnameFromRequest(request));
		if (result) {
			routeData = result.routeData;
		}
	} else {
		routeData = app.match(request);
	}

	if (!routeData) {
		// https://developers.cloudflare.com/pages/functions/api-reference/#envassetsfetch
		const asset = await env.ASSETS.fetch(
			request.url.replace(/index.html$/, '').replace(/\.html$/, ''),
		);
		if (asset.status !== 404) {
			return asset;
		}
	}

	const locals: Runtime = {
		cfContext: context,
	};
	Object.defineProperty(locals, 'runtime', {
		enumerable: false,
		value: {
			get env(): never {
				throw new Error(
					`Astro.locals.runtime.env has been removed in Astro v6. Use 'import { env } from "cloudflare:workers"' instead.`,
				);
			},
			get cf(): never {
				throw new Error(
					`Astro.locals.runtime.cf has been removed in Astro v6. Use 'Astro.request.cf' instead.`,
				);
			},
			get caches(): never {
				throw new Error(
					`Astro.locals.runtime.caches has been removed in Astro v6. Use the global 'caches' object instead.`,
				);
			},
			get ctx(): never {
				throw new Error(
					`Astro.locals.runtime.ctx has been removed in Astro v6. Use 'Astro.locals.cfContext' instead.`,
				);
			},
		},
	});

	const response = await app.render(request, {
		routeData,
		locals,

Domain

Subdomains

Frequently Asked Questions

What does handle() do?
handle() is a function in the astro codebase, defined in packages/integrations/cloudflare/src/utils/handler.ts.
Where is handle() defined?
handle() is defined in packages/integrations/cloudflare/src/utils/handler.ts at line 29.

Analyze Your Own Codebase

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

Try Supermodel Free