The asset pipeline is enabled by default. This is important in a production environment, because it can reduce the number of requests that a browser makes to render a web page. Web browsers are limited in the number of requests that they can make in parallel, so fewer requests can mean faster loading for your application. But this technique has some limitations. For example, it cannot generate the caches in advance, and it is not able to transparently include assets provided by third-party libraries.
Starting with version 3. As you'll learn later in this guide, you can customize this strategy to group files any way you like. In production, Rails inserts an MD5 fingerprint into each filename so that the file is cached by the web browser.
You can invalidate the cache by altering this fingerprint, which happens automatically whenever you change the file contents.
The third feature of the asset pipeline is that it allows coding assets via a higher-level language, with precompilation down to the actual assets. Fingerprinting is a technique that makes the name of a file dependent on the contents of the file. When the file contents change, the filename is also changed. For content that is static or infrequently changed, this provides an easy way to tell whether two versions of a file are identical, even across different servers or deployment dates.
When a filename is unique and based on its content, HTTP headers can be set to encourage caches everywhere whether at CDNs, at ISPs, in networking equipment, or in web browsers to keep their own copy of the content.
When the content is updated, the fingerprint will change. This will cause the remote clients to request a new copy of the content.
This is generally known as cache busting. The technique that Rails uses for fingerprinting is to insert a hash of the content into the name, usually at the end. For example a CSS file global. Rails' old strategy was to append a date-based query string to every asset linked with a built-in helper. In the source the generated code looked like this: Not all caches will reliably cache content where the filename only differs by query parameters Steve Souders recommends , " Query strings in particular do not work at all with some CDNs for cache invalidation.
The file name can change between nodes in multi-server environments. The default query string in Rails 2. 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. Too much cache invalidation When static assets are deployed with each new release of code, the mtime time of last modification of all these files changes, forcing all remote clients to fetch them again, even when the content of those assets has not changed.
Fingerprinting fixes these problems by avoiding query strings, and by ensuring that filenames are consistent based on their content. Fingerprinting is enabled by default for production and disabled for all other environments. You can enable or disable it in your configuration through the config. Files in this directory are served by the Sprockets middleware included in the sprockets gem. Assets can still be placed in the public hierarchy.
This will result in false positives during development as these files will work just fine since assets will be compiled on the fly. When running in production however, you will see errors since live compilation is turned off by default. See Precompiling Assets for more information on how precompiling works. The default locations are: For example, these files: It is important to note that files you want to reference outside a manifest must be added to the precompile array or they will not be available in the production environment.
The library as a whole can be accessed in the site's application manifest like so: How these hashes are generated is covered in the In Production section later on in this guide. Sprockets will also look through the paths specified in config. Images can also be organized into subdirectories if required, and they can be accessed by specifying the directory's name in the tag: This includes linking to a blank string.
With these directives, Sprockets loads the files specified, processes them if necessary, concatenates them into one single file and then compresses them if Rails.
By serving one file rather than many, the load time of pages can be greatly reduced because the browser makes fewer requests. Compression also reduces the file size enabling the browser to download it faster. The require directive is used to tell Sprockets the files that you wish to require. Here, you are requiring the files jquery. You need not supply the extensions explicitly.
Sprockets assumes you are requiring a. These paths must be specified relative to the manifest file. You should not rely on any particular order among those.
Additional layers of preprocessing can be requested by adding other extensions, where each extension is processed in a right-to-left manner. These should be used in the order the processing should be applied. Keep in mind that the order of these preprocessors is important.
With debug mode turned off the manifest above would generate instead: Sprockets sets a must-revalidate Cache-Control HTTP header to reduce request overhead on subsequent requests — on these the browser gets a Not Modified response.
If any of the files in the manifest have changed between requests, the server responds with a new compiled file. Debug mode can also be enabled in the Rails helper methods: You could potentially also enable compression in development mode as a sanity check, and disable it on-demand as required for debugging.
By default Rails assumes that assets have been precompiled and will be served as static assets by your web server.
During the precompilation phase an MD5 is generated from the contents of the compiled files, and inserted into the filenames as they are written to disc. These fingerprinted names are used by the Rails helpers in place of the manifest name. The fingerprinting behavior is controlled by the setting of config. Under normal circumstances the default option should not be changed. If there are no digests in the filenames, and far-future headers are set, remote clients will never know to refetch the files when their content changes.
Compiled assets are written to the location specified in config. You can call this task on the server during deployment to create compiled versions of your assets directly on the server.
See the next section for information on compiling locally. The rake task is: Heroku requires this to be false. If you set config. It may expose bugs where your assets reference application objects or methods, since those are still in scope in development mode regardless of the value of this flag.
Changing this flag also affects engines. Engines can define assets for precompilation as well. Since the complete environment is not loaded, engines or other gems will not be loaded, which can cause missing assets.