开发者

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

开发者 https://www.devze.com 2023-04-08 19:48 出处:网络
In development, all my javascript assets are being appended with the body=1 get variable.What is this a开发者_C百科ctually doing?

In development, all my javascript assets are being appended with the body=1 get variable. What is this a开发者_C百科ctually doing?

http://localhost:3000/assets/application.js?body=1


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消