Skip to content
← All writing
November 12, 2026 · 10 min read

Angular Prerendering in Production: 5 Bugs That Only Showed Up Live

Angular Prerendering in Production: 5 Bugs That Only Showed Up Live

Prerendering my Angular site gave me what I wanted: every page arrives as finished HTML, loads fast on a phone, and Google reads it without running any JavaScript. It also moved a large chunk of my code from "runs in a browser" to "runs once, on a build machine, with no browser and no user", and that move produced five bugs that never once appeared in ng serve.

These are all from this site, between July and October 2026, in the order I hit them. Each one has the symptom, the cause and the fix. If you run Angular prerendering (SSG) in production, the pattern behind all five is worth knowing before you hit them yourself.


What's inside

  1. The SSR server that rejected its own domain
  2. Published posts that didn't show up
  3. Image errors that fired before Angular existed
  4. The refresh that signed me out
  5. Share cards WhatsApp couldn't see
  6. The pattern behind all five
  7. A pre-launch checklist for prerendered Angular
  8. FAQ

1. The SSR server that rejected its own domain

31 July. My first deploy used Angular's default server output: a Node process that renders pages on request, with prerendered pages served from disk. Locally, view-source showed full HTML. On the real domain, pages were arriving as an empty shell and rendering in the browser, which meant no prerendered content for crawlers.

The Node server was checking the incoming Host header against an allowlist, a protection against server-side request forgery. Requests on the production host didn't pass, so it fell back to client-side rendering.

I could have configured the allowed hosts. Instead I asked whether I needed a server at all. Every page on the site can be built ahead of time, and nothing needs per-request rendering. So I switched outputMode from "server" to "static" in angular.json, which writes every route to its own index.html, and serve the result with nginx in Docker.

Lesson: if no route truly needs per-request rendering, static output removes a whole running process and its failure modes.


2. Published posts that didn't show up

3 August. I'd built an admin panel to write and publish posts. I published one, opened the blog, and it wasn't there.

That's prerendering doing exactly what it's told. The blog index was rendered at build time from an API fetch at build time. A post published afterwards doesn't exist in that HTML until the next build.

The fix had three parts:

  • Seed, then refresh. Content services start with the build-time data, so the prerendered HTML still arrives complete and crawlable. After first paint, they re-read the live API in the browser and swap in the fresh list.
  • Don't trust a missing slug. A post newer than the build isn't in the build cache, so "not found" now triggers one live refresh before showing a 404.
  • nginx fallback for new slugs. /blog/new-post has no prerendered file yet, so nginx falls back to the blog shell for the /blog/ and /work/ prefixes only, and the app loads the post from the API. Every other unknown URL still gets an honest 404.

The admin also queues a rebuild on publish now, so the fallback only covers the minutes between publishing and the new build going live.


3. Image errors that fired before Angular existed

4 August. Blog cards started showing the browser's broken-image icon, but only on devices that hadn't visited before. On my laptop, everything looked fine.

Two problems were stacked. The cover files had gone missing from the API's media volume, so their URLs returned 404. On my machine I couldn't see it, because media was served with a year-long immutable cache and my browser was still painting the images it had cached when I uploaded them.

The rendering half was more interesting. I'd added an (error) handler to swap in fallback art. It never ran. With prerendering, the <img> tag is in the HTML from the start, so the browser requests the image while it's still parsing. On a fast connection, the 404 comes back before the JavaScript bundle has even loaded. By the time Angular hydrates and attaches the listener, the error event has already happened.

The fix is to look for the evidence the failure leaves behind:

@Directive({
  selector: 'img[pfImageFailure]',
  host: { '(error)': 'failed.emit()' },
})
export class ImageFailure {
  private readonly elementRef = inject<ElementRef<HTMLImageElement>>(ElementRef);
  readonly failed = output<void>();

  constructor() {
    afterNextRender(() => {
      const image = this.elementRef.nativeElement;
      // Finished loading with no intrinsic width = it failed before we were listening
      if (image.complete && image.naturalWidth === 0) {
        this.failed.emit();
      }
    });
  }
}

The (error) binding handles failures that happen after hydration, and the check in afterNextRender catches the ones that happened before it. In the admin editor I did the opposite: a broken cover now says so in plain words, because that's the one screen that can fix it.


4. The refresh that signed me out

11 September. I'd go to my admin's traffic page, hit refresh, and land on the login page. Clicking through from the dashboard worked. Bookmarks, refreshes and pasted links all logged me out.

The URL was being served as a 258-byte static file containing this:

<meta http-equiv="refresh" content="0; url=/admin-login">

My app.routes.server.ts listed every admin route as RenderMode.Client, one by one. Except admin/traffic, which I'd added later and forgot to list. It fell through to the catch-all ** Prerender rule. At build time Angular rendered the page, the auth guard ran on the build machine where no token exists, the guard returned a redirect, and that redirect was written to disk as the page. nginx then served the file ahead of the app shell on every hard load.

