从我的 previous experience 与 Sprockets、Sinatra 和 Rack::Deflater 来看,我很确定我只是另一个 use Rack::Deflater 行,远离我想要的。
我把config.ru改成了这样:
use Rack::Static,
:urls => ["/images", "/js", "/css"],
:root => "public"
use Rack::Deflater
run lambda # ...same as in the question
并且我能够验证响应是压缩发送的:
$ curl -H 'Accept-Encoding: gzip' http://localhost:9292 | file -
/dev/stdin: gzip compressed data
但不适用于/css、/js 或/images 下的静态资产:
$ curl -H 'Accept-Encoding: gzip' http://localhost:9292/css/bootstrap.min.css | file -
/dev/stdin: ASCII English text, with very long lines
那时我意识到这是一个标准的中间件堆栈——Rack::Static intercepts 对静态文件的调用,因此跳过了下面的堆栈!这就是为什么它适用于 public/index.html 但不适用于资产。
以下config.ru 有效(注意use Rack::Deflater 现在在use Rack::Static 之前):
use Rack::Deflater
use Rack::Static,
:urls => ["/images", "/js", "/css"],
:root => "public"
run lambda { |env|
[
200,
{
'Content-Type' => 'text/html',
'Cache-Control' => 'public, max-age=86400'
},
File.open('public/index.html', File::RDONLY)
]
}
验证人:
$ curl -H 'Accept-Encoding: gzip' http://localhost:9292/css/bootstrap.min.css | file -
/dev/stdin: gzip compressed data, from Unix