Home / Function/ renderToReadableStream() — astro Function Reference

renderToReadableStream() — astro Function Reference

Architecture documentation for the renderToReadableStream() function in render.ts from the astro codebase.

Entity Profile

Dependency Diagram

graph TD
  a1e1182e_4683_0c1e_4777_d9c6023779e8["renderToReadableStream()"]
  e0446a2c_f6ef_c306_3fa2_3d3016b94419["render.ts"]
  a1e1182e_4683_0c1e_4777_d9c6023779e8 -->|defined in| e0446a2c_f6ef_c306_3fa2_3d3016b94419
  531bf937_76da_4b29_be5c_c5f57d933834["callComponentAsTemplateResultOrResponse()"]
  a1e1182e_4683_0c1e_4777_d9c6023779e8 -->|calls| 531bf937_76da_4b29_be5c_c5f57d933834
  a3bddb01_2167_e26f_7cb4_0806d1960ddd["bufferHeadContent()"]
  a1e1182e_4683_0c1e_4777_d9c6023779e8 -->|calls| a3bddb01_2167_e26f_7cb4_0806d1960ddd
  style a1e1182e_4683_0c1e_4777_d9c6023779e8 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

packages/astro/src/runtime/server/render/astro/render.ts lines 69–145

export async function renderToReadableStream(
	result: SSRResult,
	componentFactory: AstroComponentFactory,
	props: any,
	children: any,
	isPage = false,
	route?: RouteData,
): Promise<ReadableStream | Response> {
	const templateResult = await callComponentAsTemplateResultOrResponse(
		result,
		componentFactory,
		props,
		children,
		route,
	);

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

	let renderedFirstPageChunk = false;

	if (isPage) {
		await bufferHeadContent(result);
	}

	return new ReadableStream({
		start(controller) {
			const destination: RenderDestination = {
				write(chunk) {
					// Automatic doctype insertion for pages
					if (isPage && !renderedFirstPageChunk) {
						renderedFirstPageChunk = true;
						if (!result.partial && !DOCTYPE_EXP.test(String(chunk))) {
							const doctype = result.compressHTML ? '<!DOCTYPE html>' : '<!DOCTYPE html>\n';
							controller.enqueue(encoder.encode(doctype));
						}
					}

					// `chunk` might be a Response that contains a redirect,
					// that was rendered eagerly and therefore bypassed the early check
					// whether headers can still be modified. In that case, throw an error
					if (chunk instanceof Response) {
						throw new AstroError({
							...AstroErrorData.ResponseSentError,
						});
					}

					const bytes = chunkToByteArray(result, chunk);
					controller.enqueue(bytes);
				},
			};

			(async () => {
				try {
					await templateResult.render(destination);
					controller.close();
				} 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,
						});
					}

					// Queue error on next microtask to flush the remaining chunks written synchronously
					setTimeout(() => controller.error(e), 0);
				}
			})();
		},
		cancel() {
			// If the client disconnects,
			// we signal to ignore the results of existing renders and avoid kicking off more of them.
			result.cancelled = true;
		},
	});
}

Domain

Subdomains

Frequently Asked Questions

What does renderToReadableStream() do?
renderToReadableStream() is a function in the astro codebase, defined in packages/astro/src/runtime/server/render/astro/render.ts.
Where is renderToReadableStream() defined?
renderToReadableStream() is defined in packages/astro/src/runtime/server/render/astro/render.ts at line 69.
What does renderToReadableStream() call?
renderToReadableStream() calls 2 function(s): bufferHeadContent, callComponentAsTemplateResultOrResponse.

Analyze Your Own Codebase

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

Try Supermodel Free