diff --git a/apps/docs/markdown.ts b/apps/docs/markdown.ts index efd5632..c3b605a 100644 --- a/apps/docs/markdown.ts +++ b/apps/docs/markdown.ts @@ -121,16 +121,22 @@ 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): ``. */ -const INCLUDE = /^$/gm; +const INCLUDE = /^$/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 `` 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 `` 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, @@ -138,13 +144,35 @@ export function withIncludes( 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 }; } diff --git a/apps/docs/src/content/_shared/allowed-hosts.md b/apps/docs/src/content/_shared/allowed-hosts.md new file mode 100644 index 0000000..24fb377 --- /dev/null +++ b/apps/docs/src/content/_shared/allowed-hosts.md @@ -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`. diff --git a/apps/docs/src/content/_shared/before-you-start.md b/apps/docs/src/content/_shared/before-you-start.md new file mode 100644 index 0000000..1e7f855 --- /dev/null +++ b/apps/docs/src/content/_shared/before-you-start.md @@ -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" + + + + + My app + + + + + + + + +``` + +```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`. diff --git a/apps/docs/src/content/_shared/check-it-streams.md b/apps/docs/src/content/_shared/check-it-streams.md new file mode 100644 index 0000000..1330e1b --- /dev/null +++ b/apps/docs/src/content/_shared/check-it-streams.md @@ -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). diff --git a/apps/docs/src/content/_shared/event-replay.md b/apps/docs/src/content/_shared/event-replay.md new file mode 100644 index 0000000..bd176ca --- /dev/null +++ b/apps/docs/src/content/_shared/event-replay.md @@ -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. diff --git a/apps/docs/src/content/getting-started/angular-cli.md b/apps/docs/src/content/getting-started/angular-cli.md index 99f310e..a541115 100644 --- a/apps/docs/src/content/getting-started/angular-cli.md +++ b/apps/docs/src/content/getting-started/angular-cli.md @@ -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 @@ -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) -``` + -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 @@ -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). diff --git a/apps/docs/src/content/getting-started/aws-lambda.md b/apps/docs/src/content/getting-started/aws-lambda.md index 5b7adf9..1f5a92c 100644 --- a/apps/docs/src/content/getting-started/aws-lambda.md +++ b/apps/docs/src/content/getting-started/aws-lambda.md @@ -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 + + + +## 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 -## 2. The server config +## 3. The server config -## 3. The handler +## 4. The handler ```ts title="src/lambda.ts" import { join } from 'node:path'; @@ -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). + -## 4. Vite +## 5. Vite ```ts title="vite.config.ts" import { ngp } from '@push-based/vite-plugin-ngp'; @@ -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). @@ -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 @@ -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 `