简体   繁体   English

如何在 Ruby on Rails 中启用压缩?

[英]How to enable compression in Ruby on Rails?

I posted a similar question here我在这里发布了一个类似的问题

Serving Compressed Assets in Heroku with Rack-Zippy 使用 Rack-Zippy 在 Heroku 中提供压缩资产

but decided to give up on that service, since I couldn't get it to work.但决定放弃该服务,因为我无法让它工作。

I ran PageSpeed Insights on my website to determine the speed of my website.我在我的网站上运行 PageSpeed Insights 来确定我网站的速度。

The most important suggestion I received was to Enable Compression.我收到的最重要的建议是启用压缩。

Compressing resources with gzip or deflate can reduce the number of bytes sent over the network.
Enable compression for the following resources to reduce their transfer size by 191.2KiB 
(74% reduction).

I've followed the instructions on this website我已按照本网站上的说明进行操作

https://developers.google.com/speed/docs/insights/EnableCompression https://developers.google.com/speed/docs/insights/EnableCompression

and it says to consult the documentation for your web server on how to enable compression:它说要查阅有关如何启用压缩的 Web 服务器的文档:

I've used this website to find out my web server我已经使用这个网站来找出我的网络服务器

http://browserspy.dk/webserver.php http://browserspy.dk/webserver.php

It turns out that my web server is WEBrick.原来我的网络服务器是WEBrick。

The PageSpeed Insights Page only lists the following 3 servers PageSpeed Insights 页面仅列出以下 3 个服务器

Apache: Use mod_deflate
Nginx: Use ngx_http_gzip_module
IIS: Configure HTTP Compression

I've searched for documentation on gzip compression for WEBrick servers but couldn't find anything.我搜索了有关 WEBrick 服务器的 gzip 压缩的文档,但找不到任何内容。

I've searched for how to enable compression in Rails and couldn't find anything.我搜索了如何在 Rails 中启用压缩,但找不到任何东西。 That's why I'm asking here.这就是我在这里问的原因。

I've tried using Rack Zippy but gave up on it.我试过使用 Rack Zippy 但放弃了。

Right now, I don't even know where to begin.现在,我什至不知道从哪里开始。 My first step, is finding out what I should do.我的第一步,是找出我应该做什么。

Edit编辑

I followed Ahmed's suggestion of using Rack::Deflator我遵循了 Ahmed 的使用 Rack::Deflator 的建议

I confirmed that I had it by running我通过运行确认我拥有它

rake middleware
=> use Rack::Deflator

and then进而

git add .
git commit -m '-'
git push heroku master

Unfortunately PageSpeed still says it needs to be compress.不幸的是,PageSpeed 仍然说它需要压缩。 I confirmed that by going into Developer Tools << Network Settings and refreshing the page.我通过进入开发人员工具<<网络设置并刷新页面来确认。 Size and content were virtually identical for every resource meaning the files are not compressed.每个资源的大小和内容几乎相同,这意味着文件没有被压缩。

Is there something wrong with one of my files?我的一个文件有问题吗?

Thank you for your help.感谢您的帮助。

Here is my full config/application.rb file这是我的完整 config/application.rb 文件

require File.expand_path('../boot', __FILE__)

require 'rails/all'

Bundler.require(*Rails.groups)

module AppName
  class Application < Rails::Application

    config.middleware.use Rack::Deflater
    config.assets.precompile += %w(*.png *.jpg *.jpeg *.gif)
    config.exceptions_app = self.routes

    config.cache_store = :memory_store

  end
end

If there is a problem, the source is likely over there, right?如果有问题,应该是源头在那边吧?

Do I need to install the deflator gem?我需要安装 deflator gem 吗?

Enable compression启用压缩

Add it to config/application.rb:将其添加到 config/application.rb:

module YourApp
  class Application < Rails::Application
    config.middleware.use Rack::Deflater
  end
end

Source: http://robots.thoughtbot.com/content-compression-with-rack-deflater来源: http : //robots.thoughtbot.com/content-compression-with-rack-deflater

Rack::Deflater should work if you use insert_before (instead of "use"), to place it near the top of the middleware stack, prior to any other middleware that might send a response.如果您使用insert_before (而不是“use”), Rack::Deflater应该可以工作,将其放置在中间件堆栈的顶部附近,在任何其他可能发送响应的中间件之前。 .use places it at the bottom of the stack. .use将它放在堆栈的底部。 On my machine the topmost middleware is Rack::Sendfile .在我的机器上,最顶层的中间件是Rack::Sendfile So I would use:所以我会使用:

config.middleware.insert_before(Rack::Sendfile, Rack::Deflater)

You can get the list of middleware in order of loading by doing rake middleware from the command line.您可以通过从命令行执行rake middleware来按加载顺序获取中间件列表。

Note: A good link for insert_before vs Use in middleware rack注意: insert_before 与 Use in middleware rack 的一个很好的链接

As per the author of Rack::Deflater it should be placed after ActionDispatch::Static in a Rails app.根据Rack::Deflater作者,它应该放在 Rails 应用程序中的ActionDispatch::Static之后。 The reasoning is that if your app is also serving static assets (like on Heroku, for example), when assets are served from disk they are already compressed.理由是,如果您的应用程序还提供静态资产(例如在 Heroku 上),那么当资产从磁盘提供时,它们已经被压缩。 Inserting it before would only end up in Rack::Deflater attempting to re-compress those assets.之前插入它只会导致Rack::Deflater尝试重新压缩这些资产。 Therefore as a performance optimisation:因此作为性能优化:

# application.rb

config.middleware.insert_after ActionDispatch::Static, Rack::Deflater

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

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