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?

3 Answers 3

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

7
  • 4
    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 time
    – Darkmouse
    Commented Aug 29, 2014 at 23:04
  • I was just about to say that stalling heroku-deflater gem solved my problem. Please disregard my previous post. Thanks for your help.
    – Darkmouse
    Commented Aug 30, 2014 at 1:47
  • 1
    heroku-deflater do solve problems, even though it's last update on 16 Dec 2013. Commented Sep 19, 2015 at 13:33
  • wow, simply by installing this gem doc size is now 1/3 of its size. nice - thx Commented Feb 14, 2016 at 0:47
  • 1
    @KickButtowski you should find the module and class already there in ` config/application.rb`. You just need to add the one config line.
    – Ahmed
    Commented Aug 22, 2017 at 13:48
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

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 it Commented Aug 22, 2017 at 23:51
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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.