Runtimes
Angular CLI
Stream the server-rendered routes of an app made by ng new --ssr, built by ng build and served by @angular/ssr.
- Packages
@push-based/angular-ssr-stream-core@push-based/angular-ssr-stream-angular-ssr- Example
apps/example-angular-cli
On this page
This is the setup apps/example-angular-cli in the repository uses: an app
made by ng new --ssr, built by ng build, and served by @angular/ssr's
AngularNodeAppEngine behind Express. @angular/ssr stays in charge of every
request. The only thing that changes is how a route with RenderMode.Server is
rendered: as a stream, instead of all at once.
Starting from a fresh ng new my-app --ssr, that takes changes to four files,
and a fifth, angular.json, for the hosts it answers on in production.
On Angular 19.2, 20.3 or 21.2, two more apply; see
Angular 19 to 21.
1. Install#
npm install @push-based/angular-ssr-stream-core@next @push-based/angular-ssr-stream-angular-ssr@nextThe core brings the browser providers, the adapter the glue to @angular/ssr.
2. The browser config#
import { provideHttpClient, withFetch, withInterceptors } from '@angular/common/http';
import { ApplicationConfig, provideBrowserGlobalErrorListeners } from '@angular/core';
import { provideClientHydration, withEventReplay } from '@angular/platform-browser';
import { provideRouter } from '@angular/router';
import { provideStreamHydration, streamHttpInterceptor } from '@push-based/angular-ssr-stream-core';
import { routes } from './app.routes';
export const appConfig: ApplicationConfig = {
providers: [
provideBrowserGlobalErrorListeners(),
provideRouter(routes),
provideHttpClient(withFetch(), withInterceptors([streamHttpInterceptor])),
provideClientHydration(withEventReplay()),
// After provideClientHydration(): it replaces a provider from there.
provideStreamHydration(),
],
};streamHttpInterceptor reports each request to the renderer on the server,
and in the browser holds back a request the server is already making until its
response arrives on the stream. provideStreamHydration() keeps Angular's
hydration data and block registry in step with the chunks. On the server it
also brings what the streaming renderer needs from the app's injector.
app.config.server.ts stays as ng new wrote it.
3. The server routes#
ng new prerenders every route. A prerendered page is a file written at build
time, with nothing left to stream, so render the routes that should stream on
the server:
import { RenderMode, ServerRoute } from '@angular/ssr';
export const serverRoutes: ServerRoute[] = [
{
path: '**',
renderMode: RenderMode.Server,
},
];Routes left on RenderMode.Prerender or RenderMode.Client keep working as
before; only RenderMode.Server routes stream.
4. The server#
Wrap the engine in src/server.ts, the one line that changes there:
import { AngularNodeAppEngine } from '@angular/ssr/node';
import { withStreaming } from '@push-based/angular-ssr-stream-angular-ssr';
const angularApp = withStreaming(new AngularNodeAppEngine());And export the renderer from src/main.server.ts:
import { BootstrapContext, bootstrapApplication } from '@angular/platform-browser';
import { App } from './app/app';
import { config } from './app/app.config.server';
const bootstrap = (context: BootstrapContext) => bootstrapApplication(App, config, context);
export default bootstrap;
export { streamingRenderer } from '@push-based/angular-ssr-stream-angular-ssr/renderer';The Angular CLI bundles server.ts and main.server.ts separately, and each
bundle gets its own copy of Angular. The app runs on main.server.ts's copy,
so that is the one the page has to be rendered with: withStreaming() finds
the renderer among main.server.ts's exports. Without the export, the server
logs a warning once and renders every page without streaming.
5. Allowed hosts#
@angular/ssr answers 400 to a Host header it doesn't know, before
anything renders. List your hosts in angular.json, under the build target's
options:
"security": {
"allowedHosts": ["localhost", "my-app.example.com"]
}or at runtime, in NG_ALLOWED_HOSTS (comma-separated).
6. Develop#
ng serve # http://localhost:4200ng serve runs server.ts too, so pages stream in development as they do in
production.
7. Build and run#
ng build
node dist/my-app/server/server.mjs # http://localhost:4000, or $PORT8. Check that it streams#
Before you put anything in front of the server:
Request a page and print when each piece of the response arrives:
curl -sN -o /dev/null --trace-time --trace-ascii - http://localhost:4000/ | grep 'Recv data'07:51:38.138747 <= Recv data, 3557 bytes (0xde5)
07:51:40.145185 <= Recv data, 807 bytes (0x327)The shell comes at once, as one line or a few with the same time. A line that
comes later is a chunk, sent when a @defer block's data was there: here, one
block whose request took two seconds. A page with nothing slow on it arrives
in one piece, so check one that waits for data. If such a page still arrives
all at once, something held the response back until it was complete; see
Checking that a response streams.
9. Behind a proxy#
A proxy, a CDN or compression middleware can hold the response back until it
is complete, and the page then arrives all at once. Streamed responses carry
X-Accel-Buffering: no for Nginx; for the rest, read
Streaming through proxies.
10. Stream something#
Nothing streams until a template has a @defer block with a hydrate
trigger: read Making something stream.
What @angular/ssr still does#
Everything it did before, except render RenderMode.Server routes in one
piece:
- The host check, server routes, redirects, locales, and
RenderMode.PrerenderandRenderMode.Clientroutes, all unchanged. - Status and headers. A route's
statusandheadersapply to the streamed response. So do changes the app makes toRESPONSE_INIT, as long as it makes them before the shell goes out, which is once the route and its@deferblocks are in place: a not-found component that sets the status in its constructor gets its404. - Redirects from the router. When a guard or a
redirectTosends the router elsewhere, the response is a redirect, as with@angular/ssralone. REQUEST,REQUEST_CONTEXTandRESPONSE_INITare provided as@angular/ssrprovides them.- Critical CSS is inlined into the shell.
- If streaming fails before the first byte is sent, the route is rendered
by
@angular/ssras usual, and the error is logged.
Streamed responses carry Cache-Control: no-store, whatever the route's
headers say: a cached copy of a partial response would be worse than none.
Angular 19 to 21#
The steps above are for Angular 22, which ng new gives you today. On 19.2,
20.3 or 21.2, check the patch against the
compatibility table, then:
- Turn on incremental hydration. Angular 22 does it by default; before
that, add
withIncrementalHydration()from@angular/toplatform-browser provideClientHydration(). Without it nothing streams, and the server warns. - Check
main.server.ts. Itsbootstraphas to take theBootstrapContextit is called with and pass it tobootstrapApplication(), as in step 4.ng newwrites it that way in every supported version, but an app made before Angular's security fix that added it may still have abootstrapthat takes no argument: add it. - On 19, zoneless is
provideExperimentalZonelessChangeDetection(). Zone.js works as well.