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
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.
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With