Home / Function/ renderComponentToString() — astro Function Reference

renderComponentToString() — astro Function Reference

Architecture documentation for the renderComponentToString() function in component.ts from the astro codebase.

Entity Profile

Dependency Diagram

graph TD
  320325ac_8531_4bc4_5528_be7eec9a3143["renderComponentToString()"]
  8fc97f1b_c914_c155_013d_cbd729fb6b4f["component.ts"]
  320325ac_8531_4bc4_5528_be7eec9a3143 -->|defined in| 8fc97f1b_c914_c155_013d_cbd729fb6b4f
  a1f44e49_de75_0a42_174b_c6fbd80063dd["nonAstroPageNeedsHeadInjection()"]
  320325ac_8531_4bc4_5528_be7eec9a3143 -->|calls| a1f44e49_de75_0a42_174b_c6fbd80063dd
  94f21ea8_f0e7_2f4c_ee40_699f26c9ede0["renderComponent()"]
  320325ac_8531_4bc4_5528_be7eec9a3143 -->|calls| 94f21ea8_f0e7_2f4c_ee40_699f26c9ede0
  style 320325ac_8531_4bc4_5528_be7eec9a3143 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

packages/astro/src/runtime/server/render/component.ts lines 517–573

export async function renderComponentToString(
	result: SSRResult,
	displayName: string,
	Component: unknown,
	props: Record<string | number, any>,
	slots: any = {},
	isPage = false,
	route?: RouteData,
): Promise<string> {
	let str = '';
	let renderedFirstPageChunk = false;

	// Handle head injection if required. Note that this needs to run early so
	// we can ensure getting a value for `head`.
	let head = '';
	if (isPage && !result.partial && nonAstroPageNeedsHeadInjection(Component)) {
		head += chunkToString(result, maybeRenderHead());
	}

	try {
		const destination: RenderDestination = {
			write(chunk) {
				// Automatic doctype and head insertion for pages
				if (isPage && !result.partial && !renderedFirstPageChunk) {
					renderedFirstPageChunk = true;
					if (!/<!doctype html/i.test(String(chunk))) {
						const doctype = result.compressHTML ? '<!DOCTYPE html>' : '<!DOCTYPE html>\n';
						str += doctype + head;
					}
				}

				// `renderToString` doesn't work with emitting responses, so ignore here
				if (chunk instanceof Response) return;

				str += chunkToString(result, chunk);
			},
		};

		const renderInstance = await renderComponent(result, displayName, Component, props, slots);
		if (containsServerDirective(props)) {
			await bufferHeadContent(result);
		}
		await renderInstance.render(destination);
	} catch (e) {
		// We don't have a lot of information downstream, and upstream we can't catch the error properly
		// So let's add the location here
		if (AstroError.is(e) && !e.loc) {
			e.setLocation({
				file: route?.component,
			});
		}

		throw e;
	}

	return str;
}

Domain

Subdomains

Frequently Asked Questions

What does renderComponentToString() do?
renderComponentToString() is a function in the astro codebase, defined in packages/astro/src/runtime/server/render/component.ts.
Where is renderComponentToString() defined?
renderComponentToString() is defined in packages/astro/src/runtime/server/render/component.ts at line 517.
What does renderComponentToString() call?
renderComponentToString() calls 2 function(s): nonAstroPageNeedsHeadInjection, renderComponent.

Analyze Your Own Codebase

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

Try Supermodel Free