Home / Function/ createHandler() — astro Function Reference

createHandler() — astro Function Reference

Architecture documentation for the createHandler() function in ssr-function.ts from the astro codebase.

Entity Profile

Dependency Diagram

graph TD
  2b562074_82e4_36dc_c9c0_49ee958ebf2d["createHandler()"]
  9db13972_7aec_5732_70c9_a18fc4679303["ssr-function.ts"]
  2b562074_82e4_36dc_c9c0_49ee958ebf2d -->|defined in| 9db13972_7aec_5732_70c9_a18fc4679303
  style 2b562074_82e4_36dc_c9c0_49ee958ebf2d fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

packages/integrations/netlify/src/ssr-function.ts lines 10–66

export function createHandler({ notFoundContent }: { notFoundContent: string | undefined }) {
	return async function handler(request: Request, context: Context): Promise<Response> {
		const routeData = app.match(request);

		if (!routeData && typeof notFoundContent !== 'undefined') {
			return new Response(notFoundContent, {
				status: 404,
				headers: { 'Content-Type': 'text/html; charset=utf-8' },
			});
		}

		let locals: Record<string, unknown> = {};

		const astroLocalsHeader = request.headers.get('x-astro-locals');
		const middlewareSecretHeader = request.headers.get('x-astro-middleware-secret');
		if (astroLocalsHeader) {
			if (middlewareSecretHeader !== middlewareSecret) {
				return new Response('Forbidden', { status: 403 });
			}
			// hide the secret from the rest of user and library code
			request.headers.delete('x-astro-middleware-secret');
			locals = JSON.parse(astroLocalsHeader);
		}

		locals.netlify = { context };

		const response = await app.render(request, {
			routeData,
			locals,
			clientAddress: context.ip,
		});

		if (app.setCookieHeaders) {
			for (const setCookieHeader of app.setCookieHeaders(response)) {
				response.headers.append('Set-Cookie', setCookieHeader);
			}
		}

		if (cacheOnDemandPages) {
			const isCacheableMethod = ['GET', 'HEAD'].includes(request.method);

			// any user-provided Cache-Control headers take precedence
			const hasCacheControl = [
				'Cache-Control',
				'CDN-Cache-Control',
				'Netlify-CDN-Cache-Control',
			].some((header) => response.headers.has(header));

			if (isCacheableMethod && !hasCacheControl) {
				// caches this page for up to a year
				response.headers.append('CDN-Cache-Control', 'public, max-age=31536000, must-revalidate');
			}
		}

		return response;
	};
}

Domain

Subdomains

Frequently Asked Questions

What does createHandler() do?
createHandler() is a function in the astro codebase, defined in packages/integrations/netlify/src/ssr-function.ts.
Where is createHandler() defined?
createHandler() is defined in packages/integrations/netlify/src/ssr-function.ts at line 10.

Analyze Your Own Codebase

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

Try Supermodel Free