angular-ssr-stream

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

Runtimes

Angular SSR on Nitro, with streaming

Stream an Angular app from a Nitro server, deployed with any of its presets: Node, Vercel, Netlify and more.

Packages
@push-based/angular-ssr-stream-core@push-based/angular-ssr-stream-nitro
On this page

Nitro builds one server for many hosts: the same app goes to Node, Bun, Deno, Vercel, Netlify, Cloudflare and the rest of Nitro's presets by changing one setting. This is the setup apps/example-nitro uses: Angular compiled by ngp through Vite, Nitro's Vite plugin building the server, static files served by Nitro or the host's CDN, and every other request rendered by the streaming handler.

It needs Nitro 3, whose Vite plugin is what builds the server here. Nitro 3 is a release candidate. Nitro numbers its release candidates by date (3.0.261103-rc), and no version range can follow them, so the package asks for exactly the one it was checked with, and moves with each new one. unstorage 2, which Nitro's storage needs, is still a beta. For a single host that has its own adapter, such as Cloudflare Workers, that adapter is lighter.

Before you start#

This setup builds the app with Vite and @push-based/vite-plugin-ngp, not the Angular CLI. For an app made by ng new --ssr that keeps @angular/ssr, follow Angular CLI instead.

ngp needs Node 26 to build, Vite 8.3 or later, TypeScript 6.0, and every @angular/* package at exactly 22.3.0-rc.0, the release it compiles against:

bash
npm install @angular/core@22.3.0-rc.0 @angular/common@22.3.0-rc.0 @angular/compiler@22.3.0-rc.0 \
  @angular/platform-browser@22.3.0-rc.0 @angular/platform-server@22.3.0-rc.0 \
  @angular/router@22.3.0-rc.0 rxjs tslib
npm install -D @angular/compiler-cli@22.3.0-rc.0 @push-based/vite-plugin-ngp vite typescript@~6.0

The app is an ES module ("type": "module" in package.json), with vite/client in its tsconfig.json's types. Vite starts from an index.html that loads src/main.ts, which bootstraps the root component as usual:

index.html
<!doctype html>
<html lang="en">
  <head>
    <meta charset="utf-8" />
    <title>My app</title>
    <base href="/" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />
  </head>
  <body>
    <app-root></app-root>
    <script type="module" src="/src/main.ts"></script>
  </body>
</html>
src/main.ts
import { bootstrapApplication } from '@angular/platform-browser';
import { App } from './app/app';
import { appConfig } from './app/app.config';

bootstrapApplication(App, appConfig).catch((error) => console.error(error));

The steps below call the root component App, in src/app/app.ts, and its routes routes, in src/app/app.routes.ts.

1. Install#

bash
npm install @push-based/angular-ssr-stream-core@next @push-based/angular-ssr-stream-nitro@next unstorage
npm install -D nitro@3.0.261103-rc

unstorage is what Nitro reads server assets through; the handler reads the template that way.

2. The browser config#

Add the interceptor and the stream's providers next to Angular's hydration:

src/app/app.config.ts
import { provideStreamHydration, streamHttpInterceptor } from '@push-based/angular-ssr-stream-core';
import { provideHttpClient, withInterceptors } from '@angular/common/http';
import type { ApplicationConfig } from '@angular/core';
import { provideClientHydration, withEventReplay } from '@angular/platform-browser';
import { provideRouter } from '@angular/router';
import { routes } from './app.routes';

export const appConfig: ApplicationConfig = {
  providers: [
    provideHttpClient(withInterceptors([streamHttpInterceptor])),
    provideRouter(routes),
    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.

3. The server config#

src/app/app.config.server.ts
import { mergeApplicationConfig } from '@angular/core';
import { provideServerRendering } from '@angular/platform-server';
import { appConfig } from './app.config';

export const config = mergeApplicationConfig(appConfig, {
  providers: [provideServerRendering()],
});

4. The handler#

src/server.ts
import eventDispatchContract from '@angular/core/event-dispatch-contract.min.js?raw';
import { defineStreamingHandler } from '@push-based/angular-ssr-stream-nitro';
import { App } from './app/app';
import { config } from './app/app.config.server';

export default defineStreamingHandler({
  rootComponent: App,
  config,
  eventDispatchContract,
  allowedHosts: ['localhost', 'my-app.vercel.app'],
});

This file is Nitro's server entry: Nitro serves a static file when there is one, runs any routes of its own, and hands every other request to the handler, which renders the route into the template.

allowedHosts must list every hostname the server answers for: Angular's SSR host guard rejects any other host header, and the page is answered with 403.

Nitro's server ignores X-Forwarded-Host and X-Forwarded-Proto unless its trustProxy option is on, so behind a proxy the check sees the host header the proxy sends. List preview hostnames too, with a wildcard such as *.vercel.app.

A missing file under /assets/ answers 404; any other path that isn't a file, /nope.png included, is rendered as a page.

eventDispatchContract is for withEventReplay(): it is the script Angular needs to queue the clicks made before the page hydrates. The handler adds it to the template unless the template has it already. The index.server.html ngp writes for a server build has it; the plain built index.html, and the one Vite serves in vite dev, don't. Leave it out where the template lacks it and those clicks are lost; the server logs a warning, once, when the page has listeners to replay.

5. Vite#

vite.config.ts
import { streamingTemplate } from '@push-based/angular-ssr-stream-nitro/vite';
import { ngp } from '@push-based/vite-plugin-ngp';
import { nitro } from 'nitro/vite';
import { defineConfig } from 'vite';

export default defineConfig({
  optimizeDeps: { entries: ['index.html'] },
  plugins: [
    ngp({ entries: ['src/main.ts', 'src/server.ts'] }),
    streamingTemplate(),
    nitro({ serverEntry: './src/server.ts', compatibilityDate: '2026-11-01' }),
  ],
});

Nitro's plugin owns the server build, in an environment of its own, so ngp's ssr option stays off; ngp still compiles src/server.ts and the app it imports with its server compiler.

streamingTemplate() is what makes index.html the template rather than a page. In vite build it takes the built index.html out of the static output, so neither Nitro nor a host's CDN serves it for /, and hands it to Nitro as a server asset, which Nitro bundles into the server for every preset. In vite dev it gives the handler the index.html Vite serves, with Vite's dev transforms, so SSR behaves as it does in production.

6. Develop#

bash
vite dev   # http://localhost:3000

Pages are rendered by the handler and stream as they do in production.

7. Build and run#

bash
vite build                        # node-server preset, to .output/
node .output/server/index.mjs     # http://localhost:3000

vite build builds the browser app into .output/public and the server into .output/server. The node-server output is self-contained: copy .output anywhere Node runs. vite preview serves it too.

8. Check that it streams#

Request a page and print when each piece of the response arrives:

bash
curl -sN -o /dev/null --trace-time --trace-ascii - http://localhost:3000/ | grep 'Recv data'
text
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. Deploy with a preset#

The preset decides what vite build writes. Set it with NITRO_PRESET, or preset in the Nitro options:

bash
NITRO_PRESET=vercel vite build    # .vercel/output, the Build Output API
NITRO_PRESET=netlify vite build   # dist and .netlify/v1/functions
NITRO_PRESET=bun vite build       # bun .output/server/index.mjs

On Vercel and Netlify there is nothing to set: Nitro sees it is building there and picks the preset itself, so a build command of vite build is enough. Nitro's deploy docs cover each provider. Run the check above against the deployment, too.

10. Stream something#

Nothing streams until a template has a @defer block with a hydrate trigger: read Making something stream.

Presets we checked#

Each row was built from apps/example-nitro with that preset and checked with curl -N and in headless Chrome, two page loads each. It streams when the shell arrives at once and the widget's chunk about 2 s later, as its fake API answers.

Preset Run locally with Shell Widget chunk Streams
node-server node .output/server/index.mjs, and vite dev 2 ms 1 980 ms Yes
vercel @vercel/node's dev server (what vercel dev runs) 2 ms 1 975 ms Yes
netlify netlify dev --offline 50–140 ms 2 040–2 130 ms Yes
bun bun .output/server/index.mjs 2 ms 1 985 ms Yes
deno_server deno run on .output/server/index.mjs 2 ms 1 985 ms Yes
cloudflare_module wrangler dev on .output/server 2 ms 1 980 ms Yes

None of these was checked on a real deployment, only with each platform's local runtime. A CDN or proxy in front of the server can still buffer; see Troubleshooting.

Two presets buffer as Nitro ships them, going by their runtime code (not run):

  • aws_lambda returns the whole body in one Lambda result. Nitro's awsLambda: { streaming: true } switches to streamifyResponse, which streams through a Lambda Function URL, or through an API Gateway REST API with response streaming.
  • azure_swa (Azure Static Web Apps) reads the body whole with Functions v3, and with v4 hands it to Azure Functions without turning on enableHttpStream.

Nitro or srvx?#

srvx, from the same ecosystem, is a fetch-style server for Node, Bun and Deno, and it is what Nitro's node-server, bun and deno_server presets run on. On those three runtimes it would stream the same way with less in between. What it doesn't give you is the build: it has no presets and no Vite plugin, so serving the client build, loading the template and bundling the server would be yours to wire up. Use Nitro to reach the hosts it has presets for, and a dedicated adapter where one host matters on its own.

Edit this page on GitHub