This guide will cover the ideology of the asset pipeline introduced in Rails 3.1.
By referring to this guide you will be able to:
- Understand what the asset pipeline is and what it does
- Properly organize your application assets
- Understand the benefits of the asset pipeline
- Adding a pre-processor to the pipeline
- Package assets with a gem
What Is The Asset Pipeline?
Prior to Rails 3.1 these features were added through third-party Ruby libraries such as Jammit and Sprockets. Rails 3.1 includes the gem, which depends on the gem, by default.
By having this as a core feature of Rails, all developers can benefit from the power of having their assets pre-processed, compressed and minified by one central library, Sprockets. This is part of Rails’ “Fast by default” strategy as outlined by DHH in his 2011 keynote at Railsconf.
In new Rails 3.1 application the asset pipeline is enable by default. It can be disabled in by putting this line inside the class definition:config.assets.enabled = false
It is recommended that you use the defaults for all new apps.
The first is to concatenate of assets. This is important in a production environment to reduce the number of requests that a client browser has to make to render a web page. While Rails already has a feature to concatenate these types of asset—by placing at the end of tags such as and —, many people do not use it.
The default behavior in 3.1 and onward is to concatenate all files into one master file each for JS and CSS, however you can separate files or groups of files if required (see below). In production an MD5 fingerprint is inserted into each filename.
You can choose from a set of built in options or specify your own.
What is fingerprinting and why should I care?
Fingerprinting is a technique where 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 there 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 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:
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 filename can change between nodes in multi-server environments.
The query string in Rails is based on the files mtime (mtime is the file modification time). 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 problems 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 be ensuring filenames are consistent based on the content.
How to Use the Asset Pipeline
In previous versions of Rails, all assets were located in subdirectories of such as , and . With the asset pipeline, the preferred location for these assets is now the directory. Files in this directory will be served by the Sprockets middleware included in the sprockets gem.
This is not to say that assets can (or should) no longer be placed in . They still can be and will be served as static files by the application or web server. You would only use if you wish your files to undergo some pre-processing before they are served.
Assets can be placed inside an application in one of three locations: , or .
is for your own libraries’ code that doesn’t really fit into the scope of the application or those libraries which are shared across applications.
All subdirectories that exists within these three locations will be added to the search path for Sprockets (visible by calling in a console). When an asset is requested, these paths will be looked through to see if they contain an asset matching the name specified. Once an asset has been found, it’s processed by Sprockets and served.
Coding links to Assets
To access assets, we can use the same tags that we are generally familiar with:<%= image_tag “rails.png” %>
Providing that assets are enabled within our application ( in the current environment’s file is not set to ), this file will be served by Sprockets unless a file at exists, in which case that file will be served. Alternatively, a file with an MD5 hash after its name such as will also be picked up by Sprockets. How these hashes are generated is covered in the Production Assets section later on in this guide.
Otherwise, Sprockets will look through the available paths until it finds a file that matches the name and then will serve it, first looking in the application’s assets directories and then falling back to the various engines of the application.
These helpers (when the pipeline is on) are providing links to the compiled manifest with the specified name (or names).
Manifest Files and Directives
For example, in the default Rails application there’s a file which contains the following lines:
//= require jquery
//= require jquery_ujs
//= require_tree .
There’s also a default file which contains these lines:
- require_tree .
In this example is used. This will put the CSS contained within the file (if any) at the top of any other CSS in this file unless is specified after another directive.
You can have as many manifest files as you need. For example the and manifest could contain the JS and CSS files that are used for the admin section of an application.
For some assets (like CSS) the compiled order is important. You can specify individual files and they will be compiled in the order specified:
- require reset
- require layout
- require chrome
TODO: Talk about: Rack::Cache’s caching (used in dev and production. The only difference is hashing and headers).
In the development environment assets are compiled and cached on the first request after the server is started. Sprockets sets a cache-control http header to reduce request overhead on subsequent requests – on these the browser gets a 304 (not-modified) response.
If any of the files in the manifest have changed between requests, the server will respond with a new compiled file.
In the production environment, assets are served slightly differently.
On the first request the assets are compiled and cached as described above, however the manifest names are altered to include an MD5 hash. Files names typically will look like these:
The MD5 is generated from the contents of the compiled files, and is included in the http header.
Sprockets also sets the http header to . This signals all caches between your server and the client browser that this content (the file served) can be cached for 1 year. The effect of this is to reduce the number of requests for this asset from your server; the asset has a good chance of being in the local browser cache or some intermediate cache.
This behavior is controlled by the setting of setting in Rails (which is for production, for everything else). This value is propagated to Sprockets during initialization for use when action_controller is not available.
describe each and the differences between:
- Sass-rails’s handy helpers
- ERB pre-processing and
Even though assets are served by Rack::Cache with far-future headers, in high traffic sites this may not be fast enough.
Rails comes bundled with a rake task to compile the manifests to files on disc. These are located in the directory where they will be served by your web server instead of the Rails application.
TODO: Add section about image assets
The rake task is:
TODO: explain where to use this with Capistrano
TODO: talk about the option and the default matcher for files:
[ /\w+\.(?!js|css).+/, “application.js”, “application.css” ]
Sprockets also creates a gzip (.gz) of your assets. This prevents your server from contently compressing your assets for each request. You must configure your server to use gzip compression and serve the compressed assets that will be stored in the public/assets folder. The following are some configuration blocks that you can use for common servers.
NGINX & Apache examples?
Customizing The Pipeline
There is currently one option for processing CSS – SCSS. This Gem extends the CSS syntax and offers minification.
The following line will enable SCSS in you project.
config.assets.css_compressor = :scss
This option is for compression only and does not relate to the SCSS language extensions that apply when using the file extension on CSS assets.
The default Gemfile includes uglifier. This gem wraps UglifierJS (written for NodeJS) in Ruby. It compress your code by removing white spaces and other magical things like changing your if and else statements to ternary operators when possible.
TODO: Add detail about the other two
config.assets.js_compressor = :uglifier
Using your own compressor
This object must have a method that takes a string as the sole argument and it must return a string.
To enable this pass a Object to the config option in :
config.assets.css_compressor = Transformer.new
Changing the assets path
The public path that Sprockets uses by default is .
This can be changed to something else:
config.assets.prefix = “/some_other_path”
This is a handy option if you have any existing project (pre Rails 3.1) that already uses this path.
Adding Assets to Your Gems
Assets can also come from external sources in the form of gems.
Making Your Library or Gem a Pre-Processor
“You should be able to register [your gems] on Tilt and Sprockets will find them.” – Josh