Last updated
Web Caching Strategies
Caching stores copies of resources to reduce load times and server load.
The right caching strategy depends on how frequently the resource changes
and how critical it is to serve the latest version.
HTTP caching is controlled by response headers: Cache-Control,
ETag, and Last-Modified.
Cache-Control Directives
| Directive | Effect |
|---|---|
max-age=3600 | Cache for 1 hour |
no-cache | Revalidate before using cached copy |
no-store | Never cache (sensitive data) |
public | CDN and browser can cache |
private | Browser only (not CDN) |
immutable | Never revalidate (content-hashed files) |
stale-while-revalidate | Serve stale while fetching fresh |
Caching Strategy by Resource Type
Text
# HTML pages — short cache, revalidate
Cache-Control: no-cache
# Versioned assets (JS/CSS with hash in filename)
Cache-Control: public, max-age=31536000, immutable
# Images (no hash)
Cache-Control: public, max-age=86400, stale-while-revalidate=604800
# API responses
Cache-Control: private, no-cache
# Static fonts
Cache-Control: public, max-age=31536000, immutable