2014-08-29 19 views
16

我这里张贴了类似的问题如何在Ruby on Rails中启用压缩?

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

事实证明,我的Web服务器是WEBrick。

使用PageSpeed Insights页面只列出了以下3个服务器

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

我搜索过的gzip压缩了的WEBrick服务器文档,但无法找到任何东西。

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

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

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

编辑

我也跟着用机架的艾哈迈德的建议::平减指数

我确定运行

rake middleware 
=> use Rack::Deflator 

然后

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

遗憾了吧PageSpeed仍然表示需要s被压缩。我确认了进入开发者工具< <网络设置并刷新页面。每个资源的大小和内容实际上是相同的,这意味着文件不会被压缩。

我的某个文件有什么问题吗?

谢谢你的帮助。

这是我的完整配置/应用程序。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 

如果有问题,根源很可能在那里,对不对?

我是否需要安装平面缩放器宝石?

回答

29

启用压缩

将它添加到配置/ application.rb中:

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

来源:如果您使用insert_beforehttp://robots.thoughtbot.com/content-compression-with-rack-deflater

+3

再次执行PageSpeed之后,我仍然收到,我需要启用压缩相同的消息。另外,使用PageSpeed和Network,我的尺寸和内容仍然几乎相同。尽管我很欣赏这种回应,并且为此您可以得到我的投票。谢谢你的时间 – Darkmouse 2014-08-29 23:04:04

+10

有一个宝石,你可以尝试:https://github.com/romanbsd/heroku-deflater – Ahmed 2014-08-30 01:37:57

+0

我只是说,拖延heroku deflater宝石解决了我的问题。请忽略我之前的帖子。谢谢你的帮助。 – Darkmouse 2014-08-30 01:47:47

11

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

+0

我添加了一个链接,我找到了关于使用insert_before(而不是“使用”),将其放置在中间件堆栈的顶部附近。 **希望你没事吧** – 2017-08-22 23:51:35