Caching and CDN Strategies for Fast Static Sites
The best request is the request the browser does not need to make again.
Key takeaways
- The fastest request is the one the browser never makes — caching strategy should be the first performance lever you pull, not the last.
- Immutable, content-hashed assets can be cached forever; HTML almost never should be.
- A CDN's value is proximity and offload, not magic — it only helps as much as your cache headers let it.
- Stale-while-revalidate gives you near-instant repeat loads without the staleness risk of caching HTML forever.
Two kinds of assets, two completely different rules
Nearly every caching mistake comes from treating all assets the same way. In reality there are two categories, and they want opposite treatment.
| Asset type | Example | Cache-Control |
|---|---|---|
| Content-hashed, immutable | app.a1b2c3.js, style.9f8e7d.css | public, max-age=31536000, immutable |
| Mutable, must revalidate | index.html, API responses | public, max-age=0, must-revalidate |
The trick that makes the first row safe is the hash in the filename. Because the filename changes whenever the content changes, caching it for a year is completely safe — a stale cached copy of app.a1b2c3.js is simply the old version, never wrong content served under a new name. HTML, by contrast, is the file that tells the browser which hashed filenames to request next, so it needs to be checked on every visit — caching it for even a few minutes risks serving a page that points at asset versions which no longer exist.
Reading a Cache-Control header correctly
Cache-Control: public, max-age=86400, stale-while-revalidate=604800
public— any cache (browser, CDN, proxy) may store this response.max-age=86400— treat the response as fresh for 24 hours without checking the origin.stale-while-revalidate=604800— after freshness expires, keep serving the stale copy instantly while fetching a new one in the background, for up to 7 more days.
stale-while-revalidate is the most underused header in static-site performance. It gives you the speed of aggressive caching with almost none of the staleness risk, because every visit after the first triggers a background refresh.
What a CDN is actually doing for you
A CDN is a network of edge servers that cache your responses geographically close to visitors and absorb traffic before it reaches your origin. Its benefit is entirely proportional to how cacheable your responses are — a CDN sitting in front of uncacheable, always-dynamic responses provides almost no benefit beyond basic proxying.
| What the CDN gives you | Depends on |
|---|---|
| Lower latency (geographic proximity) | Edge network size — mostly out of your control |
| Origin offload (fewer requests hit your server) | Your Cache-Control headers being correct |
| Resilience during traffic spikes | Cache hit rate at the edge |
| Free TLS, HTTP/2 or HTTP/3, compression | Usually enabled by default on any modern CDN |
Watch out: putting a CDN in front of a site and leaving Cache-Control unset or overly conservative is one of the most common "we added a CDN and nothing got faster" stories. The CDN can only cache what your headers permit it to.
Cache invalidation for static sites
Content-hashed filenames make most manual invalidation unnecessary — a new deploy simply ships new filenames, and old cached files are never requested again. What still needs explicit handling is HTML and any unhashed asset: either keep its cache lifetime short (seconds to minutes) or purge the CDN cache on deploy. Most static-site CDNs (Cloudflare Pages, Netlify, Vercel) purge automatically on deploy — verify this rather than assuming it.
See self-hosting vs serverless for how this interacts with the platform choice itself — some hosts handle cache invalidation transparently, others require you to configure it.
A sane default configuration
| Path | Cache-Control |
|---|---|
/assets/* (hashed filenames) | public, max-age=31536000, immutable |
*.html | public, max-age=0, must-revalidate |
/api/* (dynamic, cacheable briefly) | public, max-age=60, stale-while-revalidate=300 |
sitemap.xml, robots.txt | public, max-age=3600 |
The best request is the request the browser doesn't need to make again. Everything else — the CDN, the edge network, the compression — is optimizing the requests you couldn't avoid.
Caching strategy and CDN choice work together, but in that order: fix your Cache-Control headers first, and the CDN you already have will start earning its keep. Adding a CDN in front of bad headers just moves the same problem geographically closer to the user.
If repeat visits are as slow as first visits, missing cache headers are one of nine common causes covered in why your website is so slow, along with a quick way to tell which one is yours.
Most cloud platforms sell a CDN as a managed service. The cloud computing and AWS guide shows where one fits next to storage and compute.
Frequently asked questions
Why can content-hashed assets be cached for a year?
Because the filename itself changes whenever the content changes, a cached copy of a hashed filename is never stale — it is simply the version that filename has always referred to.
What does stale-while-revalidate actually do?
It serves the cached response instantly even after it is technically stale, while fetching a fresh copy in the background for the next visit — combining speed with eventual freshness.
Will adding a CDN automatically make my site faster?
Only as much as your Cache-Control headers allow. A CDN in front of uncacheable or overly conservative headers provides little benefit beyond basic proxying.