如何在Ruby on Rails中启用压缩? [英] How to enable compression in Ruby on Rails?

查看:107
本文介绍了如何在Ruby on Rails中启用压缩?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I posted a similar question here

使用Rack-Zippy在Heroku中提供压缩资产

但是我决定放弃该服务,因为我无法使它正常工作.

but decided to give up on that service, since I couldn't get it to work.

我在网站上运行PageSpeed Insights来确定网站的速度.

I ran PageSpeed Insights on my website to determine the speed of my website.

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

The most important suggestion I received was to Enable Compression.

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

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

I've followed the instructions on this website

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

并说请参阅您的Web服务器的文档以了解如何启用压缩:

and it says to consult the documentation for your web server on how to enable compression:

我已经使用此网站来查找我的Web服务器

I've used this website to find out my web server

http://browserspy.dk/webserver.php

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

It turns out that my web server is WEBrick.

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

The PageSpeed Insights Page only lists the following 3 servers

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

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

I've searched for documentation on gzip compression for WEBrick servers but couldn't find anything.

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

I've searched for how to enable compression in Rails and couldn't find anything. That's why I'm asking here.

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

I've tried using Rack Zippy but gave up on it.

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

Right now, I don't even know where to begin. My first step, is finding out what I should do.

修改

我听从了艾哈迈德(Ahmed)关于使用Rack :: Deflator的建议

I followed Ahmed's suggestion of using Rack::Deflator

我通过跑步确认拥有它

rake middleware
=> use Rack::Deflator

然后

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

不幸的是,PageSpeed仍然说它需要压缩.我通过进入开发人员工具<<网络设置并刷新页面.每个资源的大小和内容实际上是相同的,这意味着文件不会被压缩.

Unfortunately PageSpeed still says it needs to be compress. I confirmed that by going into Developer Tools << Network Settings and refreshing the page. Size and content were virtually identical for every resource meaning the files are not compressed.

我的其中一个文件出问题了吗?

Is there something wrong with one of my files?

谢谢您的帮助.

这是我完整的config/application.rb文件

Here is my full config/application.rb file

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

如果有问题,来源可能在那儿,对吧?

If there is a problem, the source is likely over there, right?

我是否需要安装deflator宝石?

Do I need to install the deflator gem?

推荐答案

启用压缩

将其添加到config/application.rb:

Add it to 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

这篇关于如何在Ruby on Rails中启用压缩?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