Lesson 6 / 25
Caching strategies
Use HTTP caching, service workers, and cache headers to serve content faster on repeat visits.
HTTP Cache headers
Use Cache-Control: public, max-age=31536000 for static assets. Set short TTL (e.g., 1 hour) for HTML. Add ETag or Last-Modified for cache validation. Example: Cache-Control: max-age=3600, must-revalidate.
CDN and edge caching
Store static assets on geographically distributed servers. CDN reduces latency by serving from closest location. Use cache purging to instantly invalidate stale content when deployments happen.
Client-side caching with Service Workers
Intercept network requests and serve cached versions offline. Use cache strategies: Cache First (for static), Network First (for dynamic), Stale-While-Revalidate (serve stale, update in background).
Cache invalidation strategies
Use content hashing in filenames (app.a1b2c3d.js) for cache busting. Immediately invalidate on deploy. Monitor cache hit rates (target >90%). Balance between aggressiveness and freshness.
Quick check
Quick check: What is a good cache TTL for static assets?
- 1 year (31536000 seconds)
- 1 hour
- No caching