CDN for Ruby on Rails

Deliver precompiled assets from a CDN hostname. Use their fingerprints to cache across releases and review access requirements before caching uploaded files.

Example cache policy
Public, cacheable resource
/assets/application-a13d.css
Bypass shared cache
/account/billing

Serve fingerprinted Rails assets

Set asset_host to Cloud CDN. Cache public Active Storage URLs. Cache anonymous pages where possible. Bypass session views and POST.

Digested assets at the edge

Fingerprinted files from Propshaft or Sprockets can be cached independently of Rails page responses.

Public blobs worldwide

Active Storage public variants can be cached and transformed at the edge.

Session views stay in Rails

Devise, dashboards and CSRF forms are not cached.

Configuration

Cloud CDN overview
  1. Set config.asset_host

    Point digested assets at the Cloud CDN hostname.

  2. Cache public storage

    Put public Active Storage URLs on the same distribution or a media hostname.

  3. Optionally cache public HTML

    Marketing pages can be cached with purge on deploy.

  4. Bypass authenticated Rails

    Keep session cookies out of the cache key for app HTML.

Operational outcomes

  • Rails assets are delivered globally
  • Public storage is cached at the edge
  • Session HTML remains dynamic
  • Puma spends less time on static files

Frequently asked questions

Does this work with Propshaft and sprockets?

Yes. Both emit digest filenames that Cloud CDN can long-cache.

Can I cache Russian-doll fragment output?

Fragment cache remains in Rails. Cloud CDN caches full anonymous responses you mark as public.

How should I treat private Active Storage?

Keep private blobs on signed origin URLs. Cache public blobs only.

Is this compatible with Turbo?

Cache full documents that are shareable. Turbo Drive requests that vary on session should bypass cache.