Skip to content

How to use a CDN well

Set correct Cache-Control headers, use versioned URLs, purge by tag not by URL, and don't cache what you can't invalidate.

A CDN that helps, not hurts

  1. Cache-Control is the contract

    public, max-age=31536000, immutable for hashed assets. no-store for private data. The CDN obeys the header.

  2. Versioned URLs

    /app.css?v=1234 or /app.a1b2c3.css. When you deploy, the URL changes, the CDN sees a new resource.

  3. Purge by tag, not by URL

    Tag assets with their deployment ID. purge tag:release-42 is instant; purging URL-by-URL takes minutes.

  4. Stale-while-revalidate

    Serve stale content while fetching fresh in the background. Users never wait; the cache updates on its own.