Ruby On Rails : Assets Precompile : How rails handles asset precompilation? How asset helpers find precompiled file names

If you have other manifests or individual stylesheets and JavaScript files to include, you can add them to the precompile array in config/initializers/assets.rb: : or simply inside application.rb

Rails.application.config.assets.precompile += ['admin.js', 'admin.css', 'swfObject.js']

Always specify an expected compiled filename that ends with .js or .css, even if you want to add Sass or CoffeeScript files to the precompile array.

The rake task also generates a manifest-md5hash.json that contains a list with all your assets and their respective fingerprints. This is used by the Rails helper methods to avoid handing the mapping requests back to Sprockets. A typical manifest file looks like:

{
   "files": {
      "application-723d1be6cc741a3aabb1cec24276d681.js": {
         "logical_path": "application.js",
         "mtime": "2013-07-26T22:55:03-07:00",
         "size": 302506,
         "digest": "723d1be6cc741a3aabb1cec24276d681"
      },
      "application-12b3c7dd74d2e9df37e7cbb1efa76a6d.css": {
         "logical_path": "application.css",
         "mtime": "2013-07-26T22:54:54-07:00",
         "size": 1560,
         "digest": "12b3c7dd74d2e9df37e7cbb1efa76a6d"
      },
      "application-1c5752789588ac18d7e1a50b1f0fd4c2.css": {
         "logical_path": "application.css",
         "mtime": "2013-07-26T22:56:17-07:00",
         "size": 1591,
         "digest": "1c5752789588ac18d7e1a50b1f0fd4c2"
      },
      "favicon-a9c641bf2b81f0476e876f7c5e375969.ico": {
         "logical_path": "favicon.ico",
         "mtime": "2013-07-26T23:00:10-07:00",
         "size": 1406,
         "digest": "a9c641bf2b81f0476e876f7c5e375969"
      },
      "my_image-231a680f23887d9dd70710ea5efd3c62.png": {
         "logical_path": "my_image.png",
         "mtime": "2013-07-26T23:00:27-07:00",
         "size": 6646,
         "digest": "231a680f23887d9dd70710ea5efd3c62"
      }
   },
   "assets": {
      "application.js": "application-723d1be6cc741a3aabb1cec24276d681.js",
      "application.css": "application-1c5752789588ac18d7e1a50b1f0fd4c2.css",
      "favicon.ico": "favicona9c641bf2b81f0476e876f7c5e375969.ico",
      "my_image.png": "my_image-231a680f23887d9dd70710ea5efd3c62.png"
   }
}

The default location for the manifest is the root of the location specified in config.assets.prefix(‘/assets’ by default).

If there are missing precompiled files in production you will get an Sprockets::Helpers::RailsHelper::AssetPaths::AssetNotPrecompiledError exception indicating the name of the missing file(s).

Source: http://guides.rubyonrails.org/v4.1.0/asset_pipeline.html

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s