Getting started
Introduction: streaming SSR for Angular
What streamed hydration is: one response, sent in pieces, hydrated while it is still arriving.
On this page
Angular renders a page on the server and sends it whole, so the page is as slow as its slowest request. angular-ssr-stream sends it in pieces instead, over one response:
- Shell. The server bootstraps, waits for the route and its
@deferblocks to exist (not for their data) and sends that state, annotated for hydration, with the entry script at the end markedasync. The browser loads Angular and hydrates the shell while the response is still open. - Chunks. Each time a request finishes and the application has rendered
the result, the server sends every
@deferblock that changed: its markup in a<template>, then an inline script carrying the block's hydration data and the transfer state the change came from. The script swaps the markup in front of the block's anchor comment, whether or not Angular is running yet. - Hydration. The block hydrates against the streamed markup when its trigger fires. Nothing is fetched twice: a request the server is already making is held back in the browser until its response arrives on the stream.
The page is usable as soon as its shell is, and each slow part fills in when its own data is there, in whatever order that happens.
One page load, step by step#
There is no second request and no fetching from the browser. The server keeps the response open and adds to it. Step through one page load. Its two slow parts arrive in the order their data does, not the order of the page, and each one hydrates on its own:
The browser asks for the page
The server starts rendering. The page has two parts that wait for an API: recommendations, which take 3 seconds, and below them reviews, which take 1. Nothing has been sent yet.
What the server has sent
Nothing has arrived yet.
arrived in this step
What the browser shows
Blank page
The page and its response are made up, and cut down to what does the work. One response, flush by flush takes a real one apart.
See it#
- The demo is streaming as it happens: a widget that arrives five seconds after its page, timed as it lands and as it hydrates, four blocks with a log of each step, and every flush of a real response as it arrives.
- Angular Movies is the same technique on a real app, backed by the TMDB API.
The packages#
| Package | What it is |
|---|---|
@push-based/ |
The renderer, its inline runtime and the client. Runtime-agnostic: web Request, Response and streams. |
@push-based/ |
withStreaming(): streams an Angular CLI app's server-rendered routes, with @angular/ssr still in charge. |
@push-based/ |
createNodeHandler(): a Node request handler for node:http, Express, Connect and Koa. |
@push-based/ |
angularStream: a Fastify plugin that streams every page route. |
@push-based/ |
AngularStreamModule: a Nest module, on Express or on Fastify. |
@push-based/ |
createStreamingWorker(): a Cloudflare Worker fetch handler over the ASSETS binding. |
@push-based/ |
createStreamingLambda(): a response-streaming handler for a Lambda Function URL. |
@push-based/ |
defineStreamingHandler(): an h3 handler for a Nitro server. |
An app uses both halves of the core: providers in its browser config, and a
server entry that streams. The server entry is where runtimes differ. A runtime
that hands the server a web Request and takes a Response back (Hono, Deno,
Bun, Fastly Compute, Vercel, Netlify, Azure Functions) needs only the core's
createStreamingHandler(). The
others get a small adapter over it, which is what the rest of the packages are.
An Angular CLI app keeps @angular/ssr as its server and adds
withStreaming() to its engine.
Where to go next#
- Installation: what the packages need, and which of them your runtime takes.
- Your runtime's setup page, from the table there or under "Runtimes" in the contents: the whole setup, from the app config to running it.
- Making something stream: which parts of a template stream, and how to write them.
- Why stream out of order: what it buys, and how other frameworks came to it.
- One response, flush by flush: a real page's response, and what each piece of it carries.