patternjavascriptModerate
CDN cache invalidation: purging stale content after deployments
Viewed 0 times
CDNcache invalidationCloudflarecache-controlfingerprintingpurgedeployment
Problem
After deploying new static assets or content, users continue to receive cached old versions from the CDN for the duration of the cache TTL.
Solution
Use one of these strategies:
Strategy 1: Content-addressed filenames (best for JS/CSS)
Strategy 2: API-based purge after deploy (Cloudflare)
Strategy 3: Cache-Control headers for HTML
Strategy 1: Content-addressed filenames (best for JS/CSS)
// In your build tool (webpack/vite), enable content hashing
// Output: main.a3f8d2b1.js — filename changes when content changes
// Cache-Control: public, max-age=31536000, immutableStrategy 2: API-based purge after deploy (Cloudflare)
curl -X POST "https://api.cloudflare.com/client/v4/zones/$ZONE_ID/purge_cache" \
-H "Authorization: Bearer $CF_API_TOKEN" \
-H "Content-Type: application/json" \
--data '{"purge_everything":true}'Strategy 3: Cache-Control headers for HTML
location ~* \.html$ {
add_header Cache-Control "no-cache, must-revalidate";
}
location ~* \.(js|css|png|jpg|woff2)$ {
add_header Cache-Control "public, max-age=31536000, immutable";
}Why
HTML files should never be cached aggressively since they reference JS/CSS by filename. Fingerprinted assets can be cached forever because a new deploy generates a new filename.
Gotchas
- Purge everything is a nuclear option — targeted URL purges are safer for large sites
- CDN purge APIs are eventually consistent — it can take seconds to minutes to propagate globally
- Service workers cache aggressively and can serve stale content even after CDN purge
- Preview deployments and production must use separate cache keys
Revisions (0)
No revisions yet.