Update: My Asset Fingerprinting patch was included in paperclip-2.3.4 in Oct 2010. Thanks jyurek!
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.
Enabling it is as simple as:
- Install paperclip-2.3.4
- Add an :attachment_fingerprint column to your database
- Include :fingerprint in the path for your attachment (see below)
1 2 3 4 5 6 7
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.
Fingerprinting support was added to paperclip-2.3.4 on 6 Oct 2010.
For more info on optimizing for caching: