Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to enable compression in Ruby on Rails?

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?

like image 379
Darkmouse Avatar asked Aug 29 '14 21:08

Darkmouse


3 Answers

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

like image 106
Ahmed Avatar answered Nov 01 '22 01:11

Ahmed


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

like image 18
NateQ Avatar answered Nov 01 '22 02:11

NateQ


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

like image 3
Itay Grudev Avatar answered Nov 01 '22 03:11

Itay Grudev