Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails asset pipeline and digest values

Does anyone know how exactly the asset digest value is calculated? If I have two JS files which contain various other included JS scripts then will each file maintain the same digest hash if none of the inner scripts have been changed? Or is a new digest value calculated each time the assets:precompile operation is run?

like image 688
matsko Avatar asked Apr 05 '12 02:04

matsko


People also ask

How asset pipeline works in Rails?

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.

How do you Precompile rails assets?

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.

What are assets in rails?

Generally asset is anything that browser loads after it gets the HTML page. Meaning javascript, css and any images. But as you pointed out there are two different image types in a rails project.


1 Answers

The accepted answer is not quite true. We build static assets for our staging, demo and production servers, and the same asset is given different digest values in each case.

It turns out that the Rails environment is also being taken into consideration. Sprockets creates the digest as follows:

# Sprockets::Environment::initialize
@digest_class = ::Digest::MD5

# Sprockets::Base::digest
@digest ||= digest_class.new.update(VERSION).update(version.to_s)

# Sprockets::Base::file_digest(path)
digest.file(path.to_s)

# Sprockets::Asset::initialize
@digest = environment.file_digest(pathname).hexdigest

Rails hooks into Sprockets as follows:

# Sprockets::Railtie
app.assets = Sprockets::Environment.new(app.root.to_s) do |env|
  env.version = ::Rails.env + "-#{config.assets.version}"
  ...
end

So rails is creating a Sprockets environment that has a version equal to the Rails environment, and Sprockets uses the version when creating the digest class.

like image 131
kranzky Avatar answered Sep 28 '22 01:09

kranzky