Skip to content
This repository
Browse code

Complete another review of asset pipeline guide

  • Loading branch information...
commit 2f69469479e4fe51748e714fadcfdf30d111d5e5 1 parent 27b1bf3
Ryan Bigg authored July 18, 2011
67  railties/guides/source/asset_pipeline.textile
Source Rendered
@@ -30,13 +30,11 @@ It is recommended that you use the defaults for all new apps.
30 30
 
31 31
 h4. Main Features
32 32
 
33  
-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 +:cache => true+ at the end of tags such as +javascript_include_tag+ and +stylesheet_link_tag+--, many people do not use it.
  33
+The first feature of the pipeline is to concatenate assets. This is important in a production environment, as it reduces the number of requests that a browser needs to make to render a web page. While Rails already has a feature to concatenate these types of asset--by placing +:cache => true+ at the end of tags such as +javascript_include_tag+ and +stylesheet_link_tag+--, many people do not use it.
34 34
 
35  
-The default behavior in Rails 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.
  35
+The default behavior in Rails 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 so that the file is cached by the web browser but can be invalidated if the fingerprint is altered.
36 36
 
37  
-The second feature of the pipeline is to minify or compress. For CSS this usually involves removing whitespace and comments. For Javascript more complex processes can be applied.
38  
-
39  
-You can choose from a set of built in options or specify your own.
  37
+The second feature is to minify or compress. For CSS, this usually involves removing whitespace and comments. For JavaScript, more complex processes can be applied. You can choose from a set of built in options or specify your own.
40 38
 
41 39
 The third feature is the ability to code these assets using another language, or language extension. These include SCSS or Sass for CSS, CoffeeScript for Javascript, and ERB for both.
42 40
 
@@ -62,13 +60,18 @@ Rails old strategy was to append a query string to every asset linked with a bui
62 60
 
63 61
 This has several disadvantages:
64 62
 
65  
-1. Not all caches will cache content with a query string
66  
-
67  
-"Steve Souders recommends":http://www.stevesouders.com/blog/2008/08/23/revving-filenames-dont-use-querystring/, "...avoiding a querystring for cacheable resources". He found that in these case 5-20% of requests will not be cached.
68  
-
69  
-2. The filename can change between nodes in multi-server environments.
  63
+<ol>
  64
+  <li>
  65
+    <strong>Not all caches will cache content with a query string</strong><br>
70 66
 
71  
-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.
  67
+    "Steve Souders recommends":http://www.stevesouders.com/blog/2008/08/23/revving-filenames-dont-use-querystring/, "...avoiding a querystring for cacheable resources". He found that in these case 5-20% of requests will not be cached.
  68
+  </li>
  69
+  
  70
+  <li>
  71
+    <strong>The filename can change between nodes in multi-server environments.</strong><br>
  72
+    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.
  73
+  </li>
  74
+</ol>
72 75
 
73 76
 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.
74 77
 
@@ -84,7 +87,7 @@ h3. How to Use the Asset Pipeline
84 87
 
85 88
 In previous versions of Rails, all assets were located in subdirectories of +public+ such as +images+, +javascripts+ and +stylesheets+. With the asset pipeline, the preferred location for these assets is now the +app/assets+ directory. Files in this directory will be served by the Sprockets middleware included in the sprockets gem.
86 89
 
87  
-This is not to say that assets can (or should) no longer be placed in +public+. They still can be and will be served as static files by the application or web server. You would only use +app/assets+ if you wish your files to undergo some pre-processing before they are served.
  90
+This is not to say that assets can (or should) no longer be placed in +public+; they still can be and will be served as static files by the application or web server. You would only use +app/assets+ if you wish your files to undergo some pre-processing before they are served.
88 91
 
89 92
 When a scaffold or controller is generated for the application, Rails will also generate a JavaScript file (or CoffeeScript if the +coffee-script+ gem is in the +Gemfile+) and a Cascading Style Sheet file (or SCSS if +sass-rails+ is in the +Gemfile+) file for that controller.
