Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

what does ?body=1 do in rails 3.1 asset pipeline?

In development, all my javascript assets are being appended with the body=1 get variable. What is this actually doing?

http://localhost:3000/assets/application.js?body=1 
like image 519
Lee Quarella Avatar asked Sep 28 '11 13:09

Lee Quarella


People also ask

How does Rails asset pipeline work?

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 and pre-processors such as CoffeeScript, Sass, and ERB.

What does Rails assets Precompile do?

The Rails asset pipeline provides an assets:precompile rake task to allow assets to be compiled and cached up front rather than compiled every time the app boots. There are two ways you can use the asset pipeline on Heroku. Compiling assets locally.

What are sprockets Rails?

Sprockets is a Ruby library for compiling and serving web assets. Sprockets allows to organize an application's JavaScript files into smaller more manageable chunks that can be distributed over a number of directories and files.

How do I add a SCSS file to Rails?

Start adding @import directives to application. scss . If you are using twitters bootstrap and a few css sheets of your own, you have to import bootstrap first, because it has a sheet to reset styles. So you add @import "bootstrap/bootstrap.


1 Answers

Trawling through the Sprocket source code we find:

  # Returns a 200 OK response tuple   def ok_response(asset, env)     if body_only?(env)       [ 200, headers(env, asset, Rack::Utils.bytesize(asset.body)), [asset.body] ]     else       [ 200, headers(env, asset, asset.length), asset ]     end   end 

body_only? is set when ?body=1 or true

For a static asset, Asset.body is defined as:

def body   # File is read everytime to avoid memory bloat of large binary files   pathname.open('rb') { |f| f.read } end 

Whereas passing the asset back its self is a "Rack-capable body object"

# Add enumerator to allow `Asset` instances to be used as Rack # compatible body objects. def each   yield to_s end 

When we look at the bundled_asset, the Asset.body is redefined as retrieving the body of the asset only and not including any dependencies. Asset.to_a is defined as retrieving the asset its self as well as all of its dependencies as an array passed on to Rack.

In this way, assets aren't combined together but taken as individual objects, so individual CSS files are still individual.

like image 53
Xorlev Avatar answered Sep 24 '22 15:09

Xorlev