Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@gbuesing
Forked from jeremy/gist:4211803
Created March 27, 2014 18:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gbuesing/9815078 to your computer and use it in GitHub Desktop.
Save gbuesing/9815078 to your computer and use it in GitHub Desktop.

Declared ETags, together with Russian Doll caching, can be used to automatically mix your template and asset versions into the ETags set in your controllers. This avoids the need to blow all browser caches on each deploy and neatly contains the scope of "freshness fallout" when you tweak a view.

To include the template's version in the ETag:

  # Incorporate the cache version for this action into our ETag.
  # This allows template changes to bubble up into HTTP cache
  # freshness and bust browser caches when we make changes.
  etag do
    begin
      CacheDigests::TemplateDigestor.digest(
        "#{controller_name}/#{action_name}",
        request.format.try(:to_sym), lookup_context)
    rescue ActionView::MissingTemplate => e
      '' # Ignore missing templates
    end
  end

Similarly, for your JavaScript and CSS assets:

  ASSET_FRESHNESS_PATHS = %w( application.js application.css )

  # HTML pages cached by the browser should be updated whenever we change
  # our assets, so we include application.css and application.js digests
  # in ETags for HTML pages.
  etag do
    if request.format.try(:html?)
      ASSET_FRESHNESS_PATHS.map { |p| asset_digest_for_etag(p) }.join('-')
    end
  end

  # Check precompiled asset manifest (production) or compute the digest (dev).
  def asset_digest_for_etag(logical_path)
    if manifest = Rails.application.config.assets.digests
      manifest[logical_path]
    else
      Rails.application.assets[logical_path].digest
    end
  end

Coming in Rails 4!

Declared ETags: https://github.com/rails/rails/commit/ed5c938fa36995f06d4917d9543ba78ed506bb8d

Russian Doll caching: https://github.com/rails/cache_digests

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment