Sub topic 2
Adding SSR to a project with the Angular CLI
This chapter shows how to add server-side rendering (SSR) to an Angular project, which files the CLI generates, and how to build and run the result. It assumes you know standalone components, ApplicationConfig, basic routing, HttpClient, and npm. It targets Angular 19 or later.
Two ways to get SSR
For a new project, pass the --ssr flag:
ng new shop --ssrFor an existing project, use the schematic from @angular/ssr:
ng add @angular/ssrBoth give you the same setup. The schematic is the one to learn, because it shows what changes in a project you already have.
What the schematic changes
ng add @angular/ssr installs packages and edits or creates several files.
| Change | File | Purpose |
|---|---|---|
| Installs `@angular/ssr`, `express`, `@types/express` | `package.json` | Server renderer and the default Node HTTP server |
| Creates | `src/main.server.ts` | Server entry point that bootstraps the app |
| Creates | `src/server.ts` | Express server that renders requests |
| Creates | `src/app/app.config.server.ts` | Server-only providers |
| Creates | `src/app/app.routes.server.ts` | Render mode for each route |
| Edits | `src/app/app.config.ts` | Adds hydration and the fetch-based HTTP client |
| Edits | `angular.json`, `tsconfig.app.json` | Adds the server build target and entry |
The next chapter covers the server files in detail. Here we focus on the shared config, because it affects your browser bundle too.
The shared application config
After the schematic runs, app.config.ts looks like this:
import { ApplicationConfig } from '@angular/core';
import { provideRouter } from '@angular/router';
import { provideHttpClient, withFetch } from '@angular/common/http';
import { provideClientHydration, withEventReplay } from '@angular/platform-browser';
import { routes } from './app.routes';
export const appConfig: ApplicationConfig = {
providers: [
provideRouter(routes),
provideHttpClient(withFetch()),
provideClientHydration(withEventReplay()),
],
};Two providers matter here:
provideHttpClient(withFetch())switchesHttpClientto thefetchAPI. Angular recommends this backend for SSR, because the default XHR backend relies on browser objects.provideClientHydration(withEventReplay())tells the browser to reuse the server-rendered DOM and to replay clicks made before hydration finishes. Later chapters explain both.
If your project already called provideHttpClient() without withFetch(), the schematic may leave it unchanged. Check it yourself.
How the pieces fit together
Build and run
Build as usual:
ng buildWith SSR enabled, the output goes to two folders under dist/<project-name>/:
browser/holds the static files the client downloads.server/holdsserver.mjs, the Node entry point.
Start the server with Node:
node dist/shop/server/server.mjsBy default it listens on port 4000, or on the port in the PORT environment variable. Open http://localhost:4000 and view the page source. The HTML should contain your rendered content, not an empty <app-root></app-root>.
During development, ng serve also renders on the server. Keep in mind that it shows what the dev server does, so check the production build before you trust a result.
Common errors
window is not definedordocument is not defined. Your code touched a browser global during server rendering. Chapter 5 covers the fix.- The server renders an empty shell. The route may be set to
RenderMode.Clientinapp.routes.server.ts. The catch-all**entry is a common cause. - Changes do not appear. You are running an old
distfolder. Rebuild before you restartserver.mjs. - A hydration warning in the console. The server and browser produced different DOM. See the hydration constraints chapter.
Exercise
You run ng add @angular/ssr on an existing project, then ng build. Which two folders appear under dist/<project-name>/, and which command starts the server?
Show answer
browser/ (static client files) and server/ (the Node server bundle). Start it with node dist/<project-name>/server/server.mjs.
Exercise
A teammate opens the app in Chrome, sees the content in the Elements panel, and says SSR works. Why is this not proof, and what would you do instead?
Show answer
The Elements panel shows the DOM after client JavaScript has run, so a client-only render looks the same. Request the page with curl or use "View page source" and confirm the content is in the HTML the server sent.