32
在开发过程中,我的所有javascript资源都附加了body=1
变量。这究竟是在做什么?什么?body = 1在rails 3.1资产管道中做什么?
http://localhost:3000/assets/application.js?body=1
在开发过程中,我的所有javascript资源都附加了body=1
变量。这究竟是在做什么?什么?body = 1在rails 3.1资产管道中做什么?
http://localhost:3000/assets/application.js?body=1
通过链轮源代码拖网,我们发现:
# 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?
时设置?body=1 or true
对于静态资产,Asset.body
被定义为:
def body
# File is read everytime to avoid memory bloat of large binary files
pathname.open('rb') { |f| f.read }
end
而将资产交还给自己一个“机架能够体对象”
# Add enumerator to allow `Asset` instances to be used as Rack
# compatible body objects.
def each
yield to_s
end
当我们看bundled_asset
,该Asset.body
被重新定义为获取资产仅不包括任何依赖的身体。 Asset.to_a
被定义为将资产及其所有依赖项作为传递到Rack的数组进行检索。
通过这种方式,资产没有组合在一起,而是作为单独的对象,因此单个CSS文件仍然是单独的。