angular-ssr-stream

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

API reference

angular-ssr-stream-angular-ssr

The API of @push-based/angular-ssr-stream-angular-ssr: streaming for @angular/ssr, and the renderer main.server.ts exports.

On this page
bash
npm install @push-based/angular-ssr-stream-core@next @push-based/angular-ssr-stream-angular-ssr@next

The adapter for apps built by the Angular CLI and served by @angular/ssr. It depends on @push-based/angular-ssr-stream-core, whose browser providers the app still needs. Besides Angular's own packages, it needs @angular/ssr and @angular/router, of the same Angular version. For the whole setup, see Angular CLI.

Two entry points, because they end up in two different server bundles: @push-based/angular-ssr-stream-angular-ssr in server.ts's, and @push-based/angular-ssr-stream-angular-ssr/renderer in main.server.ts's.

withStreaming()#

ts
function withStreaming<Engine extends AngularEngine>(engine: Engine): Engine;

/** AngularAppEngine from @angular/ssr, or AngularNodeAppEngine from @angular/ssr/node. */
interface AngularEngine {
  handle(request: never, requestContext?: unknown): Promise<Response | null>;
}

Makes an engine stream the routes it renders on the server, and returns it:

src/server.ts
const angularApp = withStreaming(new AngularNodeAppEngine());

Everything else stays the engine's: the host check, matching the request to a server route, redirects, serving prerendered pages, client-rendered routes and locales. When the matched route's render mode is RenderMode.Server, the page is rendered by the core's streaming renderer, with:

  • the app bootstrapped by main.server.ts's default export, into index.server.html after @angular/ssr's HTML transforms;
  • REQUEST, REQUEST_CONTEXT, RESPONSE_INIT and, for a localized build, LOCALE_ID provided on the platform, as @angular/ssr does;
  • the route's status and headers, read once the shell is ready, so changes the app made to RESPONSE_INIT until then count. Cache-Control: no-store and X-Accel-Buffering: no are added, and Content-Language for a localized build;
  • a redirect instead of the page when the router ended up at another URL: with the route's status if that is a 3xx, 302 otherwise. No response at all (null, so the next middleware runs) when no navigation succeeded;
  • critical CSS inlined into the shell.

When the stream fails before the first byte, the error is logged and the route is rendered by @angular/ssr as it would be without streaming. When the request is aborted, the render stops. An app bootstrapped from an NgModule is rendered by @angular/ssr too, unstreamed: the streaming renderer bootstraps standalone apps only.

withStreaming() throws when it's given anything but one of those two engines, or an engine from an @angular/ssr version it doesn't support.

It reaches into @angular/ssr's internals (getAngularServerAppForRequest() on the engine, handleRendering() on the server app), so like the core it supports a checked range of Angular versions only.

streamingRenderer()#

ts
function streamingRenderer(route: StreamingRouteRender): Promise<Response | null>;

/** One server-rendered route, as withStreaming() hands it over. */
interface StreamingRouteRender {
  /** main.server.ts's default export. */
  bootstrap: StreamingBootstrap;
  /** index.server.html, after @angular/ssr's HTML transforms. */
  document: string;
  request: Request;
  /** The URL to render: the request's, without a trailing index.html. */
  url: URL;
  requestContext?: unknown;
  /** Status and headers from the server route; the app may change them. */
  responseInit: ResponseInit & { headers: Headers };
  locale?: string;
  /** @angular/ssr's critical CSS inlining, applied to the shell. */
  transformDocument?: (document: string) => string | Promise<string>;
}

Renders one server-rendered route as a stream. withStreaming() calls it; an app only exports it from src/main.server.ts:

src/main.server.ts
export { streamingRenderer } from '@push-based/angular-ssr-stream-angular-ssr/renderer';

The Angular CLI bundles server.ts and main.server.ts separately, each with its own copy of Angular, and the app runs on main.server.ts's. Rendering it with the other copy fails with NG0201, because no token of one copy is provided by the other. withStreaming() takes the renderer from main.server.ts's exports for that reason. When it's missing, the server logs a warning once, and every page is rendered without streaming.

Edit this page on GitHub