Home / Function/ handleRequest() — astro Function Reference

handleRequest() — astro Function Reference

Architecture documentation for the handleRequest() function in app.ts from the astro codebase.

Entity Profile

Dependency Diagram

graph TD
  752a8772_d13c_3672_c3b6_a2fcc170d976["handleRequest()"]
  df0b0a04_8315_c9cf_d541_013f247434eb["AstroServerApp"]
  752a8772_d13c_3672_c3b6_a2fcc170d976 -->|defined in| df0b0a04_8315_c9cf_d541_013f247434eb
  2bb6776f_f585_35a0_c0da_3d9cf1f798df["devMatch()"]
  752a8772_d13c_3672_c3b6_a2fcc170d976 -->|calls| 2bb6776f_f585_35a0_c0da_3d9cf1f798df
  style 752a8772_d13c_3672_c3b6_a2fcc170d976 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

packages/astro/src/vite-plugin-app/app.ts lines 120–220

	public async handleRequest({
		controller,
		incomingRequest,
		incomingResponse,
		isHttps,
	}: HandleRequest): Promise<void> {
		const origin = `${isHttps ? 'https' : 'http'}://${
			incomingRequest.headers[':authority'] ?? incomingRequest.headers.host
		}`;

		const url = new URL(origin + incomingRequest.url);
		let pathname: string;
		if (this.manifest.trailingSlash === 'never' && !incomingRequest.url) {
			pathname = '';
		} else {
			// We already have a middleware that checks if there's an incoming URL that has invalid URI, so it's safe
			// to not handle the error: packages/astro/src/vite-plugin-astro-server/base.ts
			pathname = decodeURI(url.pathname);
		}

		// Normalize root path to empty string when trailingSlash is 'never' and there's a non-root base
		// This ensures consistent route matching for the index route (e.g., /base?query -> '')
		if (this.manifest.trailingSlash === 'never' && pathname === '/' && this.manifest.base !== '/') {
			pathname = '';
		}

		// Add config.base back to url before passing it to SSR
		url.pathname = removeTrailingForwardSlash(this.manifest.base) + url.pathname;
		if (
			url.pathname.endsWith('/') &&
			!shouldAppendForwardSlash(this.manifest.trailingSlash, this.manifest.buildFormat)
		) {
			url.pathname = url.pathname.slice(0, -1);
		}

		let body: BodyInit | undefined = undefined;
		if (!(incomingRequest.method === 'GET' || incomingRequest.method === 'HEAD')) {
			let bytes: Uint8Array[] = [];
			await new Promise((resolve) => {
				incomingRequest.on('data', (part) => {
					bytes.push(part);
				});
				incomingRequest.on('end', resolve);
			});
			body = Buffer.concat(bytes);
		}

		const self = this;
		await runWithErrorHandling({
			controller,
			pathname,
			async run() {
				const matchedRoute = await self.devMatch(pathname);
				if (!matchedRoute) {
					// This should never happen, because ensure404Route will add a 404 route if none exists.
					throw new Error('No route matched, and default 404 route was not found.');
				}

				self.resolvedPathname = matchedRoute.resolvedPathname;
				const request = createRequest({
					url,
					headers: incomingRequest.headers,
					method: incomingRequest.method,
					body,
					logger: self.logger,
					isPrerendered: matchedRoute.routeData.prerender,
					routePattern: matchedRoute.routeData.component,
				});

				// This is required for adapters to set locals in dev mode. They use a dev server middleware to inject locals to the `http.IncomingRequest` object.
				const locals = Reflect.get(incomingRequest, clientLocalsSymbol);

				// Set user specified headers to response object.
				for (const [name, value] of Object.entries(self.settings.config.server.headers ?? {})) {
					if (value) incomingResponse.setHeader(name, value);
				}
				const clientAddress = incomingRequest.socket.remoteAddress;

				const response = await self.render(request, {
					locals,
					routeData: matchedRoute.routeData,

Domain

Subdomains

Calls

Frequently Asked Questions

What does handleRequest() do?
handleRequest() is a function in the astro codebase, defined in packages/astro/src/vite-plugin-app/app.ts.
Where is handleRequest() defined?
handleRequest() is defined in packages/astro/src/vite-plugin-app/app.ts at line 120.
What does handleRequest() call?
handleRequest() calls 1 function(s): devMatch.

Analyze Your Own Codebase

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

Try Supermodel Free