Skip to content

Instantly share code, notes, and snippets.

@stephanschubert
Created May 2, 2010 13:17
Show Gist options
  • Save stephanschubert/387116 to your computer and use it in GitHub Desktop.
Save stephanschubert/387116 to your computer and use it in GitHub Desktop.
Rails Helper Module: Encodes the asset's timestamp into the request URL as directory which will be removed by Apache's mod_rewrite
module CacheHelper
#
# Implements Google's "Leverage Proxy Caching" tip:
# Instead of using a query parameter as cache buster -
# encode it into the URL to ensure public proxies can
# cache it too.
#
# NOTE: Activates in production mode only because it's
# not necessary for development and needs a mod_rewrite
# rule for Apache anyway:
#
# RewriteEngine On
# RewriteRule ^/\d{5,}(/.+)$ $1
#
# This will remove the (fake) directory named after
# the cache buster from the front of the incoming URL.
#
def rewrite_asset_path(*args)
source = super
if Rails.env.production?
path, cache_buster = source.split("?")
if cache_buster.blank?
source
else
"/#{cache_buster}#{path}"
end
else
source
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment