Prompt
I'm building a site with lots of JS and CSS; how should I structure CDN caching?
Latest observation
For a JS/CSS-heavy site, the best CDN caching strategy is usually:
1) Treat static assets as immutable
Version your files so they can be cached for a very long time.
Examples:
/assets/app.3f2c1a.js/assets/styles.a91b8c.css
Or use queryless hashed filenames rather than relying on query strings.
Cache headers for versioned assets
Use something like:
Cache-Control: public, max-age=31536000, immutable
This tells browsers and CDNs:
- cache for 1 year
- the file will never change at that URL
- new content gets a new filename
2) Separate “versioned static” from “HTML”
Keep HTML short-lived, because it references the latest asset versions.
HTML cache headers
Usually:
Cache-Control: no-cache
or
Cache-Control: public, max-age=0, must-revalidate
If you want edge caching for HTML too, use a short TTL plus revalidation, e.g.:
Cache-Control: public, max-age=60, stale-while-revalidate=30
3) Don’t cache user-specific responses publicly
Anything personalized should either:
- not be cached
- or vary carefully by auth/session
Examples:
- dashboards
- account pages
- cart contents
- API responses with user data
Use:
Cache-Control: private, no-store
or at least:
Cache-Control: private, no-cache
depending on whether you want browser-only caching.
4) Use CDN rules by asset type
A good pattern:
JS/CSS/images/fonts
- long TTL
- immutable
- cache at CDN and browser
Cache-Control: public, max-age=31536000, immutable
HTML
- short TTL or no-cache
- revalidate frequently
Cache-Control: public, max-age=0, must-revalidate
API JSON
- depends on whether response is shared or personalized
- many APIs should be
no-storeor short TTL
5) Configure CDN to respect origin headers
Prefer to make the origin send correct cache headers and have the CDN honor them. This reduces surprises.
If your CDN supports it, also set:
- cache key: avoid including cookies/query params unless needed
- origin shield / tiered caching: improves hit rate
- compression: enable Brotli/gzip for JS/CSS
6) Avoid cache-busting with random query strings
Bad:
/app.js?v=123/app.js?cache=999
Some CDNs and proxies handle query strings inconsistently, and it’s less reliable than filename hashing.
7) Use stale-while-revalidate where appropriate
For assets or HTML that can tolerate brief staleness:
Cache-Control: public, max-age=300, stale-while-revalidate=86400
This lets users get fast responses while the CDN refreshes in the background.
8) Invalidate only when necessary
With hashed filenames, you usually don’t need purges for JS/CSS releases:
- deploy new files
- update HTML references
- old files can stay cached forever
Use CDN purges mainly for:
- critical HTML fixes
- broken assets that were published incorrectly
- content that changes without a URL change
Recommended baseline
For versioned JS/CSS:
Cache-Control: public, max-age=31536000, immutable
For HTML:
Cache-Control: public, max-age=0, must-revalidate
For personalized data:
Cache-Control: private, no-store
If you want, I can give you:
- a recommended caching matrix for HTML/JS/CSS/images/API,
- Nginx/Apache/CDN config examples, or
- a setup tailored to Next.js, React, Vue, or static hosting.