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
12 changes: 11 additions & 1 deletion apps/docs/src/app/docs/nav.ts
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,8 @@ export const DOCS: DocSection[] = [
{
id: 'guides',
title: 'Guides',
description: 'Making a page stream, checking that it does, and getting it through a proxy.',
description:
'Making a page stream, checking that it does, getting it through a proxy, and running it in production.',
groups: [
{
pages: [
Expand Down Expand Up @@ -334,6 +335,15 @@ export const DOCS: DocSection[] = [
'Compression, Nginx, CDNs and timeouts: what holds a streamed response back, and how to check that it does not.',
load: () => import('../../content/guides/streaming-through-proxies.md'),
},
{
dir: 'guides',
slug: 'production',
title: 'Going to production',
headline: 'Streamed Angular SSR in production',
description:
'Status codes, crawlers and metadata, Content Security Policy, caching, Web Vitals and request context, with a checklist.',
load: () => import('../../content/guides/production.md'),
},
{
dir: 'guides',
slug: 'service-worker',
Expand Down
150 changes: 150 additions & 0 deletions apps/docs/src/content/guides/production.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
A streamed page is sent before it is finished, and that changes a few things a
server-rendered app usually takes for granted: the status code, what crawlers
and caches get, the security policy, and how the page loads. This page goes
through them, and ends in a checklist.

## Status codes and redirects

The status and headers go out with the shell, before any data has arrived. A
request that fails later can't turn the response into an error page, and a
`404` has to be known before the first byte.

- **With the Angular CLI**, `@angular/ssr` keeps its server routes, so give a
route its `status` there, and its redirects. A component can still set
`RESPONSE_INIT` until the shell goes out: decide the status from the URL, or
from a route resolver, whose data is there before the route renders. Anything
that arrives with a chunk comes too late. See
[What `@angular/ssr` still does](../getting-started/angular-cli.md#what-angularssr-still-does).
- **On the other setups** every page is a `200`, a not-found page included.
A server that knows its routes sets the status from outside, keeping the
streamed body, as [the demo's Worker](../../../../cloudflare-demo/src/worker.ts)
does:

```ts
const response = await pages.fetch(request, env);
if (isPage(url.pathname) || !response.ok) {
return response;
}
return new Response(response.body, { status: 404, headers: response.headers });
```

Redirects are the same: answer them before the request reaches the handler.

A page that loads and then finds out its data doesn't exist, a product that was
deleted, still answers `200`. Show a not-found state in the block, and keep
such pages out of search with `<meta name="robots" content="noindex">` set in
the shell, if the route can tell by then.

## Crawlers and metadata

Crawlers get the same stream as everyone. One that runs scripts, as Google's
does, sees the page as a reader does, once the stream has ended. One that
doesn't keeps the shell: each block's loading state, with the streamed markup
in `<template>` elements at the end of the document, which isn't part of the
rendered page. There is no buffered render that includes the blocks either:
the fallback leaves them to the browser too. Whatever must be read without
scripts belongs in the shell.

The document's `<head>` is part of the shell, so the same goes for metadata:

- **A title or a meta tag set before the shell goes out is in the server's
HTML**: one set from the URL, from route data, or in a resolver.
- **One set from data that streams in later is set in the browser only.**
The server's HTML keeps whatever it had, so a crawler that doesn't run
scripts, and a link preview, see that.

So set a page's title, description and `og:*` tags from something the route
knows when it renders. A resolver that loads them holds the shell back for
that one request, which is the trade: everything else still streams.

## Content Security Policy

The packages put inline scripts in the page: the runtime at the end of the
shell, and one per chunk, with a `<style>` before it when a component new to
the page brings its styles. None of them carries a nonce yet (see
[Caveats and limitations](../reference/limitations.md#streaming)).

- **A policy with `'unsafe-inline'` in `script-src` and `style-src`** works,
and is what a streamed page needs today.
- **A strict policy, nonces or `'strict-dynamic'`,** blocks the runtime and
every chunk. The browser keeps the shell, and the streamed parts never
arrive. Hashes don't help: each chunk's script is different.
- **Angular's own `ngCspNonce`** covers the styles Angular adds itself, not
these scripts.

Check a policy in the browser's console: a blocked chunk logs a CSP violation,
and `window.__ngs` is missing or never reaches `done`.

## Caching

Streamed responses carry `Cache-Control: no-store`: a cached copy of a response
that broke off would be worse than none. With the Angular CLI, that holds for
streamed routes whatever the route's `headers` say.

- **The browser build is cacheable as usual.** The files under `assets/` are
named by their content, so they can be cached for a year.
- **Cache the data, not the page.** The page waits for its API, so a cache in
front of the API, or in the server, speeds up every chunk.
- **A route that should be cached shouldn't stream.** With the Angular CLI,
give it another render mode: `RenderMode.Prerender` writes it at build time.

## Web Vitals

Streaming doesn't make a slow request faster. It changes what the reader gets
before it.

- **TTFB and FCP** come from the shell, so they no longer wait for the slowest
request. This is where streaming shows most.
- **LCP** is when the largest element paints. If it is in the shell, a heading
or an image that doesn't depend on data, it paints with the shell. If it is
inside a block, LCP waits for that block's chunk, as it would have waited for
the whole page.
- **CLS** grows when a placeholder is replaced by content of another size, and
the page below it moves. Give each placeholder the room its content will
take, such as a `min-height` or a skeleton of the same size.
- **INP** benefits from the shell hydrating early: it answers clicks while the
rest is still arriving. Clicks inside a block only work once it has
hydrated, and clicks before Angular starts are replayed for the event types
the shell listens to.

Measure on a throttled connection, where the difference is: the Performance
panel in Chrome's DevTools, with "Slow 4G", shows the shell, each chunk and each
paint on one timeline. Then compare with your field data.

## Request context

- **With the Angular CLI**, `@angular/ssr` provides `REQUEST`, so the app can
read the incoming request's cookies and headers, and forward them to its own
API with an interceptor, as it would without streaming. `REQUEST` is `null`
in the browser.
- **On the other setups**, `createStreamingHandler()` and the adapters built on
it don't hand the request to the app: `REQUEST` isn't provided. A server that
needs it renders with
[`renderStreamingAngular()`](../reference/core.md#renderstreamingangular)
itself, passing `{ provide: REQUEST, useValue: request }` in
`platformProviders`. The handler's host check and its buffered fallback are
then yours to keep.

## Proxies and timeouts

A proxy, a CDN, a load balancer or compression in front of the server can hold
the page back until it is complete, and a timeout can cut a page whose slowest
request takes too long. [Streaming through proxies](streaming-through-proxies.md)
covers each, and how to check every hop.

## Checklist

Before a streamed page goes live:

- Every page that should be a `404` answers `404`, decided before the shell.
- Titles, descriptions and `og:*` tags are in the server's HTML.
- What must be indexed without scripts is in the shell.
- The Content Security Policy lets inline scripts and styles run.
- `allowedHosts` lists every hostname, previews included.
- Every placeholder holds the room its content takes.
- Each request a block waits for has a `timeout()` and an error state; see
[When a request fails](making-something-stream.md#when-a-request-fails).
- The data the page waits for is cached where it can be.
- The deployed page streams through every hop, checked with `curl`.
- Event replay is on: `eventDispatchContract` is passed where the template
lacks it.
9 changes: 8 additions & 1 deletion apps/docs/src/content/reference/limitations.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@
a streamed `@defer` block isn't recorded, so that block loses the clicks made
before Angular starts. When the shell has no listeners at all, Angular removes
the contract, and nothing is recorded.
- **No CSP nonce** on the inline scripts yet.
- **No CSP nonce** on the inline scripts yet, so a strict Content Security
Policy blocks the stream; see
[Content Security Policy](../guides/production.md#content-security-policy).
- **Not cacheable.** Streamed responses are sent with `Cache-Control: no-store`.
- **If the response breaks off**, the browser keeps what it has and the client
fetches the rest itself.
Expand All @@ -41,6 +43,11 @@ limits:
- **No critical CSS inlining or SRI, and no `@angular/service-worker`
build step.** The [Service Worker guide](../guides/service-worker.md) has an
experimental worker of its own, which renders the pages.
- **The app doesn't get the request.** `createStreamingHandler()` and the
adapters built on it don't provide `REQUEST`, so the app can't read the
incoming request's cookies or headers. Rendering with
`renderStreamingAngular()` and passing it in `platformProviders` works; see
[Request context](../guides/production.md#request-context).
- **Inline component `styles` are plain CSS.** There is no
`inlineStyleLanguage`; `styleUrl` files do go through Vite's Sass pipeline.
- **ngp is a prototype.** Any Angular commit can break it, hence the exact
Expand Down
Loading