Sinatra应用程序中的gzip资产

Sinatra应用程序中的gzip资产

问题描述:

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

I have been reading that zipping your assets using gzip will increase the performance of a site. There seems to be many ways to do this in a Sinatra application, so i was looking to confirm the most effective and easiest way to understand.

我碰到了

use Rack::Deflater

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

Which should be placed in my config.ru file before running the app, so in my case

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

是吗?这很简单吗,只需添加一下,我就知道这将压缩我的所有静态资产(包括图像),但是这些资源是通过CDN提供的,那么会有所作为吗?

is that it? is it this simple, Just to add I know this will zip all my static assets, including my images, but these are served from a CDN so would that make a difference?

蚂蚁的帮助对此表示赞赏

Ant help appreciated with this one

谢谢

就这么简单(不是很好:),但是如果要检查,请查看Content-Encoding响应标头,它应该说gzip.在Webkit浏览器中,它位于开发人员工具的网络"下,然后选择资源,例如app.min.css和标题"标签.

It is that easy (isn't that nice:) but if you want to check, then look at the Content-Encoding response header and it should say gzip. In a webkit browser it's in the developer tools under "Network", then select the resource, like app.min.css and the "Headers" tab.

以下博客文章中提供了一种测试方法:

A way to test for this is given in the following blog post:

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

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

I modified the specs into shared examples, so I can add them in where I really want to check:

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

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

My main spec uses it like this:

  describe "Public pages" do

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

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

The it_behaves_like "Compressed pages" will run that shared example and check it has the right header etc.