5

我一直在阅读使用 gzip 压缩您的资产将提高网站的性能。在 Sinatra 应用程序中似乎有很多方法可以做到这一点,所以我想确认最有效和最容易理解的方法。

我遇到过

use Rack::Deflater

在运行应用程序之前,它应该放在我的 config.ru 文件中,所以在我的情况下

require './david'
use Rack::Deflater
run Sinatra::Application

是吗?就这么简单吗?补充一下,我知道这会压缩我所有的静态资产,包括我的图片,但是这些都是从 CDN 提供的,所以会有什么不同吗?

蚂蚁帮助对此表示赞赏

谢谢

4

1 回答 1

5

就这么简单(不是那么好:)但是如果你想检查一下,然后看看Content-Encoding响应头,它应该说gzip. 在 webkit 浏览器中,它位于“网络”下的开发人员工具中,然后选择资源,例如app.min.css和“标题”选项卡。

以下博客文章中给出了一种对此进行测试的方法:

http://artsy.github.io/blog/2012/02/24/10x-rack-and-rails-output-compression-with-rack-deflater/

我将规范修改为共享示例,因此我可以将它们添加到我真正想要检查的地方:

shared_examples "Compressed pages" do
  subject { last_response.headers }
  its(["Content-Encoding"]) { should be_nil }
  context "After compression" do
    before do
      get page
      @etag = last_response.headers["Etag"]
      @content_length = last_response.headers["Content-Length"]
      get page, {}, { "HTTP_ACCEPT_ENCODING" => "gzip" }
    end
    its(["Etag"]) { should == @etag }
    its(["Content-Length"]) { should_not == @content_length }
    its(["Content-Encoding"]) { should == "gzip"}
  end
end

我的主要规格是这样使用它的:

  describe "Public pages" do

    describe "Home page", :type => :request do
      let(:page) { "/" }
      it_behaves_like "Compressed pages"

it_behaves_like "Compressed pages"运行该共享示例并检查它是否具有正确的标题等。

于 2013-05-29T15:25:56.453 回答