Improve Rails Asset Caching with MD5 and virtual folders
Ruby
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Failed to load latest commit information.
lib
test
Gemfile
Gemfile.lock
Rakefile
Readme.md
VERSION
init.rb
virtual_asset_path.gemspec

Readme.md

Improve Rails Asset Caching with MD5 and virtual folders

  • MD5 only changes on content-change and is identical on all servers
  • Virtual folders are CDN/proxy/... cacheable and transparent via nginx/apache rewrite rule

Results

/foo/bar.jpg ... will be ...

# default + simple rewrite rules
/asset-v1afd23/foo/bar.jpg  # style: folder   method: MD5

# rails 3.1-ish, but complicated rewrite rules
/foo/bar-asset-v1afd23.jpg  # style: suffix   method: MD5

# no rewrite rule needed (great for development)
/foo/bar.jpg?1afd23         # style: query    method: MD5

# different methods ...
/asset-v1308942465/foo/bar.jpg  # style: folder   method: mtime
/asset-vxxx/foo/bar.jpg         # style: folder   method: ENV['RAILS_ASSET_ID'] = 'xxx'

# configure
VirtualAssetPath.style = :folder # default
VirtualAssetPath.style = :MD5    # default

Install

(only tested on rails 2 atm)

./script/plugin install git://github.com/grosser/virtual_asset_path.git

Or gem install virtual_asset_path

Nginx config

rewrite  ^/asset-v[a-z0-9]+(.*) $1; # style: folder
??? # style: suffix

Apache config

??? # style: folder
??? # style: suffix

Tips

  • [Speed] MD5 takes ~0.1 ms/file (results are cached per instance if cache_asset_timestamps is active)
  • [Speed] mtime takes ~0.005 ms/file (...)

TODO

  • Rails 3.1 support -- things got much more complicated ...

Author

Michael Grosser
michael@grosser.it
Hereby placed under public domain, do what you want, just do not hold me accountable...