Module: Vidibus::AssetCacheBuster

Defined in:
lib/vidibus/asset_cache_buster.rb,
lib/vidibus/asset_cache_buster/version.rb

Overview

Rewrite cache buster for assets. To get this solution working, it is required to add some configuration to your apache2 VirtualHost:

# Remove timesstamp from versioned static javascripts, stylesheets, and images. # This requires a change in Rails so that assets will be included as image.12345.jpg RewriteEngine on RewriteRule ^/(javascripts|stylesheets|images)/(.+).(.+).(js|css|jp?g|gif|png)$ /$1/$2.$4 [L]

This rewrite does not support SSL. Maybe Apache2 config should be improved…

Constant Summary collapse

VERSION =
"0.0.1"

Instance Method Summary collapse

Instance Method Details

#asset_file_path(path) ⇒ Object

Only works with integer timestamps!



16
17
18
19
20
21
22
# File 'lib/vidibus/asset_cache_buster.rb', line 16

def asset_file_path(path)
  return super if request.ssl?
  if path.match(/^(.+?)(\.\d+)?(\..+)$/)
    path = "#{$1}#{$3}"
  end
  File.join(config.assets_dir, path)
end

#rewrite_asset_path(source, path = nil) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/vidibus/asset_cache_buster.rb', line 24

def rewrite_asset_path(source, path = nil)
  return super if request.ssl?
  if path && path.respond_to?(:call)
    return path.call(source)
  elsif path && path.is_a?(String)
    return path % [source]
  end

  asset_id = rails_asset_id(source)
  if asset_id.blank?
    source
  else
    source.gsub(/^(.+)(\..+)$/,"\\1.#{asset_id}\\2")
  end
end