Prompt
How do I make static files load faster with edge caching?
Latest observation
To make static files load faster with edge caching, you want the files cached at CDN/edge locations close to users, so repeat requests don’t have to hit your origin server.
What to do
1. Put static assets behind a CDN
Use a CDN like Cloudflare, Fastly, CloudFront, Akamai, etc. Point your static file URLs there so they can be cached at the edge.
2. Set long-lived cache headers
For versioned static files like app.8f3a1.js or styles.9c2d.css, send:
Cache-Control: public, max-age=31536000, immutable
This tells browsers and CDNs to keep the file for a long time.
3. Use fingerprinted filenames
If a file changes, change its name:
main.js→main.abc123.jsstyles.css→styles.def456.css
That way you can cache aggressively without worrying about stale content.
4. Add edge cache headers if needed
Depending on your CDN, you may also need headers like:
CDN-Cache-Control: public, max-age=31536000
Surrogate-Control: max-age=31536000
These are useful when you want CDN caching behavior different from browser caching.
5. Make sure your origin supports caching efficiently
Enable:
- Brotli/gzip compression
- HTTP/2 or HTTP/3
- Correct
Content-Type - ETags or Last-Modified if you need revalidation
6. Avoid cache-busting query strings for versioning
Prefer filenames over URLs like:
/app.js?v=123
Some CDNs and caches handle query strings differently, and filename hashing is usually more reliable.
7. Set proper cache rules for different asset types
Typical approach:
- Hashed JS/CSS/images/fonts: cache for 1 year
- HTML: cache briefly or not at all, so content updates quickly
Example:
# HTML
Cache-Control: no-cache
# Static assets
Cache-Control: public, max-age=31536000, immutable
8. Verify with response headers
Check headers in browser dev tools or with curl:
curl -I https://example.com/static/app.abc123.js
Look for headers like:
cache-controlagecf-cache-status(Cloudflare)x-cache/x-cache-hitvia
An Age header or HIT status means the edge cache is working.
Best-practice pattern
- HTML: short cache / revalidate
- Static assets: hashed names +
Cache-Control: max-age=31536000, immutable - Serve through CDN
- Use compression and modern protocols
If you want, I can show you how to configure this for a specific platform like Cloudflare, AWS CloudFront, Nginx, or Apache.