From 9aa9ca8eaa527324ef17f17e64ee13f631e92b9b Mon Sep 17 00:00:00 2001 From: ChristopherPHolder Date: Sat, 28 Nov 2026 09:20:28 +0100 Subject: [PATCH] docs: the reference and installation pages say what the code does - Introduction lists the angular-ssr adapter, and Installation says once that no release is out yet. - Every adapter's install line installs the core next to it. - core: a rejecting loadTemplate is not a 404; StreamingAppOptions, the bootstrap types and every wire-format type are named. - angular-ssr: StreamingRouteRender, its peers, the NgModule fallback, Content-Language, redirect statuses and the unknown-engine error. - nestjs: forRootAsync() takes Nest's own ConfigurableModuleAsyncOptions, ANGULAR_STREAM_OPTIONS has a type, angularStreamNest() takes routes. - aws-lambda and nitro: URL protocol and host, HEAD, and the build checks. - Node 20 to run the Node adapters, Vite 7 or 8 for the dev plugins. Closes #100 --- .../src/content/_shared/browser-config.md | 2 +- .../content/getting-started/angular-cli.md | 3 +- .../content/getting-started/installation.md | 21 +++++++--- .../content/getting-started/introduction.md | 21 +++++----- .../docs/src/content/reference/angular-ssr.md | 40 +++++++++++++++---- apps/docs/src/content/reference/aws-lambda.md | 9 +++-- apps/docs/src/content/reference/cloudflare.md | 2 +- apps/docs/src/content/reference/core.md | 38 +++++++++++++----- apps/docs/src/content/reference/fastify.md | 2 +- apps/docs/src/content/reference/nestjs.md | 39 +++++++++++++----- apps/docs/src/content/reference/nitro.md | 5 ++- apps/docs/src/content/reference/node.md | 2 +- packages/nestjs/README.md | 2 +- 13 files changed, 132 insertions(+), 54 deletions(-) diff --git a/apps/docs/src/content/_shared/browser-config.md b/apps/docs/src/content/_shared/browser-config.md index 31865c9..4ea810d 100644 --- a/apps/docs/src/content/_shared/browser-config.md +++ b/apps/docs/src/content/_shared/browser-config.md @@ -6,7 +6,7 @@ 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 './routes'; +import { routes } from './app.routes'; export const appConfig: ApplicationConfig = { providers: [ diff --git a/apps/docs/src/content/getting-started/angular-cli.md b/apps/docs/src/content/getting-started/angular-cli.md index 97390dd..99f310e 100644 --- a/apps/docs/src/content/getting-started/angular-cli.md +++ b/apps/docs/src/content/getting-started/angular-cli.md @@ -4,7 +4,8 @@ made by `ng new --ssr`, built by `ng build`, and served by `@angular/ssr`'s 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. +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. ## 1. Install diff --git a/apps/docs/src/content/getting-started/installation.md b/apps/docs/src/content/getting-started/installation.md index d8af38e..59e60ca 100644 --- a/apps/docs/src/content/getting-started/installation.md +++ b/apps/docs/src/content/getting-started/installation.md @@ -1,3 +1,7 @@ +Two things decide the setup: how the app is built, the Angular CLI or Vite +with ngp, and where it runs. Both come down to a runtime page; this one has +what they all need. + ## Requirements - **Angular 19.2, 20.3, 21.2 or 22**, from the patch each row of @@ -16,6 +20,12 @@ to exactly `22.3.0-rc.0`. `@angular/ssr` is not used on this path, so its `AngularAppEngine`, server routes and render modes are not available; see [Caveats and limitations](../reference/limitations.md). +- **To run, Node 20 or later** where the server is Node: the Node, Fastify, + NestJS and AWS Lambda adapters use `Headers.getSetCookie()`. CI runs them on + Node 26. The other runtimes need what their own platform asks for. +- **For the dev plugins, Vite 7 or later** (`angularStreamNode()`, + `angularStreamNest()`), and Vite 8 for Nitro's. They are optional peer + dependencies: a production server doesn't load them. ## Compatibility @@ -59,7 +69,11 @@ joins the peer ranges once it passes. ## Install -Releases go to npm's `next` dist-tag for now: +No release is out yet: until the first one, the packages are only available as +source in [the repository](https://github.com/push-based/angular-ssr-stream), +where the apps use them. Releases will go to npm's `next` dist-tag while ngp +pins Angular to a release candidate, which is why every command on these pages +installs `@next`: ```bash npm install @push-based/angular-ssr-stream-core@next @@ -96,11 +110,6 @@ version of each. The packages ship ESM and type declarations, and carry no Angular decorators, so they need no Angular compilation of their own. -> [!NOTE] -> Until the first release is out, the packages are only available as source in -> [the repository](https://github.com/push-based/angular-ssr-stream), where -> the apps use them. - The hosted platforms (Vercel, Netlify, AWS Lambda, Azure Functions, Fastly Compute, Deno Deploy, and Nitro's presets for them) have been checked locally, with each platform's own tooling or a stand-in for it, but not yet on a real diff --git a/apps/docs/src/content/getting-started/introduction.md b/apps/docs/src/content/getting-started/introduction.md index 4cfbb0a..0ab822f 100644 --- a/apps/docs/src/content/getting-started/introduction.md +++ b/apps/docs/src/content/getting-started/introduction.md @@ -41,15 +41,16 @@ apart. ## The packages -| Package | What it is | -| ------------------------------------------------------------------------- | --------------------------------------------------------------------------------------------------------- | -| [`@push-based/angular-ssr-stream-core`](../reference/core.md) | The renderer, its inline runtime and the client. Runtime-agnostic: web `Request`, `Response` and streams. | -| [`@push-based/angular-ssr-stream-node`](../reference/node.md) | `createNodeHandler()`: a Node request handler for `node:http`, Express, Connect and Koa. | -| [`@push-based/angular-ssr-stream-fastify`](../reference/fastify.md) | `angularStream`: a Fastify plugin that streams every page route. | -| [`@push-based/angular-ssr-stream-nestjs`](../reference/nestjs.md) | `AngularStreamModule`: a Nest module, on Express or on Fastify. | -| [`@push-based/angular-ssr-stream-cloudflare`](../reference/cloudflare.md) | `createStreamingWorker()`: a Cloudflare Worker `fetch` handler over the `ASSETS` binding. | -| [`@push-based/angular-ssr-stream-aws-lambda`](../reference/aws-lambda.md) | `createStreamingLambda()`: a response-streaming handler for a Lambda Function URL. | -| [`@push-based/angular-ssr-stream-nitro`](../reference/nitro.md) | `defineStreamingHandler()`: an h3 handler for a Nitro server. | +| Package | What it is | +| --------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------ | +| [`@push-based/angular-ssr-stream-core`](../reference/core.md) | The renderer, its inline runtime and the client. Runtime-agnostic: web `Request`, `Response` and streams. | +| [`@push-based/angular-ssr-stream-angular-ssr`](../reference/angular-ssr.md) | `withStreaming()`: streams an Angular CLI app's server-rendered routes, with `@angular/ssr` still in charge. | +| [`@push-based/angular-ssr-stream-node`](../reference/node.md) | `createNodeHandler()`: a Node request handler for `node:http`, Express, Connect and Koa. | +| [`@push-based/angular-ssr-stream-fastify`](../reference/fastify.md) | `angularStream`: a Fastify plugin that streams every page route. | +| [`@push-based/angular-ssr-stream-nestjs`](../reference/nestjs.md) | `AngularStreamModule`: a Nest module, on Express or on Fastify. | +| [`@push-based/angular-ssr-stream-cloudflare`](../reference/cloudflare.md) | `createStreamingWorker()`: a Cloudflare Worker `fetch` handler over the `ASSETS` binding. | +| [`@push-based/angular-ssr-stream-aws-lambda`](../reference/aws-lambda.md) | `createStreamingLambda()`: a response-streaming handler for a Lambda Function URL. | +| [`@push-based/angular-ssr-stream-nitro`](../reference/nitro.md) | `defineStreamingHandler()`: an h3 handler for a Nitro server. | An app uses both halves of the core: providers in its browser config, and a server entry that streams. The server entry is where runtimes differ. A runtime @@ -57,6 +58,8 @@ that hands the server a web `Request` and takes a `Response` back (Hono, Deno, Bun, Fastly Compute, Vercel, Netlify, Azure Functions) needs only the core's [`createStreamingHandler()`](../reference/core.md#createstreaminghandler). The others get a small adapter over it, which is what the rest of the packages are. +An Angular CLI app keeps `@angular/ssr` as its server and adds +`withStreaming()` to its engine. ## Where to go next diff --git a/apps/docs/src/content/reference/angular-ssr.md b/apps/docs/src/content/reference/angular-ssr.md index 8c5b8a5..8b24762 100644 --- a/apps/docs/src/content/reference/angular-ssr.md +++ b/apps/docs/src/content/reference/angular-ssr.md @@ -1,11 +1,12 @@ ```bash -npm install @push-based/angular-ssr-stream-angular-ssr@next +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`](core.md), whose browser -providers the app still needs. For the whole setup, see -[Angular CLI](../getting-started/angular-cli.md). +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](../getting-started/angular-cli.md). Two entry points, because they end up in two different server bundles: `@push-based/angular-ssr-stream-angular-ssr` in `server.ts`'s, and @@ -39,15 +40,21 @@ is rendered by the core's streaming renderer, with: `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; -- a redirect instead of the page when the router ended up at another URL, and - no response at all (`null`, so the next middleware runs) when no navigation - succeeded; + 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. +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 @@ -57,6 +64,23 @@ supports a checked range of Angular versions only. ```ts function streamingRenderer(route: StreamingRouteRender): Promise; + +/** 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; +} ``` Renders one server-rendered route as a stream. `withStreaming()` calls it; an diff --git a/apps/docs/src/content/reference/aws-lambda.md b/apps/docs/src/content/reference/aws-lambda.md index eeb10e3..4c9aebe 100644 --- a/apps/docs/src/content/reference/aws-lambda.md +++ b/apps/docs/src/content/reference/aws-lambda.md @@ -1,5 +1,5 @@ ```bash -npm install @push-based/angular-ssr-stream-aws-lambda@next +npm install @push-based/angular-ssr-stream-core@next @push-based/angular-ssr-stream-aws-lambda@next ``` The AWS Lambda adapter. It depends on @@ -55,7 +55,9 @@ global is missing. That happens outside Lambda, or with For each event, the handler: 1. Builds a web `Request`. The URL is the `host` header (the Function URL's - hostname, also behind CloudFront), plus `rawPath` and `rawQueryString`. + hostname, also behind CloudFront, or `requestContext.domainName` without + one), plus `rawPath` and `rawQueryString`, over `X-Forwarded-Proto`, or + `https` without it. The headers are the event's, with `cookies` joined back into a `cookie` header. The body is decoded from base64 when `isBase64Encoded` is set. 2. Renders it with the core handler. The template is read from `templatePath` @@ -65,7 +67,8 @@ For each event, the handler: status code and headers go in the metadata prelude, and `Set-Cookie` headers go in its `cookies`. The prelude is sent straight away. Each body chunk is written as the renderer flushes it, waiting for `drain` when the - stream is full. The handler resolves once the stream has finished. + stream is full. A `HEAD` request gets the prelude only, and the body is + cancelled. The handler resolves once the stream has finished. If the render fails after the first chunk, the handler rejects. The runtime then ends the stream with error trailers, so the client gets a cut-off page. diff --git a/apps/docs/src/content/reference/cloudflare.md b/apps/docs/src/content/reference/cloudflare.md index ec4f482..c71fcb1 100644 --- a/apps/docs/src/content/reference/cloudflare.md +++ b/apps/docs/src/content/reference/cloudflare.md @@ -1,5 +1,5 @@ ```bash -npm install @push-based/angular-ssr-stream-cloudflare@next +npm install @push-based/angular-ssr-stream-core@next @push-based/angular-ssr-stream-cloudflare@next ``` The Cloudflare Workers adapter. It depends on diff --git a/apps/docs/src/content/reference/core.md b/apps/docs/src/content/reference/core.md index f8147f9..7727530 100644 --- a/apps/docs/src/content/reference/core.md +++ b/apps/docs/src/content/reference/core.md @@ -80,7 +80,10 @@ instead. ### Wire format `StreamChunk`, `StreamBlock`, `StreamInit`, `StreamRuntime`, `NGH` and -`streamRequestKey()` describe what travels in a chunk. See +`streamRequestKey()` describe what travels in a chunk; `SerializedView` and +`SerializedDeferBlock` the hydration data in it; `StreamSwap` and +`StreamLogEntry` what the inline runtime records, and `StreamSink` the app's +side of it, attached once Angular starts. See [Wire format and internals](../concepts/internals.md#the-wire-format). ## Server @@ -92,7 +95,8 @@ function createStreamingHandler( options: StreamingHandlerOptions, ): StreamingHandler; -interface StreamingHandlerOptions { +/** What every adapter takes too, besides its own options. */ +interface StreamingAppOptions { /** The application's root component. */ rootComponent: Type; /** The application's server config, provideServerRendering() included. */ @@ -101,6 +105,9 @@ interface StreamingHandlerOptions { allowedHosts: readonly string[]; /** The contents of @angular/core/event-dispatch-contract.min.js, for withEventReplay(). */ eventDispatchContract?: string; +} + +interface StreamingHandlerOptions extends StreamingAppOptions { /** Fetches the built index.html. A response that isn't ok turns into a 404. */ loadTemplate(request: Request, context: Context): Promise; } @@ -113,8 +120,10 @@ request it: 1. applies Angular's host guard, answering `403` for a host not in `allowedHosts`; -2. loads the template with `loadTemplate`, answering `404` if that fails, and - adds `eventDispatchContract` to it; +2. loads the template with `loadTemplate`, answering `404` if its response + isn't `ok`, and adds `eventDispatchContract` to it. A `loadTemplate` that + rejects isn't caught: the error reaches the runtime, which usually answers + `500`; 3. streams the route into it with `renderStreamingAngular()`; 4. if streaming throws before its first flush, renders the page buffered instead, with `createBufferedRender()`. @@ -161,16 +170,25 @@ type StreamingAngularRenderOptions = { transformDocument?: (document: string) => string | Promise; /** Stops the render, before or after the shell has gone out. */ signal?: AbortSignal; -} & ( - | { rootComponent: Type; config: ApplicationConfig } - | { bootstrap: (context: BootstrapContext) => Promise } -); +} & (StreamingRootComponent | StreamingBootstrapFunction); + +interface StreamingRootComponent { + rootComponent: Type; + config: ApplicationConfig; +} + +interface StreamingBootstrapFunction { + bootstrap: StreamingBootstrap; +} + +/** What an Angular CLI app's main.server.ts exports by default. */ +type StreamingBootstrap = (context: BootstrapContext) => Promise; ``` The streaming renderer itself: resolves once the shell is ready, with a response whose body is the shell followed by a chunk per finished request. The -status is `responseInit.status`, `200` without one, and the response carries -`Cache-Control: no-store`: a cached copy of a partial response would be worse +status is `responseInit.status`, `200` without one. The renderer sets +`Content-Type` itself, and `Cache-Control: no-store`: a cached copy of a partial response would be worse than none. It throws if rendering fails before the first flush; after that, the error breaks the stream. diff --git a/apps/docs/src/content/reference/fastify.md b/apps/docs/src/content/reference/fastify.md index a71bb0b..700d9c3 100644 --- a/apps/docs/src/content/reference/fastify.md +++ b/apps/docs/src/content/reference/fastify.md @@ -1,5 +1,5 @@ ```bash -npm install @push-based/angular-ssr-stream-fastify@next fastify +npm install @push-based/angular-ssr-stream-core@next @push-based/angular-ssr-stream-fastify@next fastify ``` The Fastify plugin. It is built on the [Node adapter](node.md), and depends on diff --git a/apps/docs/src/content/reference/nestjs.md b/apps/docs/src/content/reference/nestjs.md index e9dafa1..f0cb37a 100644 --- a/apps/docs/src/content/reference/nestjs.md +++ b/apps/docs/src/content/reference/nestjs.md @@ -1,5 +1,5 @@ ```bash -npm install @push-based/angular-ssr-stream-nestjs@next +npm install @push-based/angular-ssr-stream-core@next @push-based/angular-ssr-stream-nestjs@next ``` The NestJS module, for `@nestjs/platform-express` and @@ -20,7 +20,9 @@ Two entry points: `@push-based/angular-ssr-stream-nestjs` for the server, and ```ts class AngularStreamModule { static forRoot(options: AngularStreamModuleOptions): DynamicModule; - static forRootAsync(options: AngularStreamModuleAsyncOptions): DynamicModule; + static forRootAsync( + options: ConfigurableModuleAsyncOptions, + ): DynamicModule; } /** The Node adapter's options, unchanged. */ @@ -153,19 +155,22 @@ complete one, and no filter writes into it. That holds on both platforms. ## `AngularStreamModule.forRootAsync()` ```ts -interface AngularStreamModuleAsyncOptions { +import type { ConfigurableModuleAsyncOptions } from '@nestjs/common'; + +// What it comes to, with Options = AngularStreamModuleOptions: +interface ConfigurableModuleAsyncOptions { imports?: ModuleMetadata['imports']; - useFactory?: (...args: any[]) => AngularStreamModuleOptions | Promise; - inject?: InjectionToken[]; - useClass?: Type<{ create(): AngularStreamModuleOptions | Promise }>; - useExisting?: Type<{ - create(): AngularStreamModuleOptions | Promise; - }>; + useFactory?: (...args: any[]) => Options | Promise; + inject?: (InjectionToken | OptionalFactoryDependency)[]; + useClass?: Type<{ create(): Options | Promise }>; + useExisting?: Type<{ create(): Options | Promise }>; + provideInjectionTokensFrom?: Provider[]; } ``` The same module, with the options from a provider, as Nest's -`ConfigurableModuleBuilder` defines it: +`ConfigurableModuleBuilder` defines it. The type is Nest's own, from +`@nestjs/common`; this package exports no name for it. ```ts AngularStreamModule.forRootAsync({ @@ -185,7 +190,16 @@ The options are read once, when the module initializes. ## `ANGULAR_STREAM_OPTIONS` -The injection token the module's options are provided under. +```ts +const ANGULAR_STREAM_OPTIONS: string | symbol; +``` + +The injection token the module's options are provided under, whether they came +from `forRoot()` or `forRootAsync()`: + +```ts +constructor(@Inject(ANGULAR_STREAM_OPTIONS) options: AngularStreamModuleOptions) {} +``` ## `createRequestListener()` @@ -224,11 +238,14 @@ import { angularStreamNest } from '@push-based/angular-ssr-stream-nestjs/vite'; function angularStreamNest(options?: AngularStreamNestOptions): Plugin[]; +/** angularStreamNode()'s options, unchanged. */ interface AngularStreamNestOptions { /** The server entry, relative to Vite's root. Default: 'src/server.ts'. */ entry?: string; /** The template, relative to Vite's root. Default: 'index.html'. */ template?: string; + /** Requests for the server entry before Vite sees them: path prefixes, or a function. Default: none. */ + routes?: readonly string[] | ((pathname: string, req: IncomingMessage) => boolean); } ``` diff --git a/apps/docs/src/content/reference/nitro.md b/apps/docs/src/content/reference/nitro.md index 0e38520..1165492 100644 --- a/apps/docs/src/content/reference/nitro.md +++ b/apps/docs/src/content/reference/nitro.md @@ -1,5 +1,5 @@ ```bash -npm install @push-based/angular-ssr-stream-nitro@next unstorage +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 ``` @@ -82,6 +82,9 @@ plugins: [ - In `vite build` it takes the built `index.html` out of the client build, so that it is not in the static output for Nitro or a CDN to serve, and writes it to that directory. Nitro then bundles it into the server, whatever the preset. + The build fails when the client build has no top-level `index.html`, rather + than answering every page with `404` later. A template left there by + `vite dev` is removed first, so it can't end up in a production bundle. - In `vite dev` it writes the `index.html` Vite serves there, with Vite's dev transforms, and again whenever `index.html` changes. - It makes `index.html` the client build's input, unless the config names one, diff --git a/apps/docs/src/content/reference/node.md b/apps/docs/src/content/reference/node.md index fe69330..bed0e46 100644 --- a/apps/docs/src/content/reference/node.md +++ b/apps/docs/src/content/reference/node.md @@ -1,5 +1,5 @@ ```bash -npm install @push-based/angular-ssr-stream-node@next +npm install @push-based/angular-ssr-stream-core@next @push-based/angular-ssr-stream-node@next ``` The Node adapter. It depends on [`@push-based/angular-ssr-stream-core`](core.md), diff --git a/packages/nestjs/README.md b/packages/nestjs/README.md index a015d75..332f01e 100644 --- a/packages/nestjs/README.md +++ b/packages/nestjs/README.md @@ -10,7 +10,7 @@ Streamed server-side rendering for Angular in a NestJS app. The browser starts hydrating the page while the response is still arriving. `AngularStreamModule` -renders every `GET` that nothing else in the app answers, on +renders every `GET` and `HEAD` that nothing else in the app answers, on `@nestjs/platform-express` through the Node adapter, [`@push-based/angular-ssr-stream-node`](https://www.npmjs.com/package/@push-based/angular-ssr-stream-node), and on `@nestjs/platform-fastify` through the Fastify adapter,