3 ms·
Nice fix! Declared ETags, together with Russian Doll caching, take the next step: automatically mixing your template and asset versions into your ETags. To in
by jeremykemper 14y ago
Nice fix!
Declared ETags, together with Russian Doll caching, take the next step: automatically mixing your template and asset versions into your ETags.
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
This avoids the need to use global versions that blow all browser caches. Neatly contains the scope of "freshness fallout" when you tweak a view and deploy.