Created
May 2, 2010 13:17
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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