The Server-Side Rendering (SSR) Runtime in Astro is responsible for processing every incoming HTTP request into a fully rendered HTTP response. This involves a structured pipeline that includes middleware execution, component rendering, and response generation, while abstracting away environment-specific details for adaptability across Node.js, Deno, and edge environments.
This page offers an in-depth technical overview of key runtime abstractions—RenderContext, SSRResult, and SSRManifest—and details the entire request processing pipeline, covering middleware orchestration, component rendering (including hydration and JSX rendering), slot and head management, and the flexible handler pipeline system.
Sources: packages/astro/src/core/app/types.ts63-148 packages/astro/src/core/base-pipeline.ts67-120
The SSR runtime architecture is organized into multiple interconnected layers, progressing from receiving the raw HTTP request to producing the final HTTP response.
FetchState class encapsulates the state for an individual request, including cookies, locals, and route parameters packages/astro/src/core/fetch/fetch-state.ts129-176SSRManifest provides static application configuration used for route matching, assets, and renderers packages/astro/src/core/app/types.ts63-148SSRResult manages the hydration, styling, and scripting metadata during rendering packages/astro/src/types/public/internal.ts11-15renderPage packages/astro/src/runtime/server/render/page.ts12-131Sources: packages/astro/src/runtime/server/render/page.ts12-131 packages/astro/src/core/fetch/fetch-state.ts129-176 packages/astro/src/core/base-pipeline.ts126-194
Astro's SSR runtime is divided between static application configuration (the Pipeline) and per-request orchestration (the FetchState).
Pipeline holds immutable configuration for the app:
manifest: SSRManifest — the manifest generated at build time containing routes, assets, etc packages/astro/src/core/app/types.ts63-148renderers: SSRLoadedRenderer[] — array of framework renderers (React, Vue, Solid, etc.) packages/astro/src/types/public/internal.ts13middleware — composed middleware chain to run for each request packages/astro/src/core/app/types.ts106FetchState manages the current request lifecycle:
request — the standard Request object for the incoming HTTP request packages/astro/src/core/fetch/fetch-state.ts135routeData — metadata about the matched route packages/astro/src/core/fetch/fetch-state.ts136locals — per-request state shared across middleware and rendering phases packages/astro/src/core/fetch/fetch-state.ts85cookies — parsed cookie interface for reading/modifying HTTP cookies packages/astro/src/core/fetch/fetch-state.ts83Sources: packages/astro/src/core/fetch/fetch-state.ts73-176 packages/astro/src/core/app/types.ts63-148
SSRResult is the rendering context object responsible for collecting and managing all metadata related to rendering a single page. It is passed deeply through the render tree as components render.
Key responsibilities:
Sources: packages/astro/src/runtime/server/render/component.ts89-106 packages/astro/src/runtime/server/render/page.ts63-68
The SSRManifest is a static object loaded once at server startup (or bundled at build time). It includes:
routes) packages/astro/src/core/app/types.ts65renderers) packages/astro/src/core/app/types.ts79assets, inlinedScripts) packages/astro/src/core/app/types.ts98-99csp) packages/astro/src/core/app/types.ts125i18n) packages/astro/src/core/app/types.ts105Sources: packages/astro/src/core/app/types.ts63-148
Astro supports native Astro components, framework components (React, Vue, etc.), and raw HTML/Server Islands.
Astro implements an island architecture that allows framework components to be hydrated on the client.
check hook on configured renderers to find one that can handle the component packages/astro/src/runtime/server/render/component.ts132-137extractDirectives parses client:* attributes to determine the hydration strategy packages/astro/src/runtime/server/render/component.ts94-97client: directive is present, metadata is populated to inject the <astro-island> custom element and its corresponding hydration script packages/astro/src/runtime/server/render/component.ts101-106Sources: packages/astro/src/runtime/server/render/component.ts75-192 packages/astro/src/runtime/server/hydration.ts10-11
The runtime supports several rendering strategies in renderPage:
| Mode | Implementation | Description |
|---|---|---|
| Streaming (Node.js) | renderToAsyncIterable | Yields chunks of HTML as an AsyncIterable for Node.js response construction packages/astro/src/runtime/server/render/page.ts75-85 |
| Streaming (Web) | renderToReadableStream | Uses ReadableStream for Deno/Edge environments packages/astro/src/runtime/server/render/page.ts87 |
| Buffered | renderToString | Renders the entire page into a string before sending packages/astro/src/runtime/server/render/page.ts90 |
Sources: packages/astro/src/runtime/server/render/page.ts71-91
Server Islands provide deferred rendering for components marked with server:defer.
containsServerDirective identifies components that should be deferred packages/astro/src/runtime/server/render/component.ts28RenderInstruction to propagate metadata about deferred islands up the tree packages/astro/src/runtime/server/render/instruction.ts1-5templateEnter and templateExit track nesting depth during rendering to manage scoped content packages/astro/src/runtime/server/index.ts46Sources: packages/astro/src/runtime/server/render/component.ts28 packages/astro/src/runtime/server/render/server-islands.ts1-28
renderJSX, which associates components with their specific renderers packages/astro/src/runtime/server/index.ts15renderSlot and renderSlots. Slots are merged using mergeSlots to handle multiple children passed to a component packages/astro/src/runtime/server/index.ts52-63<astro-slot> and <astro-static-slot> for efficient HTML output packages/astro/src/runtime/server/render/component.ts67-73Sources: packages/astro/src/runtime/server/index.ts52-63 packages/astro/src/runtime/server/render/slot.ts1-30
Astro manages the <head> section by buffering content:
<head> in its tree (headInTree) packages/astro/src/runtime/server/render/page.ts61-64renderHead and maybeRenderHead are used to flush buffered head content into the HTML stream packages/astro/src/runtime/server/index.ts28-31Sources: packages/astro/src/runtime/server/render/page.ts61-68 packages/astro/src/runtime/server/render/head.ts1-10
The request lifecycle is governed by a pipeline that coordinates middleware and response generation:
locals or return a Response early packages/astro/src/core/base-pipeline.ts126-194baseMiddleware, trailingSlashMiddleware, routeGuardMiddleware) is injected into the stack to handle assets and routing before the Astro SSR handler packages/astro/src/vite-plugin-astro-server/plugin.ts121-138recordServerError and setRouteError capture runtime failures and format them for the Astro error overlay in dev mode packages/astro/src/vite-plugin-astro-server/plugin.ts93-98Sources: packages/astro/src/vite-plugin-astro-server/plugin.ts78-105 packages/astro/src/core/base-pipeline.ts126-194
Refresh this wiki
This wiki was recently refreshed. Please wait 6 days to refresh again.