Ric*_*wis 5 ruby performance gzip assets sinatra
我一直在阅读使用gzip压缩您的资产将提高网站的性能.在Sinatra应用程序中似乎有很多方法可以做到这一点,因此我希望确认最有效和最简单的方法来理解.
我遇到过
use Rack::Deflater
Run Code Online (Sandbox Code Playgroud)
在运行应用程序之前应将其放在我的config.ru文件中,所以在我的情况下
require './david'
use Rack::Deflater
run Sinatra::Application
Run Code Online (Sandbox Code Playgroud)
是吗?是这么简单,只是添加我知道这将拉链我的所有静态资产,包括我的图像,但这些是从CDN提供,所以会有所作为?
Ant帮助赞赏这一个
谢谢
这很容易(不是很好:)但如果你想检查,那么看看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
Run Code Online (Sandbox Code Playgroud)
我的主要规范使用它像这样:
describe "Public pages" do
describe "Home page", :type => :request do
let(:page) { "/" }
it_behaves_like "Compressed pages"
Run Code Online (Sandbox Code Playgroud)
在it_behaves_like "Compressed pages"该共享例子和将运行检查它有权标题等