Home / File/ page.ts — astro Source File

page.ts — astro Source File

Architecture documentation for page.ts, a typescript file in the astro codebase. 8 imports, 0 dependents.

File typescript CoreAstro RenderingEngine 8 imports 1 functions

Entity Profile

Dependency Diagram

graph LR
  cc6ae1bd_6211_1fa2_3099_a5a8ba9a887a["page.ts"]
  10d4e39f_edb6_3e34_aa93_ae1211e7da05["../types/public/internal.js"]
  cc6ae1bd_6211_1fa2_3099_a5a8ba9a887a --> 10d4e39f_edb6_3e34_aa93_ae1211e7da05
  a7387e5d_75cc_9a5e_f39d_a88dfdd799ce["./astro/index.js"]
  cc6ae1bd_6211_1fa2_3099_a5a8ba9a887a --> a7387e5d_75cc_9a5e_f39d_a88dfdd799ce
  8e9a6490_0cf3_4720_cff3_c31ab660bf38["./astro/render.js"]
  cc6ae1bd_6211_1fa2_3099_a5a8ba9a887a --> 8e9a6490_0cf3_4720_cff3_c31ab660bf38
  b0a43ea3_d8cf_4023_b3ca_f683a93e9dbb["./common.js"]
  cc6ae1bd_6211_1fa2_3099_a5a8ba9a887a --> b0a43ea3_d8cf_4023_b3ca_f683a93e9dbb
  3e244973_9bb3_86d6_0270_a57134b6266f["./component.js"]
  cc6ae1bd_6211_1fa2_3099_a5a8ba9a887a --> 3e244973_9bb3_86d6_0270_a57134b6266f
  f53796be_99c4_db53_3b21_19bf1fc24f8a["./csp.js"]
  cc6ae1bd_6211_1fa2_3099_a5a8ba9a887a --> f53796be_99c4_db53_3b21_19bf1fc24f8a
  331fe5de_39c8_462f_a089_e3ee4a1fcd11["./index.js"]
  cc6ae1bd_6211_1fa2_3099_a5a8ba9a887a --> 331fe5de_39c8_462f_a089_e3ee4a1fcd11
  f6f0048c_504d_27bf_f669_9d6d4a931037["./util.js"]
  cc6ae1bd_6211_1fa2_3099_a5a8ba9a887a --> f6f0048c_504d_27bf_f669_9d6d4a931037
  style cc6ae1bd_6211_1fa2_3099_a5a8ba9a887a fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

import type { RouteData, SSRResult } from '../../../types/public/internal.js';
import { isAstroComponentFactory } from './astro/index.js';
import { renderToAsyncIterable, renderToReadableStream, renderToString } from './astro/render.js';
import { encoder } from './common.js';
import { type NonAstroPageComponent, renderComponentToString } from './component.js';
import { renderCspContent } from './csp.js';
import type { AstroComponentFactory } from './index.js';
import { isDeno, isNode } from './util.js';

export async function renderPage(
	result: SSRResult,
	componentFactory: AstroComponentFactory | NonAstroPageComponent,
	props: any,
	children: any,
	streaming: boolean,
	route?: RouteData,
): Promise<Response> {
	if (!isAstroComponentFactory(componentFactory)) {
		result._metadata.headInTree =
			result.componentMetadata.get((componentFactory as any).moduleId)?.containsHead ?? false;

		const pageProps: Record<string, any> = { ...(props ?? {}), 'server:root': true };

		const str = await renderComponentToString(
			result,
			componentFactory.name,
			componentFactory,
			pageProps,
			{},
			true,
			route,
		);

		const bytes = encoder.encode(str);
		const headers = new Headers([
			['Content-Type', 'text/html'],
			['Content-Length', bytes.byteLength.toString()],
		]);
		if (
			result.shouldInjectCspMetaTags &&
			(result.cspDestination === 'header' || result.cspDestination === 'adapter')
		) {
			headers.set('content-security-policy', renderCspContent(result));
		}

		return new Response(bytes, {
			headers,
			status: result.response.status,
		});
	}

	// Mark if this page component contains a <head> within its tree. If it does
	// We avoid implicit head injection entirely.
	result._metadata.headInTree =
		result.componentMetadata.get(componentFactory.moduleId!)?.containsHead ?? false;

	let body: BodyInit | Response;
	if (streaming) {
		// isNode is true in Deno node-compat mode but response construction from
		// async iterables is not supported, so we fallback to ReadableStream if isDeno is true.
		if (isNode && !isDeno) {
			const nodeBody = await renderToAsyncIterable(
				result,
				componentFactory,
				props,
				children,
				true,
				route,
			);
			// Node.js allows passing in an AsyncIterable to the Response constructor.
			// This is non-standard so using `any` here to preserve types everywhere else.
			body = nodeBody as any;
		} else {
			body = await renderToReadableStream(result, componentFactory, props, children, true, route);
		}
	} else {
		body = await renderToString(result, componentFactory, props, children, true, route);
	}

	// If the Astro component returns a Response on init, return that response
	if (body instanceof Response) return body;

	// Create final response from body
	const init = result.response;
	const headers = new Headers(init.headers);
	if (
		(result.shouldInjectCspMetaTags && result.cspDestination === 'header') ||
		result.cspDestination === 'adapter'
	) {
		headers.set('content-security-policy', renderCspContent(result));
	}

	// For non-streaming, convert string to byte array to calculate Content-Length
	if (!streaming && typeof body === 'string') {
		body = encoder.encode(body);
		headers.set('Content-Length', body.byteLength.toString());
	}
	let status = init.status;
	let statusText = init.statusText;
	// Custom 404.astro and 500.astro are particular routes that must return a fixed status code
	if (route?.route === '/404') {
		status = 404;
		if (statusText === 'OK') {
			statusText = 'Not Found';
		}
	} else if (route?.route === '/500') {
		status = 500;
		if (statusText === 'OK') {
			statusText = 'Internal Server Error';
		}
	}

	if (status) {
		return new Response(body, { ...init, headers, status, statusText });
	} else {
		return new Response(body, { ...init, headers });
	}
}

Domain

Subdomains

Functions

Dependencies

  • ../types/public/internal.js
  • ./astro/index.js
  • ./astro/render.js
  • ./common.js
  • ./component.js
  • ./csp.js
  • ./index.js
  • ./util.js

Frequently Asked Questions

What does page.ts do?
page.ts is a source file in the astro codebase, written in typescript. It belongs to the CoreAstro domain, RenderingEngine subdomain.
What functions are defined in page.ts?
page.ts defines 1 function(s): renderPage.
What does page.ts depend on?
page.ts imports 8 module(s): ../types/public/internal.js, ./astro/index.js, ./astro/render.js, ./common.js, ./component.js, ./csp.js, ./index.js, ./util.js.
Where is page.ts in the architecture?
page.ts is located at packages/astro/src/runtime/server/render/page.ts (domain: CoreAstro, subdomain: RenderingEngine, directory: packages/astro/src/runtime/server/render).

Analyze Your Own Codebase

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

Try Supermodel Free