Home / File/ render-template.ts — astro Source File

render-template.ts — astro Source File

Architecture documentation for render-template.ts, a typescript file in the astro codebase. 5 imports, 0 dependents.

File typescript CoreAstro RenderingEngine 5 imports 2 functions 1 classes

Entity Profile

Dependency Diagram

graph LR
  88076124_0268_757a_e929_c3261d00db3a["render-template.ts"]
  a7eea85d_ea97_57b1_9a24_f99face6a97d["./escape.js"]
  88076124_0268_757a_e929_c3261d00db3a --> a7eea85d_ea97_57b1_9a24_f99face6a97d
  a28c4b4b_41a8_d771_0fc1_f9fbc7ccded6["../util.js"]
  88076124_0268_757a_e929_c3261d00db3a --> a28c4b4b_41a8_d771_0fc1_f9fbc7ccded6
  6c868e3a_5b34_f101_8dd5_f09c50957ba0["./any.js"]
  88076124_0268_757a_e929_c3261d00db3a --> 6c868e3a_5b34_f101_8dd5_f09c50957ba0
  b0a43ea3_d8cf_4023_b3ca_f683a93e9dbb["./common.js"]
  88076124_0268_757a_e929_c3261d00db3a --> b0a43ea3_d8cf_4023_b3ca_f683a93e9dbb
  f6f0048c_504d_27bf_f669_9d6d4a931037["./util.js"]
  88076124_0268_757a_e929_c3261d00db3a --> f6f0048c_504d_27bf_f669_9d6d4a931037
  style 88076124_0268_757a_e929_c3261d00db3a fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

import { markHTMLString } from '../../escape.js';
import { isPromise } from '../../util.js';
import { renderChild } from '../any.js';
import type { RenderDestination } from '../common.js';
import { createBufferedRenderer } from '../util.js';

const renderTemplateResultSym = Symbol.for('astro.renderTemplateResult');

// The return value when rendering a component.
// This is the result of calling render(), should this be named to RenderResult or...?
export class RenderTemplateResult {
	public [renderTemplateResultSym] = true;
	private htmlParts: TemplateStringsArray;
	public expressions: any[];
	private error: Error | undefined;
	constructor(htmlParts: TemplateStringsArray, expressions: unknown[]) {
		this.htmlParts = htmlParts;
		this.error = undefined;
		this.expressions = expressions.map((expression) => {
			// Wrap Promise expressions so we can catch errors
			// There can only be 1 error that we rethrow from an Astro component,
			// so this keeps track of whether or not we have already done so.
			if (isPromise(expression)) {
				return Promise.resolve(expression).catch((err) => {
					if (!this.error) {
						this.error = err;
						throw err;
					}
				});
			}
			return expression;
		});
	}

	render(destination: RenderDestination): void | Promise<void> {
		// Render all expressions eagerly and in parallel
		const flushers = this.expressions.map((exp) => {
			return createBufferedRenderer(destination, (bufferDestination) => {
				// Skip render if falsy, except the number 0
				if (exp || exp === 0) {
					return renderChild(bufferDestination, exp);
				}
			});
		});

		let i = 0;

		const iterate = (): void | Promise<void> => {
			while (i < this.htmlParts.length) {
				const html = this.htmlParts[i];
				const flusher = flushers[i];

				// increment here due to potential return in
				// Promise scenario
				i++;

				if (html) {
					// only write non-empty strings

					destination.write(markHTMLString(html));
				}

				if (flusher) {
					const result = flusher.flush();

					if (isPromise(result)) {
						return result.then(iterate);
					}
				}
			}
		};

		return iterate();
	}
}

// Determines if a component is an .astro component
export function isRenderTemplateResult(obj: unknown): obj is RenderTemplateResult {
	return typeof obj === 'object' && obj !== null && !!(obj as any)[renderTemplateResultSym];
}

export function renderTemplate(htmlParts: TemplateStringsArray, ...expressions: any[]) {
	return new RenderTemplateResult(htmlParts, expressions);
}

Domain

Subdomains

Dependencies

  • ../util.js
  • ./any.js
  • ./common.js
  • ./escape.js
  • ./util.js

Frequently Asked Questions

What does render-template.ts do?
render-template.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 render-template.ts?
render-template.ts defines 2 function(s): isRenderTemplateResult, renderTemplate.
What does render-template.ts depend on?
render-template.ts imports 5 module(s): ../util.js, ./any.js, ./common.js, ./escape.js, ./util.js.
Where is render-template.ts in the architecture?
render-template.ts is located at packages/astro/src/runtime/server/render/astro/render-template.ts (domain: CoreAstro, subdomain: RenderingEngine, directory: packages/astro/src/runtime/server/render/astro).

Analyze Your Own Codebase

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

Try Supermodel Free