Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion apps/docs/src/content/_shared/browser-config.md
Original file line number Diff line number Diff line change
Expand Up @@ -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: [
Expand Down
3 changes: 2 additions & 1 deletion apps/docs/src/content/getting-started/angular-cli.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
21 changes: 15 additions & 6 deletions apps/docs/src/content/getting-started/installation.md
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
21 changes: 12 additions & 9 deletions apps/docs/src/content/getting-started/introduction.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,22 +41,25 @@ 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
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

Expand Down
40 changes: 32 additions & 8 deletions apps/docs/src/content/reference/angular-ssr.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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
Expand All @@ -57,6 +64,23 @@ supports a checked range of Angular versions only.

```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
Expand Down
9 changes: 6 additions & 3 deletions apps/docs/src/content/reference/aws-lambda.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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`
Expand All @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion apps/docs/src/content/reference/cloudflare.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
38 changes: 28 additions & 10 deletions apps/docs/src/content/reference/core.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -92,7 +95,8 @@ function createStreamingHandler<Context = void>(
options: StreamingHandlerOptions<Context>,
): StreamingHandler<Context>;

interface StreamingHandlerOptions<Context = void> {
/** What every adapter takes too, besides its own options. */
interface StreamingAppOptions {
/** The application's root component. */
rootComponent: Type<unknown>;
/** The application's server config, provideServerRendering() included. */
Expand All @@ -101,6 +105,9 @@ interface StreamingHandlerOptions<Context = void> {
allowedHosts: readonly string[];
/** The contents of @angular/core/event-dispatch-contract.min.js, for withEventReplay(). */
eventDispatchContract?: string;
}

interface StreamingHandlerOptions<Context = void> extends StreamingAppOptions {
/** Fetches the built index.html. A response that isn't ok turns into a 404. */
loadTemplate(request: Request, context: Context): Promise<Response>;
}
Expand All @@ -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()`.
Expand Down Expand Up @@ -161,16 +170,25 @@ type StreamingAngularRenderOptions = {
transformDocument?: (document: string) => string | Promise<string>;
/** Stops the render, before or after the shell has gone out. */
signal?: AbortSignal;
} & (
| { rootComponent: Type<unknown>; config: ApplicationConfig }
| { bootstrap: (context: BootstrapContext) => Promise<ApplicationRef> }
);
} & (StreamingRootComponent | StreamingBootstrapFunction);

interface StreamingRootComponent {
rootComponent: Type<unknown>;
config: ApplicationConfig;
}

interface StreamingBootstrapFunction {
bootstrap: StreamingBootstrap;
}

/** What an Angular CLI app's main.server.ts exports by default. */
type StreamingBootstrap = (context: BootstrapContext) => Promise<ApplicationRef>;
```

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.

Expand Down
2 changes: 1 addition & 1 deletion apps/docs/src/content/reference/fastify.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
39 changes: 28 additions & 11 deletions apps/docs/src/content/reference/nestjs.md
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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<AngularStreamModuleOptions, 'create'>,
): DynamicModule;
}

/** The Node adapter's options, unchanged. */
Expand Down Expand Up @@ -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<Options, 'create'> {
imports?: ModuleMetadata['imports'];
useFactory?: (...args: any[]) => AngularStreamModuleOptions | Promise<AngularStreamModuleOptions>;
inject?: InjectionToken[];
useClass?: Type<{ create(): AngularStreamModuleOptions | Promise<AngularStreamModuleOptions> }>;
useExisting?: Type<{
create(): AngularStreamModuleOptions | Promise<AngularStreamModuleOptions>;
}>;
useFactory?: (...args: any[]) => Options | Promise<Options>;
inject?: (InjectionToken | OptionalFactoryDependency)[];
useClass?: Type<{ create(): Options | Promise<Options> }>;
useExisting?: Type<{ create(): Options | Promise<Options> }>;
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({
Expand All @@ -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()`

Expand Down Expand Up @@ -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);
}
```

Expand Down
5 changes: 4 additions & 1 deletion apps/docs/src/content/reference/nitro.md
Original file line number Diff line number Diff line change
@@ -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
```

Expand Down Expand Up @@ -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,
Expand Down
Loading
Loading