Suffix

Published by Simon Schoeters

GZIP output compression

‘Output Compression’ by Tom Fakes is a Ruby on Rails plugin that allows GZIP output compression for clients that support it.

GZIP compression is a common way to compress the content of web pages by the server before the request is passed on to the browser. The browser decompresses the content and renders it. This method needs less data passing over the network, up to 55% in my case, but has a small performance hit as both the server and the browser need to (de)compress the package.

The Ruby on Rails plugin is great but at the time of writing (in Ruby 1.8.2) it's broken and needs some manual fixing... lets fix it.

  1. First install the plugin (sudo not required):
    ./script/plugin install http://craz8.com/svn/trunk/plugins/output_compression
  2. Open the output_compression.rb file in the vendor/plugins/output_compression/lib/ folder and move the “alias :original_request_for_component :request_for_component” line below the request_for_component one. At first it should look like this:

    module ActionController
      module Components
        protected
          alias :original_request_for_component :request_for_component
          def request_for_component(options)
            request_for_component = original_request_for_component(options)
            request_for_component.is_component_request = true
            return request_for_component
          end
      end
    end

    The end result should look like this:

    module ActionController
      module Components
        protected
          def request_for_component(options)
            request_for_component = original_request_for_component(options)
            request_for_component.is_component_request = true
            return request_for_component
          end
          alias :original_request_for_component :request_for_component
      end
    end
  3. Lastly you need to add the following line in the controller you would like to enable GZIP for. I chose to put this in my application controller (application.rb) to enable compression for all my pages:

    after_filter OutputCompressionFilter

Tools

This blog post is open source. Did you spot a mistake? Have any ideas for improvements? Contribute to this post via Github. Thank you!