Ruby on Rails & Compressed Prototype

Hello! Today I’m wandering about a compressed version of prototype to use in my brand new ruby on rails application.
Initialy I didn’t understand why rails doesn’t provide a compressed version of prototype but after some googling I’ve found some usefull answers.

Concept is simply clear.

Why use a compressed/packed version of prototype if we can provide it to clients throught gzip compression with best results? There isn’t a valid reason.
So we need to provide a gzip compressed version of prototype.

How-to provide a gzip compressed version of prototype throught rails?
Before of all, prototype.js isn’t different from other files.
So we can use an usefull plugin to compress output of rails with gzip.
You can install this plugin or copy/paste output_compression.rb code to your app/controllers/application.rb.

After you have to change your application’s routes with something like this

map.connect '/javascripts/gzip/prototype.js', :controller => 'Application', :action => 'prototype'

Now we’re going to define our prototype action

def prototype
  p_string = open('./public/javascripts/prototype.js') {|f| f.read}
  send_data p_string, :filename => "prototype.js", :type => "text/javascript", :disposition => 'inline'
end

We simply read and send to output our original prototype.js version.
Nothing new now.
But If we add this line

after_filter :compress_output

to our ApplicationController output will be compressed with gzip (/javascript/gzip/prototype.js included).

So we can use this helper to load prototype compressed version in our views:

<%= javascript_include_tag 'gzip/prototype' %>

Don’t consider this as the best solution in the word.
Probably we prefer to use mod_gzip of apache or another fast functionality of our proxy.

But, in facts, this solution seems to work and don’t require additionals except for

require 'zlib'
require 'stringio'

Have a nice day!

Tags: , , , , , ,

About Autore

Amo internet e le opportunità che ci offre. Grazie anche a te, che fai parte di questo mondo fantastico.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: