Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I determine the MD5 digest of a given asset in the Rails asset pipeline?

I'm writing a Javascript-rich application in a Ruby on Rails 3.1 project and using Handlebars for my JS templating framework. I'm trying to figure out a way to dynamically append the MD5 digest of an asset (generated during asset precompilation on production) to my tags inside of my Handlebars template. I'm hoping that there's a hash with the asset path as the key and the MD5 digest as the value, but I haven't been able to find one.

An ideal solution would be passing the hash from Ruby into Javascript and defining a Handlebars helper that would automatically append the MD5 digest to the "src" attribute of the asset.

Has anybody attempted to do something similar? There must be a way to use Javascript templates in Rails and also reap the benefits of asset fingerprinting.

like image 737
Jordan Brown Avatar asked Feb 06 '13 00:02

Jordan Brown


People also ask

How do you Precompile an asset?

To compile your assets locally, run the assets:precompile task locally on your app. Make sure to use the production environment so that the production version of your assets are generated. A public/assets directory will be created. Inside this directory you'll find a manifest.

How does rails asset pipeline work?

The asset pipeline provides a framework to concatenate and minify or compress JavaScript and CSS assets. It also adds the ability to write these assets in other languages such as CoffeeScript, Sass and ERB. Prior to Rails 3.1 these features were added through third-party Ruby libraries such as Jammit and Sprockets.

What does rake assets Precompile do?

rake assets:precompile. We use rake assets:precompile to precompile our assets before pushing code to production. This command precompiles assets and places them under the public/assets directory in our Rails application.


2 Answers

As someone mentioned in the comments, appending a hash to the asset paths is a default part of the asset pipeline.

In production, Rails inserts an MD5 fingerprint into each filename so that the file is cached by the web browser

You can read more about fingerprinting in the asset pipeline here. Rails uses Sprockets to compile assets. The fingerprinting comes as part of Sprockets process.

You can use Sprockets' find_asset method, passing in a logical path to your asset to get a Sprockets::BundledAsset instance. For example

[1] pry(main)> Rails.application.assets.find_asset('application.js')
=> #<Sprockets::BundledAsset:0x3fe368ab8070 pathname="/Users/deefour/Sites/MyApp/app/assets/javascripts/application.js", mtime=2013-02-03 15:33:57 -0500, digest="ab07585c8c7b5329878b1c51ed68831e">

You can call digest_path on this object to get it's MD5 sum appended to the asset.

[1] pry(main)> Rails.application.assets.find_asset('application.js').digest_path
=> "application-ab07585c8c7b5329878b1c51ed68831e.js"

With this knowledge you can easily create a helper to return the digest_path for any asset in your application, and call this helper from within your .js.erb files.

like image 198
deefour Avatar answered Sep 30 '22 20:09

deefour


This is an old question, but it seems that nowadays you can use assets_manifest:

Rails.application.assets_manifest.assets["application.css"] => "application-46ae33e78e504ff295219f41d63c79719d062e48dc0c07bd9b6f7bcad72c6636.css"

More discussion here: https://github.com/rails/sprockets-rails/issues/311

like image 39
NoR Avatar answered Sep 30 '22 22:09

NoR