2014-08-29 22 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