← Back to Angular, page 7

angular-0138 · Hard senior

How do you deploy an Angular app to a static host or CDN, and how do SPA fallback, caching headers, base href and hashed file names fit together?

ng build outputs static files with content hashes in their names. Serve index.html with no-cache so users get new versions, serve hashed JS and CSS with a long immutable cache, send unknown paths back to index.html for client side routes, and set the base href when the app lives under a sub path.

Full answer

A client-side Angular app is a set of static files. The deployment problems are routing, caching, and versions.

Build output: with ng build (production configuration) the files in the browser folder have content hashes: main-AB12CD34.js. The name changes whenever the content changes. index.html references these names.

  1. SPA fallback. A URL like /courses/42 exists only in the router, not as a file. A request for it must return index.html, and then the router renders the right page. Configure the server: nginx try_files $uri $uri/ /index.html;, a rewrite on a CDN, or the static host's "single page app" setting. Real missing assets (a .js or .png that does not exist) should still return 404, so do not rewrite those.
  2. Caching. Hashed files can be cached for a year with Cache-Control: public, max-age=31536000, immutable, because a new version has a new name. index.html must not be cached for long: Cache-Control: no-cache (revalidate every time), or a very short max-age. If index.html is cached, users stay on an old version, and may request hashed files that you already deleted.
  3. Old versions. Keep old hashed files for a while after a deployment. A user with an open old tab may still load a lazy chunk from the previous version, which would 404 if deleted. This is also why a "new version available" prompt helps (service worker or version check).
  4. Base href. If the app is served from https://example.com/school/, build with --base-href /school/ and make sure the server fallback points to that index.html. Relative asset URLs and the router depend on it.
  5. Compression and protocols: gzip or brotli, HTTP/2 or 3, and a CDN close to users.
  6. Environment config: for one build many environments, load a config.json at start-up (not cached) instead of rebuilding.
  7. Server rendering changes this: you deploy a Node server (or edge function) plus static files, and caching rules apply to HTML responses too.

CI/CD checklist: build once, run tests, publish the artifact, deploy to staging, run smoke tests, promote the same artifact to production, and keep a rollback path (previous version still available).

typescript
// file: headers.ts
export function cacheControlFor(path: string): string {
  if (path === '/' || path.endsWith('.html') || path.endsWith('config.json')) {
    return 'no-cache';
  }
  if (/-[A-Za-z0-9]{8}\.(js|css|woff2|png|svg|webp)$/.test(path)) {
    return 'public, max-age=31536000, immutable';
  }
  return 'public, max-age=3600';
}

for (const p of ['/', '/index.html', '/main-AB12CD34.js', '/favicon.ico', '/config.json']) {
  console.log(p.padEnd(20), cacheControlFor(p));
}

// file: nginx.conf
server {
  listen 80;
  root /usr/share/nginx/html;
  index index.html;

  location = /index.html { add_header Cache-Control "no-cache"; }
  location ~* \.(js|css|woff2|png|svg|webp)$ {
    try_files $uri =404;                       # missing assets stay 404
    add_header Cache-Control "public, max-age=31536000, immutable";
  }
  location / { try_files $uri $uri/ /index.html; }   # client side routes
}

The sample is an nginx configuration and a headers helper. The helper function was run in Node twice with identical output. The nginx file is an example and was not run against a server here.

Key points

  • Hashed file names allow long immutable caching. `index.html` must be revalidated.
  • SPA fallback returns `index.html` for client routes but still returns 404 for missing assets.
  • Keep old chunks for a while. Set `--base-href` for sub paths.
  • Build once, promote the same artifact, and keep a rollback.

Common mistakes

  • Caching `index.html` for a long time and trapping users on an old version.
  • Rewriting every 404 (including missing JS files) to `index.html`.
  • Deleting old chunks immediately after deployment.

Follow-ups

  • Why does a missing chunk cause a "ChunkLoadError", and how do you handle it?
  • How does the base href affect the router and assets?
  • What changes when you add SSR?

Tips

  • State the hashed names plus long cache plus no-cache `index.html` rule.
  • Mention old chunks and open tabs.
  • End with the build-once promote-many practice.

Evaluation rubric

  • Covers fallback, caching, base href and versions correctly.
  • Explains the reasons, not only settings.
  • Mentions CI/CD practices.