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.
- SPA fallback. A URL like
/courses/42exists only in the router, not as a file. A request for it must returnindex.html, and then the router renders the right page. Configure the server: nginxtry_files $uri $uri/ /index.html;, a rewrite on a CDN, or the static host's "single page app" setting. Real missing assets (a.jsor.pngthat does not exist) should still return 404, so do not rewrite those. - 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.htmlmust not be cached for long:Cache-Control: no-cache(revalidate every time), or a very shortmax-age. Ifindex.htmlis cached, users stay on an old version, and may request hashed files that you already deleted. - 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).
- 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 thatindex.html. Relative asset URLs and the router depend on it. - Compression and protocols: gzip or brotli, HTTP/2 or 3, and a CDN close to users.
- Environment config: for one build many environments, load a
config.jsonat start-up (not cached) instead of rebuilding. - 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.