Angular SSG + Hydration: How I Implemented Static Generation
Why I Chose SSG for My Portfolio
- Instant loading thanks to prerendered HTML
- Better SEO, since Google sees full HTML without JS
- No backend needed after deployment
- Static hosting (cheap, fast, and globally cached)
- Perfect compatibility with Angular routes
The goal was simple: Make the site feel native-fast while giving Google everything it needs to index each page properly.
Why Hydration Matters
- Angular doesn’t attach event listeners
- Components don’t become active
- No animations, no bindings, no interactions
- Your site looks correct, but feels dead
Hydration solves this by bootstrapping Angular without destroying the prerendered HTML. Instead of wiping the DOM and re-rendering everything from scratch, Angular reuses the existing server-generated markup and "wakes it up".
Setting Up Angular SSG
Angular makes SSG extremely simple. If your project is already using the Angular CLI, all you need is:
ng add @angular/ssrThis sets up:
- Adds "prerender": true in your angular.json build configuration to enable Static Site Generation (SSG) automatically.
- Creates a server.ts file to handle SSR configuration (used for serving the prerendered output).
- Enables hydration support so your prerendered HTML becomes fully interactive on the client side.
Just to be clear :
In Angular 17+, when you enable "prerender": true in your angular.json, the build automatically generates static HTML for each route, no need for manual route lists or a custom server.ts file (that’s only required for SSR setups).
After that, you can generate your static pages using:
ng buildThis scans your routes and generates static HTML files inside the dist/browser folder. Each route becomes its own prerendered HTML page.
Quick note:
The location of your prerendered files can vary depending on your Angular setup. In most cases, the output will be found under dist/< project-name>/browser, but this depends entirely on your outputPath setting inside angular.json. For example, if you set "outputPath": "dist/browser", the files will be generated directly in that folder instead. It’s also worth noting that Angular 17’s new application builder slightly adjusted some defaults compared to earlier versions, so double-check your angular.json to confirm the final build path before serving your static files.
How to Run the App (Local + SSG Build)
Once SSG and hydration are enabled, you can run your Angular app in two different ways depending on what you want to test.
1. Run the dev server (normal Angular behavior)
If you just want to work on your app in development mode:
ng serveThis runs your app without SSG. It’s fast, live-reloads automatically, and is perfect for daily development.
2. Build the static prerendered site and Preview it locally
To generate the full SSG output and preview:
ng buildnpx http-server dist/browser -p 4200Conclusion
Patrick Linguerri
Angular Frontend Engineer