Asset Fingerprinting with Paperclip

One of the tricks to improving the performance of your website is to optimize
caching. By instructing browsers and proxies to cache assets that don’t change
very often (css, images, javascript), we can reduce page load time as well as
bandwidth costs.

By instructing browsers and proxies to never check for updated versions of
cached assets we can further speed things up. Google recommend the use of
fingerprinting to enable dynamic updating of cached content. This simply
means including a fingerprint of the resource in it’s URL so the URL gets
updated when the resource changes.

Paperclip is a popular file attachment plugin for Ruby on Rails.
It was a perfect candidate for fingerprinting support as it generates resource
URLs from stored data.

This enables us to set far future expire headers so that browsers don’t need to
check for a newer version. If a change does occur, say because a user uploads a
new avatar, the new filename will be rendered in your html and the cached
version will be ignored.

The example above will set Expires headers in S3. If you’re using local storage
you can configure your webserver to do something similar.

We disable the timestamped query string because some proxies refuse to cache
items with query strings.