Suffix

GZIP output compression

Add HTTP compression to all Ruby on Rails requests.

“Output Compression” by Tom Fakes is a Ruby on Rails plugin that adds GZIP output compression for clients that support it.

GZIP compression is a common way to compress the content of web pages on the server before the request is sent on to the browser. The browser decompresses the content and renders it. This method passes less data over the network, up to 55% less 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 is broken at the time of writing (in Ruby 1.8.2) and needs some manual fixing… lets fix it.

  1. First, install the plugin (sudo not needed).

    ./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. Change 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

    To 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. Next, add the following line to the controller you would like to enable GZIP for. I chose to add it to my application controller to enable compression for all my pages.

    after_filter OutputCompressionFilter

Check for GZIP compressed requests via the HTTP Compression Test.