angular-ssr-stream

Type at least two characters. Every page is searched, code included.

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:

  1. Shell. The server bootstraps, waits for the route and its @defer blocks to exist (not for their data) and sends that state, annotated for hydration, with the entry script at the end marked async. The browser loads Angular and hydrates the shell while the response is still open.
  2. Chunks. Each time a request finishes and the application has rendered the result, the server sends every @defer block 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.
  3. 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

The response: nothing sent yet

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/angular-ssr-stream-core The renderer, its inline runtime and the client. Runtime-agnostic: web Request, Response and streams.
@push-based/angular-ssr-stream-angular-ssr withStreaming(): streams an Angular CLI app's server-rendered routes, with @angular/ssr still in charge.
@push-based/angular-ssr-stream-node createNodeHandler(): a Node request handler for node:http, Express, Connect and Koa.
@push-based/angular-ssr-stream-fastify angularStream: a Fastify plugin that streams every page route.
@push-based/angular-ssr-stream-nestjs AngularStreamModule: a Nest module, on Express or on Fastify.
@push-based/angular-ssr-stream-cloudflare createStreamingWorker(): a Cloudflare Worker fetch handler over the ASSETS binding.
@push-based/angular-ssr-stream-aws-lambda createStreamingLambda(): a response-streaming handler for a Lambda Function URL.
@push-based/angular-ssr-stream-nitro 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#

Edit this page on GitHub