gpt4 book ai didi

heroku - 如何为 Heroku Cedar 上的静态 Rack 站点启用 gzip 压缩?

转载 作者:行者123 更新时间:2023-12-04 22:48:17 24 4
gpt4 key购买 nike

关注 Creating Static Sites in Ruby with Rack文章,我在 Heroku 上得到一个静态站点,带有 config.ru看起来像这样:

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)
]
}

当我对结果运行 YSlow 时,它报告没有任何文件被 gzip 压缩。我该怎么做才能压缩 Assets 和 public/index.html ?

最佳答案

来自我的 previous experience带链轮、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

我能够验证响应是通过 gzip 发送的:

$ curl -H 'Accept-Encoding: gzip' http://localhost:9292 | file -
/dev/stdin: gzip compressed data

但不适用于 /css 下的静态 Assets , /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但不是为了 Assets 。

以下 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

关于heroku - 如何为 Heroku Cedar 上的静态 Rack 站点启用 gzip 压缩?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15190295/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com