Home / Function/ createRequest() — astro Function Reference

createRequest() — astro Function Reference

Architecture documentation for the createRequest() function in request.ts from the astro codebase.

Entity Profile

Dependency Diagram

graph TD
  66ed48fd_7dab_0574_1bf7_19ef3f6d795d["createRequest()"]
  efdcd621_1ac1_b986_346b_ad401214e3a2["request.ts"]
  66ed48fd_7dab_0574_1bf7_19ef3f6d795d -->|defined in| efdcd621_1ac1_b986_346b_ad401214e3a2
  style 66ed48fd_7dab_0574_1bf7_19ef3f6d795d fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

packages/astro/src/core/request.ts lines 35–97

export function createRequest({
	url,
	headers,
	method = 'GET',
	body = undefined,
	logger,
	isPrerendered = false,
	routePattern,
	init,
}: CreateRequestOptions): Request {
	// headers are made available on the created request only if the request is for a page that will be on-demand rendered
	const headersObj = isPrerendered
		? undefined
		: headers instanceof Headers
			? headers
			: new Headers(
					// Filter out HTTP/2 pseudo-headers. These are internally-generated headers added to all HTTP/2 requests with trusted metadata about the request.
					// Examples include `:method`, `:scheme`, `:authority`, and `:path`.
					// They are always prefixed with a colon to distinguish them from other headers, and it is an error to add the to a Headers object manually.
					// See https://httpwg.org/specs/rfc7540.html#HttpRequest
					Object.entries(headers as Record<string, any>).filter(([name]) => !name.startsWith(':')),
				);

	if (typeof url === 'string') url = new URL(url);

	// Remove search parameters if the request is for a page that will be on-demand rendered
	if (isPrerendered) {
		url.search = '';
	}

	const request = new Request(url, {
		method: method,
		headers: headersObj,
		// body is made available only if the request is for a page that will be on-demand rendered
		body: isPrerendered ? null : body,
		...init,
	});

	if (isPrerendered) {
		// Warn when accessing headers in SSG mode
		let _headers = request.headers;

		// We need to remove descriptor's value and writable properties because we're adding getters and setters.
		const { value, writable, ...headersDesc } =
			Object.getOwnPropertyDescriptor(request, 'headers') || {};

		Object.defineProperty(request, 'headers', {
			...headersDesc,
			get() {
				logger.warn(
					null,
					`\`Astro.request.headers\` was used when rendering the route \`${routePattern}'\`. \`Astro.request.headers\` is not available on prerendered pages. If you need access to request headers, make sure that the page is server-rendered using \`export const prerender = false;\` or by setting \`output\` to \`"server"\` in your Astro config to make all your pages server-rendered by default.`,
				);
				return _headers;
			},
			set(newHeaders: Headers) {
				_headers = newHeaders;
			},
		});
	}

	return request;
}

Domain

Subdomains

Frequently Asked Questions

What does createRequest() do?
createRequest() is a function in the astro codebase, defined in packages/astro/src/core/request.ts.
Where is createRequest() defined?
createRequest() is defined in packages/astro/src/core/request.ts at line 35.

Analyze Your Own Codebase

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

Try Supermodel Free