我在这里发布了一个类似的问题
使用 Rack-Zippy 在 Heroku 中提供压缩文件
但是我决定放弃那项服务,因为我无法让它正常工作。
我在我的网站上运行了 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服务器文档以了解如何启用压缩:
我使用这个网站来确定我的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
我搜索了有关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宝石吗?
heroku-deflater
的最后更新日期为2013年12月16日,但它仍然可以解决问题。 - Trantor Liuconfig/application.rb
中找到已经存在的模块和类。你只需要添加一个配置行。 - Ahmed