The new Fingerprinting technique in Rails 3.1

In the new rails 3.1, I heard about the new fingerprinting technique used by them.Looking at the concept I mist say that its hats off the the core team for developing such a great concept.

Here is a quick but thorough overview :

Fingerprinting is a technique whereby the filenames of content that is static or infrequently updated is altered to be unique to the content contained in the file.

When a filename is unique and based on its content, HTTP headers can be set to encourage caches everywhere (at ISPs, in browsers) to keep their own copy of the content. When the content is updated, the fingerprint will change and the remote clients will request the new file. This is generally known as cachebusting.

The most effective technique is to insert a hash of the content into the name, usually at the end. For example a CSS file global.css is hashed and the filename is updated to incorporate the hash.

global.css => global-908e25f4bf641868d8683022a5b62f54.css

This is the strategy adopted by the Rails asset pipeline.

Rails’ old strategy was to append a query string to every asset linked with a built-in helper. In the source the generated code looked like this:

/stylesheets/global.css?1309495796

 

This has several disadvantages:

  1. Not all caches will cache content with a query string
    Steve Souders recommends, “…avoiding a querystring for cacheable resources”. He found that in these case 5-20% of requests will not be cached.
  2. The file name can change between nodes in multi-server environments.
    The query string in Rails is based on the modification time of the files. When assets are deployed to a cluster, there is no guarantee that the timestamps will be the same, resulting in different values being used depending on which server handles the request.

The other problem is that when static assets are deployed with each new release of code, the mtime of all these files changes, forcing all remote clients to fetch them again, even when the content of those assets has not changed.

Fingerprinting avoids all these problems by ensuring filenames are consistent based on their content.

Fingerprinting is enabled by default for production and disabled for all the others environments. You can enable or disable it in your configuration through the config.assets.digest option.