The fix was to stop listing routes one by one:

export const serverRoutes: ServerRoute[] = [
  { path: 'blog/:slug', renderMode: RenderMode.Prerender, getPrerenderParams: /* slugs */ },
  { path: 'admin-login', renderMode: RenderMode.Client },
  { path: 'admin', renderMode: RenderMode.Client },
  { path: 'admin/**', renderMode: RenderMode.Client },
  { path: '**', renderMode: RenderMode.Prerender },
];

A prefix can't go stale when the next admin page gets added. I also checked the build output: no HTML under admin/ at all, and no file anywhere containing url=/admin-login.

Lesson: anything behind a login should never be prerendered. Your guards will run at build time, and whatever they return becomes the page.


5. Share cards WhatsApp couldn't see

17 September. Sharing a post on WhatsApp showed a link with no preview image. The tags were in the page. View-source showed og:title and og:image, correct and complete.

They were just far down. Angular inlines critical CSS into <head> to speed up first paint, and the Meta service appends tags after it. On my pages, og:title landed around byte 22,900 and og:image at byte 23,526, behind 27 KB of <style>. Link-preview crawlers fetch a limited prefix of a document, not the whole thing, so the tags were past the point where some of them stop reading.

There was a second, simpler problem: one older post's card pointed at an image file that had never been committed, so it was a 404 in production. Fixing that alone would have hidden the real cause until the CSS grew again.

The fix is a small postbuild script that reorders <head>: charset and viewport first, then title, description, canonical, og:* and twitter:*, then everything else. og:image moved to byte 1,072. I verified it was a pure reorder: same body bytes, same set of head tags, same stylesheet order, and running it twice changes nothing.

If you're working on how a site shows up in search and in AI answers, this kind of plumbing matters more than people expect. I covered the content side in SEO vs GEO: how to get cited by ChatGPT and Google AI Overviews.


6. The pattern behind all five

Every one of these bugs came from the same gap: code written for a browser, running somewhere without one, or running at a different time than I assumed.

Bug What I assumed What actually happened
Host header A server renders every request A server rejected production traffic
Stale posts Data is fetched when the page loads Data was fetched when the site was built
Image errors Angular is listening when the image fails The image failed before Angular loaded
Logout on refresh Guards run when a user visits Guards ran on a build machine with no user
Share cards Tags in <head> are tags that get read Tags 23 KB deep weren't read

Two more from the same family: my scroll-reveal directive briefly hid the prerendered hero on load, and later kept the whole blog list invisible on phones for a month. I wrote up the second one in IntersectionObserver not firing? My blog was blank on phones for a month.


7. A pre-launch checklist for prerendered Angular

Before you ship a prerendered Angular site, check these against the built output rather than ng serve:

  • Every authenticated route is RenderMode.Client, matched by prefix. Then search dist/ for redirect URLs to be sure.
  • View-source on the live domain, not localhost. Real content should be in the HTML.
  • Publish something new and confirm it appears without a rebuild, or that a rebuild is triggered automatically.
  • Break an image URL on purpose and load the page in a private window.
  • Check where og:image sits in <head>. curl -s https://yoursite/page | grep -bo 'og:image' prints the byte offset. Keep it in the first few KB.
  • Test the share preview in WhatsApp and LinkedIn's Post Inspector, not only in your browser.
  • Load every main page at phone width and scroll to the bottom.

Most of this takes ten minutes, and it would have caught all five of these before a visitor did. For where code like this should live in an Angular project, see how I structure an Angular frontend.


FAQ

What is prerendering in Angular?

Prerendering (static site generation) renders each route to HTML at build time, so the server sends finished pages instead of an empty shell. In Angular 19 and later you control it per route with RenderMode.Prerender in app.routes.server.ts.

What's the difference between SSR and prerendering in Angular?

SSR renders a page on the server for every request. Prerendering renders it once, at build time. Prerendering is faster and needs no running server, but content is only as fresh as the last build unless you refresh it in the browser.

Why does my Angular auth guard redirect during the build?

Because prerendering runs guards on the build machine, where there's no logged-in user. Mark authenticated routes RenderMode.Client so they're never prerendered.

Why doesn't my Angular image (error) event fire?

With prerendered HTML, the image can fail to load before Angular hydrates and attaches the listener. Check img.complete && img.naturalWidth === 0 after hydration to catch failures that already happened.

Check that the image URL returns 200, and that the tags appear early in <head>. Link-preview crawlers often read only the first part of a page, and inlined CSS can push the tags past that point.


Shipping an Angular app soon?

I build Angular frontends with .NET backends for founders and product teams, and I've already hit most of these the hard way. If you want a second pair of eyes on a prerendered build before launch, get in touch.