Runtimes
Angular SSR on Vercel, with streaming
Stream an Angular app from a Vercel Function, from the app config to the Build Output Vercel deploys.
- Packages
@push-based/angular-ssr-stream-coreand no adapter- Example
apps/example-vercel
On this page
This is the setup apps/example-vercel uses: Angular compiled by ngp through
Vite, the server bundle running as one Vercel Function on the Node.js runtime,
static assets served by Vercel, and every navigation routed to the function.
There is no adapter package: the core's createStreamingHandler() is already a
web-standard handler, which is what a Vercel Function exports. The Vite build
writes the deployment itself, in the layout of Vercel's
Build Output API.
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
npm install -D @types/node vercel2. 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 function#
import { readFile } from 'node:fs/promises';
import { createStreamingHandler } from '@push-based/angular-ssr-stream-core/server';
import { App } from './app/app';
import { config } from './app/app.config.server';
const template = readFile(new URL('./index.server.html', import.meta.url), 'utf8');
const handle = createStreamingHandler({
rootComponent: App,
config,
allowedHosts: ['localhost', 'my-app.vercel.app'],
loadTemplate: async () =>
new Response(await template, { headers: { 'content-type': 'text/html;charset=utf-8' } }),
});
export default {
fetch: (request: Request) => handle(request),
};A Vercel Function on the Node.js runtime may export { fetch }, and Vercel
streams the Response it returns. Every request method reaches fetch.
The template is index.server.html, which ngp's ssr build writes next to the
server bundle: the built index.html with the script withEventReplay() needs,
so the handler takes no eventDispatchContract. The build below copies it into
the function, and the function reads it once per instance.
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.
Each preview deployment has its own *.vercel.app hostname; '*.vercel.app'
admits all of them, and the example lists that.
5. Vite#
import { cp, mkdir, rm, writeFile } from 'node:fs/promises';
import path from 'node:path';
import { ngp } from '@push-based/vite-plugin-ngp';
import { type Plugin, defineConfig } from 'vite';
export default defineConfig(({ command }) => ({
optimizeDeps: { entries: ['index.html'] },
environments: {
// One bundle the function loads without node_modules, and no copy of `public/`.
ssr: { resolve: { noExternal: true }, build: { copyPublicDir: false } },
},
plugins: [
ngp({
entries: ['src/main.ts', 'src/function.ts'],
// Only the build renders on the server; `vite dev` serves the app client-side.
ssr: command === 'build' && { entry: 'src/function.ts' },
}),
vercelBuildOutput(),
],
}));
function vercelBuildOutput(): Plugin {
return {
name: 'vercel-build-output',
apply: 'build',
buildApp: {
order: 'post',
async handler(builder) {
const { client, ssr } = builder.environments;
await builder.build(client!);
await builder.build(ssr!);
const { root } = builder.config;
const clientDir = path.resolve(root, client!.config.build.outDir);
const serverDir = path.resolve(root, ssr!.config.build.outDir);
const output = path.resolve(root, '.vercel/output');
const fn = path.join(output, 'functions/index.func');
await rm(output, { recursive: true, force: true });
// `index.html` is the template, not a page. Left out, `/` reaches the function.
await cp(clientDir, path.join(output, 'static'), {
recursive: true,
filter: (file) => file !== path.join(clientDir, 'index.html'),
});
// The bundle, and the template next to it: `index.server.html`.
await mkdir(fn, { recursive: true });
await cp(serverDir, fn, { recursive: true });
await writeFile(path.join(fn, 'package.json'), JSON.stringify({ type: 'module' }));
await writeFile(
path.join(fn, '.vc-config.json'),
JSON.stringify({
runtime: 'nodejs26.x',
handler: 'function.js',
launcherType: 'Nodejs',
supportsResponseStreaming: true,
}),
);
// Files first, then everything else to the function, `/` included.
await writeFile(
path.join(output, 'config.json'),
JSON.stringify({
version: 3,
routes: [
{ handle: 'filesystem' },
// A hashed file that no longer exists (an old chunk after a deploy) is a 404, not a page.
{ src: '/assets/(.*)', status: 404 },
{ src: '/(.*)', dest: '/index' },
],
}),
);
},
},
};
}ngp's ssr option builds the browser app into dist/client and the server
entry into dist/server. It is on only for the build: in vite dev, ngp would
render pages with a render(url, document) export that returns a string, which
a streaming entry doesn't have. resolve.noExternal: true bundles every
dependency into the server build, so the function needs no node_modules.
The plugin's buildApp hook builds both, then writes .vercel/output:
| Path | What it holds |
|---|---|
config.json |
The routes: static files first, a 404 for a missing /assets/ file, every other path to the function. |
static/ |
dist/client without index.html, served by Vercel's CDN. |
functions/ |
The server bundle, and its chunks in assets/. |
functions/ |
The template. |
functions/ |
A Node.js function, with response streaming on. |
index.html stays out of static/ for the same reason the Cloudflare setup
sets html_handling: "none": it is the template, not a page. If it were there,
Vercel would serve it for / before the function ever saw the request.
Add .vercel/ to .gitignore.
6. Develop#
vite dev # http://localhost:5173vite dev serves the app client-side, without a server render, and Angular
logs NG0505 because there is no hydration data. Nothing streams until the build.
7. Build and deploy#
Create a Vercel project for the app (in a monorepo, set its Root Directory to
the app's folder). Vercel runs the app's build script, finds the
.vercel/output it wrote and deploys that as it is, so the project needs no
vercel.json and no framework preset. From your machine:
vercel build # runs the build script; .vercel/output is the result
vercel deploy --prebuilt # uploads .vercel/output8. Check that it streams#
Against the deployment's address:
Request a page and print when each piece of the response arrives:
curl -sN -o /dev/null --trace-time --trace-ascii - https://my-app.vercel.app/ | 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. Stream something#
Nothing streams until a template has a @defer block with a hydrate
trigger: read Making something stream.
The Edge runtime#
Vercel also runs functions on its Edge runtime, and the Angular server bundle fits there: the example's is about 210 kB gzipped, against limits of 1 MB (Hobby), 2 MB (Pro) and 4 MB (Enterprise). Built for the Edge runtime and run in Vercel's local Edge emulation, it streams the same way. It does take three changes from the setup above:
- No file system: the template has to be part of the bundle (inlined with
Vite's
define, or imported as a string), not read from disk. ssr: { target: 'webworker' }in the Vite config. Built for Node, the bundle importsnode:moduleto reach therequire()that Angular's server XHR fallback (xhr2) uses, and the Edge runtime has nonode:module.- An Edge function's
.vc-config.jsonis{ "runtime": "edge", "entrypoint": "..." }. The bundle that was checked was a single file (codeSplitting: false), with no chunks to load.
Vercel itself recommends the Node.js runtime over Edge, and the Node.js setup is the one documented here and used by the example.
What has been checked#
In this repository, with apps/example-vercel:
vite build, andvercel buildwith no login, and a hand-written.vercel/project.jsonin place ofvercel link: Vercel CLI picked up the.vercel/outputthe build wrote, with or without the Vite framework preset.- The built function, run through
@vercel/node's local server (the code that wraps a{ fetch }export and streams its response on Vercel's Node.js runtime), withstatic/served first:curl -Nshows the shell at once and the widget's<template data-ngs>chunk 2 s later, and in headless Chrome the widget hydrates. - The Edge variant, run through
@vercel/node's Edge emulation: the same timings.
Not checked: a real deployment. Vercel's CDN, its compression and its routing in front of the function have not been seen streaming this app.