繁体   English   中英

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

[英]How to enable gzip compression for static Rack sites on Heroku Cedar?

在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压缩。 如何压缩资产和public/index.html

根据我以前使用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

我能够验证响应是否被gzipped发送:

$ 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 拦截对静态文件的调用,从而跳过下面的堆栈! 这就是为什么它适用于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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM