Cache-Control & CSP Header Builder

1. HTTP Cache-Control Builder



            

2. Content-Security-Policy (CSP) Builder



    

Cache-Control & CSP Best Practices

Combining public, max-age=31536000, immutable for hashed static bundles (app.a1b2c3.js) with no-store on authenticated API responses maximizes CDN hit rates without risking stale user data. Meanwhile, a strict Content-Security-Policy with frame-ancestors 'none' mitigates XSS script injection and clickjacking attacks.

Cache-Control Options & How They Interact (FAQ)

  • public vs. private: public allows both browsers and shared proxies/CDNs (Cloudflare, CloudFront, Nginx proxy cache) to store the response. private restricts caching to the end user's browser only - CDNs will refuse to cache it even if max-age is high. Always use private (or no-store) for user-specific HTML or API responses.
  • max-age vs. s-maxage: max-age sets the freshness lifetime in seconds for both browsers and CDNs. Adding s-maxage overrides max-age only for shared CDN caches. For example, public, max-age=60, s-maxage=3600 lets browsers cache for 1 minute while the CDN caches for 1 hour (which you can purge instantly on deploy).
  • stale-while-revalidate (SWR): Allows caches to serve an expired response immediately while fetching a fresh copy asynchronously in the background. Best paired with a short max-age or s-maxage (e.g., max-age=60, stale-while-revalidate=86400) for instant page loads. Do not combine with must-revalidate if you want stale-serving during revalidation.
  • immutable: Tells the browser the file will never change during its max-age window, so even pressing Reload (F5) skips conditional 304 Not Modified checks. Rule: pair immutable only with fingerprinted filenames (main.9f3a1c.css); never use it on index.html or unversioned URLs.
  • no-cache vs. no-store: Despite its name, no-cache does store the response, but forces the cache to revalidate with the origin (ETag / Last-Modified) before serving it - ideal for HTML entrypoints. no-store forbids storing the response anywhere (memory/disk/CDN) and overrides max-age, making it mandatory for sensitive tokens, PII, and financial data.
  • must-revalidate: Strictly forbids serving a stale response after max-age expires, even if the origin server is temporarily down or unreachable.

Content-Security-Policy (CSP) Directives (FAQ)

  • default-src: The fallback rule for any fetch directive you do not specify explicitly (font-src, media-src, object-src, etc.). Setting default-src 'self' establishes a safe allowlist baseline.
  • script-src: Controls which JavaScript files and inline scripts can execute. Important: directives do not merge with default-src - if you set script-src https://cdn.example.com without 'self', your own same-origin scripts will be blocked! Avoid 'unsafe-inline' here whenever possible.
  • style-src: Controls CSS loading. Legacy frameworks or inline style="..." attributes often need 'unsafe-inline' unless you migrate all inline styles to external stylesheets or nonces.
  • img-src: Controls image sources. Adding data: is required if your app renders inline Data URIs (such as canvas exports or base64 icons), and https: allows external HTTPS images without loosening script security.
  • connect-src: Restricts fetch(), XMLHttpRequest, WebSockets (wss:), and EventSource. Remember to list external API origins or analytics endpoints here so browser requests are not blocked.
  • frame-ancestors: Controls which sites can embed your page in an <iframe> (modern replacement for X-Frame-Options). Unlike fetch directives, frame-ancestors never falls back to default-src, so set 'none' or 'self' explicitly to block clickjacking.
  • upgrade-insecure-requests: Instructs the browser to automatically upgrade legacy http:// subresource URLs on an HTTPS page to https:// before requesting them, preventing mixed-content warnings.
Please read the privacy policy (agreement of the cookies usage, other websites embedded content, etc.). If you continue to use the site, we will assume that you agree with our privacy policy.
OkPrivacy Policy