Built For Speed: Using the AssetPackager Plugin

Inspired by the recent launch of code.google.com/speed, I decided to sit down and see how I could apply their guidelines. This is the first in a series of posts on improving front-end performance for your Rails applications.

First of all, we need to create our sample application. Recently, I’ve been using Beet, a gem for generating Ruby projects, but you can create your local version however works for you. Using Beet, the following command tells the Rails generator to use MySQL, remove unused files (public/index.html, etc.) and initialize a Git repository:

   beet -g built_for_speed -r="rails/db/mysql, rails/clean_files, rails/git"

Next, let’s create a Post resource:

   script/generate scaffold Post title:string body:text

Make sure your databases are created, and run the migrations. Note that for the purposes of this tutorial, I’m running the application in production mode (to better see the speed benefits), using Passenger on my MacBook Pro. By the way, I highly recommend the Fingertips Passenger preference pane for managing your sites locally.

   RAILS_ENV=production rake db:migrate

Now let’s add the Blueprint CSS framework. Download the latest version from blueprintcss.org and unpack it somewhere. Blueprint provides you with compressed versions of the CSS files, but humor me and add the uncompressed versions. From the unpacked directory, copy all the CSS files from /blueprint/src/ into /public/stylesheets/blueprint/ in your application.

Before we start up the application, let’s add the CSS files as well as the default JavaScript files to the head of our posts layout (/app/views/layout/posts.html.erb). The head of your layout file should look something like this (note that I added my own base.css file):

   <head>     ...     <%= stylesheet_link_tag 'blueprint/reset', :media => 'projection, screen' %>     <%= stylesheet_link_tag 'blueprint/typography', :media => 'projection, screen' %>     <%= stylesheet_link_tag 'blueprint/forms', :media => 'projection, screen' %>     <%= stylesheet_link_tag 'blueprint/grid', :media => 'projection, screen' %>     <%= stylesheet_link_tag 'blueprint/print', :media => 'print' %>     <!--[if lt IE 8]> 		<%= stylesheet_link_tag 'blueprint/ie', :media => "screen, projection" %> 	<![endif]--> 	<%= stylesheet_link_tag 'base', :media => 'projection, screen' %> 	<%= javascript_include_tag :defaults %>   </head>

Okay, now let’s fire up the application. I’ll be using Firefox so we can profile the application using YSlow. Go ahead and create your first post. Once you’re looking at the ‘show’ page, let’s open up Firebug and click on the “YSlow” tab. On the YSlow screen, click the “Run Test” button to get your page grade.

Posts: show
Uploaded with plasq’s Skitch!

Bummer, we got an overall D – not so good. Let’s take a look at what’s going on. YSlow grades are listed in order of importance, so let’s check out the first section: “Make fewer HTTP requests”. Looks like we got a C in that area. What can we do to improve our grade? YSlow gives us some tips: “combine multiple scripts into one script, combine multiple CSS files into one style sheet”. Before we get back to the application, take a look at the “Components” tab in the YSlow dialog.

Posts: show
Uploaded with plasq’s Skitch!

Hmm, five JavaScript files for a total of 234.3K and six CSS files for a total of 18K. We definitely need to work on that.

In order to compress JavaScript and CSS files in my applications, I use Scott Becker’s AssetPackager plugin. Go ahead and install it:

   script/plugin install git://github.com/sbecker/asset_packager.git

The first step after installation is to create the configuration file for AssetPackager:

   $ rake asset:packager:create_yml

You should see a message to reorder the files under ‘base’, so let’s go ahead and do that. Open up the newly-created /config/asset_packages.yml file. You’ll notice that there are two top-level entries – one for ‘javascripts’ and one for ‘stylesheets’. AssetPackager should have correctly generated the required files for the ‘javascripts’ section, but we’ll need to add the Blueprint files. Your completed asset_packages.yml file should look something like this (again, I added a base.css file):

 	---  	javascripts:  	- base:  	  - prototype 	  - effects 	  - dragdrop 	  - controls 	  - application 	stylesheets:  	- base: 	  - blueprint/reset 	  - blueprint/typography 	  - blueprint/forms 	  - blueprint/grid  	  - base 	- print: 	  - blueprint/print 	- ie: 	  - blueprint/ie

Now that the config file is set up, you can go ahead and generate the combined, minified JavaScript and CSS files:

   rake asset:packager:build_all

This command will output a “[name]_packaged” file for each entry under both ‘javascripts’ and ‘stylesheets’. Now we have to tell our application to use those compressed files. Go back to your posts.html.erb layout and change the head to look like this:

   <head>     ...     <%= stylesheet_link_merged :base, :media => "screen, projection" %>     <%= stylesheet_link_merged :print, :media => "print" %>     <!--[if lt IE 8]> 	<%= stylesheet_link_merged :ie, :media => "screen, projection" %>     <![endif]-->     <%= javascript_include_merged :defaults %>   </head>

Okay, now it’s time to see the fruits of our labors. Restart the application in the Passenger preference pane, and reload the post page in Firefox. Now let’s run YSlow again. This time, you should see output like this:

Posts: show
Uploaded with plasq’s Skitch!

Alright! We’ve improved our grade up to an overall B, with an A for “Make fewer HTTP requests”. Let’s take a look at the ‘Components’ tab.

Posts: show
Uploaded with plasq’s Skitch!

Thanks to AssetPackager, we’re down to one JavaScript file for a total of 171.7K and two CSS files for a total of 12.6K. Now there’s eight fewer components, and we’re saving 68K of bandwidth on each request. Nice work!

The source code for this sample application can be found at: github.com/dramsay/built_for_speed

Check back for more performance tips in the future.