90 93
 
@@ -119,7 +122,11 @@ In regular views you can access images in the +assets/images+ directory like thi
119 122
   <%= image_tag "rails.png" %>
120 123
 </erb>
121 124
 
122  
-Images can be organized into directories if required, and this Just Works.
  125
+Images can be organized into directories if required, and they can be accessed by specifying the directory's name in the tag:
  126
+
  127
+<erb>
  128
+  <%= image_tag "icons/rails.png" %>
  129
+</erb>
123 130
 
124 131
 Providing that assets are enabled within our application (+config.assets.enabled+ in the current environment's file is not set to +false+), this file will be served by Sprockets unless a file at +public/assets/rails.png+ exists, in which case that file will be served.
125 132
 
@@ -138,21 +145,15 @@ This will insert a correctly formatted data uri into the CSS source.
138 145
 
139 146
 h5. CSS and ERB
140 147
 
141  
-If you add an erb extension to a css asset:
142  
-
143  
-<plain>
144  
-application.css.erb
145  
-</plain>
146  
-
147  
-then you can use the asset_path helper in your CSS rules:
  148
+If you add an +erb+ extension to a CSS asset, making it something such as +application.css.erb+ then you can use the +asset_path+ helper in your CSS rules:
148 149
 
149 150
 <css>
150 151
 .class{background-image:<%= asset_path 'image.png' %>}
151 152
 </css>
152 153
 
153  
-This will write the path to any specified images in +/app/assets/images+ and its subdirectories.
  154
+This will write the path to the particular asset being referenced. In this example, it would make sense to have an image in one of the asset load paths, such as +app/assets/images/image.png+, which would be referenced here. If this image is already available in +public/assets+ as a fingerprinted file then that path will be referenced.
154 155
 
155  
-Note that the closing tag cannot be of the style '-%>'.
  156
+Note that the closing tag cannot be of the style +-%>+.
156 157
 
157 158
 h5. CSS and SCSS
158 159
 
@@ -160,7 +161,7 @@ TODO: Sass-rails's handy +image_url+ helpers
160 161
 
161 162
 h4. Manifest Files and Directives
162 163
 
163  
-Sprockets uses manifest files to determine which assets to include and serve. These manifest files contain _directives_ -  instructions that tell Sprockets which files to require in order to build a single CSS or JavaScript file. With these directives, Sprockets will load the files specified, process them if necessary, concatenate them into one single file and then compress them (if +Rails.application.config.assets.compress+ is set to +true+). By serving one file rather than many, a page's load time is greatly reduced.
  164
+Sprockets uses manifest files to determine which assets to include and serve. These manifest files contain _directives_ - instructions that tell Sprockets which files to require in order to build a single CSS or JavaScript file. With these directives, Sprockets will load the files specified, process them if necessary, concatenate them into one single file and then compress them (if +Rails.application.config.assets.compress+ is set to +true+). By serving one file rather than many, a page's load time is greatly reduced as there is not as many requests to make for each file.
164 165
 
165 166
 For example, in the default Rails application there's a +app/assets/javascripts/application.js+ file which contains the following lines:
166 167
 
@@ -206,7 +207,7 @@ The file extensions used on an asset will determine what preprocssing will be ap
206 207
 
207 208
 When these files are requested, they will be processed by the processors provided by the +coffee-script+ and +sass-rails+ gems and then sent back to the browser as JavaScript and CSS respectively.
208 209
 
209  
-Additional layers of pre-processing can be requested by adding other extensions. These should be used in the order the processing should be applied. For example, a stylesheet called +app/assets/stylesheets/projects.css.scss.erb+ would first be processed as ERB, then SCSS and finally served as CSS. The same applies to a JavaScript file - +app/assets/javascripts/projects.js.coffee.erb+ would be process as ERB, CoffeeScript and served as JavaScript.
  210
+Additional layers of pre-processing can be requested by adding other extensions, where each extension will be processed in a right-to-left manner. These should be used in the order the processing should be applied. For example, a stylesheet called +app/assets/stylesheets/projects.css.scss.erb+ would first be processed as ERB, then SCSS and finally served as CSS. The same applies to a JavaScript file - +app/assets/javascripts/projects.js.coffee.erb+ would be process as ERB, CoffeeScript and served as JavaScript.
210 211
 
211 212
 Keep in mind that the order of these pre-processors is important. For example, if we called our JavaScript file +app/assets/javascripts/projects.js.erb.coffee+ then it would be processed with the CoffeeScript interpreter first, which wouldn't understand ERB and therefore we would run into problems.
212 213
 
@@ -266,9 +267,9 @@ Rails comes bundled with a rake task to compile the manifests to files on disc.
266 267
 
267 268
 The rake task is:
268 269
 
269  
-<erb>
  270
+<plain>
270 271
 rake assets:precompile
271  
-</erb>
  272
+</plain>
272 273
 
273 274
 You can run this as part of a Capistrano deployment:
274 275
 
@@ -284,9 +285,9 @@ TODO: Extend above task to allow for this and add task to set it up (See commits
284 285
 
285 286
 The default matcher for compiling files will include +application.js+, +application.css+ and all files that do not end in +js+ or +css+:
286 287
 
287  
-<erb>
  288
+<ruby>
288 289
 [ /\w+\.(?!js|css).+/, /application.(css|js)$/ ]
289  
-</erb>
  290
+</ruby>
290 291
 
291 292
 If you have other manifests or individual stylesheet and javascript files to include, you can append them to the +precompile+ array:
292 293
 
@@ -316,8 +317,6 @@ TODO: NGINX instructions
316 317
 
317 318
 When files are precompiled Sprockets also creates "Gzip":http://en.wikipedia.org/wiki/Gzip (.gz) version of your assets. This avoids the server having to do this for any requests; it can simply read the compressed files from disc. 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 configuration options can be used:
318 319
 
319  
-TODO: NGINX instructions
320  
-
321 320
 TODO: Apache instructions
322 321
 
323 322
 
@@ -340,7 +339,7 @@ h4. Javascript
340 339
 
341 340
 Possible options for Javascript compression are +:closure+, +:uglifier+ and +:yui+. These require the use of the +closure-compiler+, +uglifier+ or +yui-compressor+ gems respectively.
342 341
 
343  
-The default Gemfile includes "uglifier":https://github.com/lautis/uglifier. This gem wraps "UglifierJS":https://github.com/mishoo/UglifyJS (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.
  342
+The default Gemfile includes "uglifier":https://github.com/lautis/uglifier. This gem wraps "UglifierJS":https://github.com/mishoo/UglifyJS (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 where possible.
344 343
 
345 344
 The following line will invoke uglifier for Javascript compression.
346 345
 
@@ -352,9 +351,7 @@ The +config.assets.compress+ must be set to +true+ to enable Javascript compress
352 351
 
353 352
 h4. Using your own compressor
354 353
 
355  
-The compressor config settings for CSS and Javascript will also take an Object.
356  
-
357  
-This object must have a +compress+ method that takes a string as the sole argument and it must return a string.
  354
+The compressor config settings for CSS and Javascript will also take any Object. This object must have a +compress+ method that takes a string as the sole argument and it must return a string.
358 355
 
359 356
 <erb>
360 357
 class Transformer
@@ -381,7 +378,7 @@ This can be changed to something else:
381 378
 config.assets.prefix = "/some_other_path"
382 379
 </erb>
383 380
 
384  
-This is a handy option if you have any existing project (pre Rails 3.1) that already uses this path.
  381
+This is a handy option if you have any existing project (pre Rails 3.1) that already uses this path or you wish to use this path for a new resource.
385 382
 
386 383
 h4. X-Sendfile headers
387 384
 

0 notes on commit 2f69469

Please sign in to comment.
Something went wrong with that request. Please try again.