ruby-on-rails – 如何在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

事实证明,我的网络服务器是WEBrick.

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

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

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

搜索了如何在Rails中启用压缩,找不到任何东西.这就是为什么我在这里问.

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

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

编辑

我遵循艾哈迈德的建议,使用Rack :: Deflator

我确认我已经跑了

rake middleware
=> use Rack::Deflator

接着

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

不幸的是,PageSpeed仍然表示需要压缩.我确认,通过进入Developer Tools<网络设置并刷新页面.每个资源的大小和内容几乎相同,意味着文件不被压缩. 我的一个文件有什么问题吗? 感谢您的帮助. 这是我的完整的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

如果有问题,源头可能在那边,对吧?

我需要安装放气宝石吗?

解决方法

启用压缩

将其添加到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

相关文章

validates:conclusion,:presence=>true,:inclusion=>{...
一、redis集群搭建redis3.0以前,提供了Sentinel工具来监控各...
分享一下我老师大神的人工智能教程。零基础!通俗易懂!风趣...
上一篇博文 ruby传参之引用类型 里边定义了一个方法名 mo...
一编程与编程语言 什么是编程语言? 能够被计算机所识别的表...
Ruby类和对象Ruby是一种完美的面向对象编程语言。面向对象编...