Runtimes
Angular SSR on Cloudflare Workers, with streaming
Stream an Angular app from a Cloudflare Worker, from the app config to the deploy.
- Packages
@push-based/angular-ssr-stream-core@push-based/angular-ssr-stream-cloudflare- Example
apps/example-cloudflare
On this page
This is the setup both apps in the repository use: Angular compiled by ngp
through Vite, the server bundle running directly in workerd, static assets
served by Cloudflare's ASSETS binding, and every navigation falling through
to the rendering Worker.
Before you start#
This setup builds the app with Vite and
@push-based/,
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:
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.0The 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:
<!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>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#
npm install @push-based/angular-ssr-stream-core@next @push-based/angular-ssr-stream-cloudflare@next
npm install -D @cloudflare/vite-plugin wrangler2. The browser config#
Add the interceptor and the stream's providers next to Angular's hydration:
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#
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 Worker#
import eventDispatchContract from '@angular/core/event-dispatch-contract.min.js?raw';
import { createStreamingWorker } from '@push-based/angular-ssr-stream-cloudflare';
import { App } from './app/app';
import { config } from './app/app.config.server';
export default createStreamingWorker({
rootComponent: App,
config,
eventDispatchContract,
allowedHosts: ['localhost', 'my-app.example.workers.dev'],
}) satisfies ExportedHandler<Env>;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.
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.
The Worker fetches that index.html through the ASSETS binding.
5. Wrangler#
{
"name": "my-app",
"main": "./src/worker.ts",
"compatibility_date": "2026-09-07",
"assets": {
"binding": "ASSETS",
"html_handling": "none",
},
}html_handling: "none" makes the asset server match exact file paths only.
/assets/* and /favicon.ico are served directly, and every navigation falls
through to the Worker, which fetches /index.html through ASSETS and renders
the route into it. index.html is the template, not the page.
6. Vite#
import { cloudflare } from '@cloudflare/vite-plugin';
import { ngp } from '@push-based/vite-plugin-ngp';
import { defineConfig } from 'vite';
export default defineConfig({
optimizeDeps: { entries: ['index.html'] },
plugins: [
ngp({ entries: ['src/main.ts', 'src/worker.ts'] }),
cloudflare({ viteEnvironment: { name: 'ssr' } }),
],
});The Cloudflare plugin owns the ssr environment and runs it in workerd, so
ngp's own ssr option stays off. ngp still compiles the Worker with its server
compiler.
7. Develop#
vite dev # http://localhost:5173The Worker runs in workerd, and the ASSETS binding is backed by the dev
server, so the template already carries Vite's transforms and pages stream as
they do in production.
8. Build and deploy#
vite build # client to dist/client, Worker to dist/ssr
vite preview # the built Worker in workerd, http://localhost:4173
wrangler deploy # deploys the built Workervite build writes dist/ssr/wrangler.json and points wrangler at it through
.wrangler/, so wrangler deploy ships the built Worker
rather than wrangler.jsonc, whose main is TypeScript source.
9. Check that it streams#
With vite preview running:
Request a page and print when each piece of the response arrives:
curl -sN -o /dev/null --trace-time --trace-ascii - http://localhost:4173/ | 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.
After the deploy, run the same command against the Worker's address.
10. Stream something#
Nothing streams until a template has a @defer block with a hydrate
trigger: read Making something stream.