How to enable compression in Ruby on Rails?
Asked Answered
M

3

26

I posted a similar question here

Serving Compressed Assets in Heroku with Rack-Zippy

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

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

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

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

http://browserspy.dk/webserver.php

It turns out that my web server is WEBrick.

The PageSpeed Insights Page only lists the following 3 servers

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

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

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

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.

Edit

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

I confirmed that I had it by running

rake middleware
=> use Rack::Deflator

and then

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

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?

Thank you for your help.

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?

Do I need to install the deflator gem?

Michaelmas answered 29/8, 2014 at 21:20 Comment(0)
P
36

Enable compression

Add it to config/application.rb:

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

Source: http://robots.thoughtbot.com/content-compression-with-rack-deflater

Padlock answered 29/8, 2014 at 21:33 Comment(7)
After running PageSpeed again, I still receive the same message that I need to enable compression. Also, using PageSpeed and Network, my size and content are still virtually identical. I appreciate the response though, and for that you get my vote. Thank you for your timeMichaelmas
I was just about to say that stalling heroku-deflater gem solved my problem. Please disregard my previous post. Thanks for your help.Michaelmas
heroku-deflater do solve problems, even though it's last update on 16 Dec 2013.Bagger
wow, simply by installing this gem doc size is now 1/3 of its size. nice - thxTillich
can anyone tell me what YourApp means here? Should I put the name of app or ...?Afghani
@KickButtowski you should find the module and class already there in ` config/application.rb`. You just need to add the one config line.Padlock
@Padlock Is there a point in using both the heroku-deflater gem and the rack deflater line? Would they cause conflict?Claustral
U
20

Rack::Deflater should work if you use insert_before (instead of "use"), to place it near the top of the middleware stack, prior to any other middleware that might send a response. .use places it at the bottom of the stack. On my machine the topmost middleware is Rack::Sendfile. So I would use:

config.middleware.insert_before(Rack::Sendfile, Rack::Deflater)

You can get the list of middleware in order of loading by doing rake middleware from the command line.

Note: A good link for insert_before vs Use in middleware rack

Unexceptional answered 23/12, 2015 at 12:1 Comment(1)
I added a link I found regarding to use insert_before (instead of "use"), to place it near the top of the middleware stack. Hope you are fine with itAfghani
P
5

As per the author of Rack::Deflater it should be placed after ActionDispatch::Static in a Rails app. The reasoning is that if your app is also serving static assets (like on Heroku, for example), when assets are served from disk they are already compressed. Inserting it before would only end up in Rack::Deflater attempting to re-compress those assets. Therefore as a performance optimisation:

# application.rb

config.middleware.insert_after ActionDispatch::Static, Rack::Deflater

Pruter answered 7/9, 2020 at 11:3 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.