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
48 changes: 38 additions & 10 deletions apps/docs/markdown.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,30 +121,58 @@ export interface SearchEntry {

/**
* A line that is replaced by another file's Markdown: a step several pages
* share, written once. The path is relative to the page, like a link.
* share, written once. The path is relative to the page, like a link. It may be
* followed by values for the file's `{{name}}` slots, as `name=value` (no
* spaces in the value): `<!-- include ../_shared/check-it-streams.md url=http://localhost:3000/ -->`.
*/
const INCLUDE = /^<!-- include (\S+) -->$/gm;
const INCLUDE = /^<!-- include (\S+)((?: [\w-]+=\S+)*) -->$/gm;

/** A slot in an included file, filled from the include line. */
const SLOT = /\{\{([\w-]+)\}\}/g;

/**
* Puts each included file in the place of its `<!-- include … -->` line. The
* text is inlined before it is rendered, so its links resolve from the page
* that includes it: keep included files one folder down from `contentDir`,
* like the pages, and the same relative link works from both. An included file
* cannot include another.
* Puts each included file in the place of its `<!-- include … -->` line, with
* its slots filled. The text is inlined before it is rendered, so its links
* resolve from the page that includes it: keep included files one folder down
* from `contentDir`, like the pages, and the same relative link works from
* both. An included file cannot include another. A slot left without a value,
* or a value for no slot, fails the build.
*/
export function withIncludes(
source: string,
file: string,
options: MarkdownOptions,
): { source: string; included: string[] } {
const included: string[] = [];
const whole = source.replace(INCLUDE, (_, target: string) => {
const whole = source.replace(INCLUDE, (_, target: string, args: string) => {
const where = path.relative(options.repoRoot, file);
const absolute = path.resolve(path.dirname(file), target);
if (!existsSync(absolute)) {
throw new Error(`${path.relative(options.repoRoot, file)}: nothing to include at ${target}`);
throw new Error(`${where}: nothing to include at ${target}`);
}
included.push(absolute);
return readFileSync(absolute, 'utf8').trim();
const values = new Map(
args
.trim()
.split(' ')
.filter(Boolean)
.map((pair) => [pair.slice(0, pair.indexOf('=')), pair.slice(pair.indexOf('=') + 1)]),
);
const unused = new Set(values.keys());
const text = readFileSync(absolute, 'utf8')
.trim()
.replace(SLOT, (_slot, name: string) => {
const value = values.get(name);
if (value === undefined) {
throw new Error(`${where}: ${target} needs a value for {{${name}}}`);
}
unused.delete(name);
return value;
});
if (unused.size > 0) {
throw new Error(`${where}: ${target} has no slot for ${[...unused].join(', ')}`);
}
return text;
});
return { source: whole, included };
}
Expand Down
3 changes: 3 additions & 0 deletions apps/docs/src/content/_shared/allowed-hosts.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
`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`.
48 changes: 48 additions & 0 deletions apps/docs/src/content/_shared/before-you-start.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
This setup builds the app with Vite and
[`@push-based/vite-plugin-ngp`](https://github.com/push-based/vite-plugin-ngp),
not the Angular CLI. For an app made by `ng new --ssr` that keeps
`@angular/ssr`, follow [Angular CLI](../getting-started/angular-cli.md)
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:

```html title="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>
```

```ts title="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`.
17 changes: 17 additions & 0 deletions apps/docs/src/content/_shared/check-it-streams.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
Request a page and print when each piece of the response arrives:

```bash
curl -sN -o /dev/null --trace-time --trace-ascii - {{url}} | 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](../guides/streaming-through-proxies.md#checking-that-a-response-streams).
7 changes: 7 additions & 0 deletions apps/docs/src/content/_shared/event-replay.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
`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.
40 changes: 16 additions & 24 deletions apps/docs/src/content/getting-started/angular-cli.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ rendered: as a stream, instead of all at once.

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.
On Angular 19.2, 20.3 or 21.2, two more apply; see
[Angular 19 to 21](#angular-19-to-21).

## 1. Install

Expand Down Expand Up @@ -126,21 +128,23 @@ ng build
node dist/my-app/server/server.mjs # http://localhost:4000, or $PORT
```

Check that it streams before you put anything in front of it:
## 8. Check that it streams

```bash
curl -sN -o /dev/null --trace-time --trace-ascii - http://localhost:4000/ | grep 'Recv data'
```
Before you put anything in front of the server:

```text
07:51:38.138747 <= Recv data, 3557 bytes (0xde5)
07:51:40.145185 <= Recv data, 807 bytes (0x327)
```
<!-- include ../_shared/check-it-streams.md url=http://localhost:4000/ -->

That's the example: the shell at once, and the widget's chunk two seconds
later, when its data is there. One line for the whole page means something
buffered it; see
[Checking that a response streams](../guides/streaming-through-proxies.md#checking-that-a-response-streams).
## 9. Behind a proxy

A proxy, a CDN or compression middleware can hold the response back until it
is complete, and the page then arrives all at once. Streamed responses carry
`X-Accel-Buffering: no` for Nginx; for the rest, read
[Streaming through proxies](../guides/streaming-through-proxies.md).

## 10. Stream something

Nothing streams until a template has a `@defer` block with a `hydrate`
trigger: read [Making something stream](../guides/making-something-stream.md).

## What `@angular/ssr` still does

Expand Down Expand Up @@ -181,15 +185,3 @@ The steps above are for Angular 22, which `ng new` gives you today. On 19.2,
it may still have a `bootstrap` that takes no argument: add it.
- **On 19**, zoneless is `provideExperimentalZonelessChangeDetection()`. Zone.js
works as well.

## 8. Behind a proxy

A proxy, a CDN or compression middleware can hold the response back until it
is complete, and the page then arrives all at once. Streamed responses carry
`X-Accel-Buffering: no` for Nginx; for the rest, read
[Streaming through proxies](../guides/streaming-through-proxies.md).

## 9. Stream something

Nothing streams until a template has a `@defer` block with a `hydrate`
trigger: read [Making something stream](../guides/making-something-stream.md).
69 changes: 42 additions & 27 deletions apps/docs/src/content/getting-started/aws-lambda.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,34 @@ and nothing in it is an npm dependency of the app, which CDK or SST would add.
> [!IMPORTANT]
> This setup has not been deployed. Everything on this page was built and run
> locally, through a stand-in for the Lambda runtime and the Function URL (see
> [Check that it streams](#7-check-that-it-streams)). Nothing was checked
> [Check that it streams](#9-check-that-it-streams)). Nothing was checked
> against a real AWS account. That includes CloudFront, which AWS doesn't
> document as streaming either way.

## 1. The browser config
## Before you start

<!-- include ../_shared/before-you-start.md -->

## 1. Install

```bash
npm install @push-based/angular-ssr-stream-core@next @push-based/angular-ssr-stream-aws-lambda@next
npm install -D @types/node
```

The adapter needs neither the AWS SDK nor `@types/aws-lambda`. Deploying takes
the [AWS SAM CLI](https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/install-sam-cli.html)
and the AWS CLI, installed on their own.

## 2. The browser config

<!-- include ../_shared/browser-config.md -->

## 2. The server config
## 3. The server config

<!-- include ../_shared/server-config.md -->

## 3. The handler
## 4. The handler

```ts title="src/lambda.ts"
import { join } from 'node:path';
Expand Down Expand Up @@ -62,10 +77,9 @@ host the request is rendered for. Function URLs look like
the function's region, so the pattern above matches any Function URL in the
region the function is deployed to, whichever that is.

`eventDispatchContract` is for `withEventReplay()`, as on
[Cloudflare Workers](cloudflare-workers.md#3-the-worker).
<!-- include ../_shared/event-replay.md -->

## 4. Vite
## 5. Vite

```ts title="vite.config.ts"
import { ngp } from '@push-based/vite-plugin-ngp';
Expand Down Expand Up @@ -105,7 +119,7 @@ ngp's own `ssr` option stays off. It builds a `render(url, document)` entry,
not a Lambda handler. ngp still compiles `src/lambda.ts` with its server
compiler, since it's listed in `entries`.

## 5. The function, CloudFront and S3
## 6. The function, CloudFront and S3

The whole template is
[`apps/example-aws-lambda/template.yaml`](../../../../../apps/example-aws-lambda/template.yaml).
Expand Down Expand Up @@ -158,7 +172,14 @@ these patterns: Vite's hashed files under `assets/*`, plus `*.ico`, `*.svg` and
other files. Everything that doesn't match goes to the function, which renders
it as a page.

## 6. Build and deploy
## 7. Develop

`vite dev` serves the app client-side, without the function: Angular renders it
in the browser and logs NG0505, as there is no hydration data. To see it stream
before a deploy, run the built function through the stand-in in
[Check that it streams](#9-check-that-it-streams).

## 8. Build and deploy

```bash
vite build # client to dist/client, handler to dist/server
Expand All @@ -174,11 +195,7 @@ template, which only the function reads.
> These commands were not run. The template passes `cfn-lint` with the SAM
> translator, but it has never been deployed.

## 7. Check that it streams

`curl -N` the page and note when each part arrives. The shell comes at once.
Each `<template data-ngs>` chunk follows when its data does. A response that
arrives all at once after the slowest request was buffered somewhere.
## 9. Check that it streams

Neither SAM local nor the Runtime Interface Emulator streams a response. The
example has its own stand-in instead,
Expand All @@ -189,24 +206,22 @@ Runtime API, and `HttpResponseStream.from()` makes the first write send the
JSON metadata prelude and eight null bytes. A Function URL in front reads the
prelude for the status and headers, and forwards the rest as it arrives.

Copy the example's
[`scripts/`](../../../../../apps/example-aws-lambda/scripts/serve.ts) folder,
both files, into the app. They need nothing but Node:

```bash
pnpm nx serve example-aws-lambda # builds it first; http://localhost:3000/
curl -sN http://localhost:3000/
vite build
node scripts/serve.ts # the built function behind a local Function URL, http://localhost:3000/
```

Through the stand-in, the example's shell, with the widget's
`data-placeholder`, arrives at about 0 ms. The widget's
`<template data-ngs="d0">` arrives about 2000 ms later.
<!-- include ../_shared/check-it-streams.md url=http://localhost:3000/ -->

After a deploy, check the Function URL first and then the CloudFront URL. If
the Function URL streams and CloudFront doesn't, the buffering is in CloudFront.

```bash
curl -sN https://<FunctionUrl>/
curl -sN https://<SiteUrl>/
```
After a deploy, run it against the Function URL first and then the CloudFront
URL (`SiteUrl`). If the Function URL streams and CloudFront doesn't, the
buffering is in CloudFront.

## 8. Stream something
## 10. Stream something

<!-- include ../_shared/stream-something.md -->

Expand Down
Loading
Loading