【问题标题】:How to enable compression in Ruby on Rails?如何在 Ruby on Rails 中启用压缩?
【发布时间】:2014-08-29 21:20:40
【问题描述】:

我在这里发布了一个类似的问题

Serving Compressed Assets in Heroku with Rack-Zippy

但决定放弃这项服务,因为我无法让它发挥作用。

我在我的网站上运行 PageSpeed Insights 以确定我的网站的速度。

我收到的最重要的建议是启用压缩。

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).

我已按照本网站上的说明进行操作

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

它说要查阅您的 Web 服务器的文档以了解如何启用压缩:

我已使用此网站查找我的网络服务器

http://browserspy.dk/webserver.php

原来我的网络服务器是WEBrick。

PageSpeed Insights Page 仅列出以下 3 台服务器

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

我搜索了有关 WEBrick 服务器的 gzip 压缩的文档,但找不到任何东西。

我搜索了如何在 Rails 中启用压缩,但找不到任何东西。这就是我在这里问的原因。

我尝试过使用 Rack Zippy 但放弃了。

现在,我什至不知道从哪里开始。我的第一步是找出我应该做什么。

编辑

我按照 Ahmed 的建议使用 Rack::Deflator

我通过运行确认我拥有它

rake middleware
=> use Rack::Deflator

然后

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

不幸的是,PageSpeed 仍然说它需要压缩。我通过进入开发者工具

我的某个文件有问题吗?

感谢您的帮助。

这是我的完整 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

如果有问题,源头应该在那边吧?

我需要安装 deflator gem 吗?

【问题讨论】:

    标签: ruby-on-rails ruby performance gzip pagespeed


    【解决方案1】:

    启用压缩

    将其添加到 config/application.rb:

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

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

    【讨论】:

    • 再次运行 PageSpeed 后,我仍然收到同样的消息,我需要启用压缩。此外,使用 PageSpeed 和 Network,我的大小和内容仍然几乎相同。不过,我很感谢您的回复,为此您得到了我的投票。感谢您的宝贵时间
    • 我正要说停止 heroku-deflater gem 解决了我的问题。请无视我之前的帖子。感谢您的帮助。
    • heroku-deflater 确实可以解决问题,尽管它的最后一次更新是在 2013 年 12 月 16 日。
    • 哇,只需安装此 gem 文档大小现在是其大小的 1/3。不错-谢谢
    • @KickButtowski 你应该在`config/application.rb`中找到模块和类。您只需要添加一个配置行。
    【解决方案2】:

    如果您使用insert_before(而不是“使用”),Rack::Deflater 应该可以工作,将它放在中间件堆栈的顶部附近,在任何其他可能发送响应的中间件之前。 .use 将它放在堆栈的底部。在我的机器上,最顶层的中间件是Rack::Sendfile。所以我会使用:

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

    您可以通过在命令行中执行rake middleware 来获取按加载顺序排列的中间件列表。

    注意:A good link for insert_before vs Use in middleware rack

    【讨论】:

    • 我添加了一个我发现的关于使用 insert_before(而不是“使用”)的链接,将其放置在中间件堆栈的顶部附近。 希望你没事
    【解决方案3】:

    根据Rack::Deflaterauthor,它应该放在Rails 应用程序中的ActionDispatch::Static 之后。原因是,如果您的应用程序还提供静态资产(例如在 Heroku 上),那么当资产从磁盘提供时,它们已经被压缩了。之前插入它只会导致Rack::Deflater 尝试重新压缩这些资产。因此作为性能优化:

    # application.rb
    
    config.middleware.insert_after ActionDispatch::Static, Rack::Deflater
    
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-08-31
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多