Content websites read far more than they write: a page may be viewed thousands of times between edits. Caching turns that into speed, as long as a published change shows up quickly. Here is how the layers fit together.
The three caches
| Layer | Where | Good for |
|---|---|---|
| Browser | The visitor's device | Images, fonts, scripts with versioned URLs |
| CDN / edge | Servers near the visitor | Whole HTML pages and assets |
| Application | Your server | Results of database or API calls |
Each can serve a request without asking the layer behind it.
Cache-Control in practice
Versioned assets (the file name changes when the content does):
Cache-Control: public, max-age=31536000, immutable
HTML pages on a CDN, fresh for a minute, then refreshed in the background:
Cache-Control: public, s-maxage=60, stale-while-revalidate=600
s-maxage applies to shared caches (the CDN) only, so browsers still revalidate. stale-while-revalidate lets the CDN serve the cached page instantly while it fetches a fresh copy.
Personal pages (accounts, carts):
Cache-Control: private, no-store
Publishing without stale pages
Time-based expiry alone means a fixed delay after every edit. Better: invalidate on publish.
- Tag-based invalidation: tag cached data with what it depends on (
page:about,article:42,menu). When an editor publishes, purge those tags. Frameworks like Next.js support this with cache tags and revalidation calls. - Purge by URL on the CDN for the pages that changed, plus listings that show them (the blog index, category pages, the sitemap).
Combine both: a short time limit as a safety net, and an explicit purge for speed.
What not to cache
- Responses that include personal data or a session.
- Error pages, for long (cache them for seconds at most, or a broken page lingers).
- Anything that differs per visitor without a
Varyheader that describes how.
Measuring the effect
Look at time to first byte (TTFB) before and after. Cached HTML at the edge typically answers in tens of milliseconds instead of hundreds. Your CDN's hit ratio should be well above 80% for a content site.
A setup that works for most sites
- Long-lived immutable caching for assets with hashed names.
- HTML at the CDN with
s-maxageof 1 to 5 minutes andstale-while-revalidate. - Server-side data cached with tags, invalidated when content is published.
- A publish hook that purges the changed URLs at the CDN.
Then watch it from the outside with an uptime check and from real visitors with real user monitoring, so you notice if a cache misconfiguration makes things slower or serves the wrong page.
What does stale-while-revalidate do?
It lets a cache serve an expired copy immediately while it fetches a fresh one in the background, so visitors never wait for the refresh.
How do I show edits quickly with caching?
Invalidate on publish: purge the cache tags or URLs the change affects, and keep a short time-based expiry as a safety net.
Should HTML be cached in the browser?
Usually only briefly or not at all. Cache HTML at the CDN, where you can purge it, and assets in the browser with versioned URLs.
Try VitalRix free
Uptime monitoring, APM, logs, real user monitoring and status pages in one place. No credit card needed.