Concatenate your stylesheets and JavaScripts in 3 seconds - Rails 2.0 a feature a day #3

I don’t think I need to explain how concatenating your 5 stylesheets and 12 JavaScript files into single files each makes your webpages load faster. Unless you’re using multiple asset hosts (Rails 2 allows for multiple asset hosts), then it becomes tricky to say for sure which method is better, but I digress.

There’re a bunch of really useful plugins that help you bundle your JS and CSS files, most of them offering minifying/packing of JS and stripping of comments and whitespace in CSS as well. I was a big fan (still love it) of Asset Packager, but newer plugins like PackR and Bundle-fu work great too.

With Rails 2.0 though, unless you want the minifying/packing/comment-stripping features, you can do away with installing a packager plugin. Remember your trusty javascript_include_tag and stylesheet_link_tag? They now accept a :cache option:

# :cache => true creates a cached javascript named all.js.
<%= javascript_include_tag :all, :cache => true %>
# This becomes:
# <script type="text/javascript" src="/javascripts/all.js"></script>

# You can name your javascript too, by passing a name instead of true.
<%= javascript_include_tag 'jquery', 'swfobject', 'application', :cache => 'base' %>
# This becomes:
# <script type="text/javascript" src="/javascripts/base.js"></script>

# Same deal for stylesheets...
# :cache => true creates a stylesheet named all.css.
<%= stylesheet_link_tag :all, :cache => true %>
# This becomes:
# <link href="/stylesheets/all.css"  media="screen" rel="Stylesheet" type="text/css" />

# You can name your stylesheet like for javascript_include_tag.
<%= stylesheet_link_tag 'yui/reset-fonts-grids', 'application', :cache => 'styles' %>
# This becomes:
# <link href="/stylesheets/styles.css"  media="screen" rel="Stylesheet" type="text/css" />

Of course, this only takes place if caching is turned on (ActionController::Base.perform_caching), which is on by default in the production environment and off in the development environment.

For those who want to look at some code, check out the changeset.

About the contributor, DHH

Well, DHH hardly needs any mention. David has_one Wikipedia entry. Nice little touch on this new feature though, I love the convenience.

I promise, like I’d said before, to cover contributions by someone less “mainstream” in the next Rails 2 “feature a day” blog post.

4 Comments & TrackBacks ()

Paper doll icon
Ruby, Rails, CSS, Tools, HTML « exceptionz's Gravatar

[…] Rails 2.0 - Concatenate your stylesheets and javascript […]

Posted by: Ruby, Rails, CSS, Tools, HTML « exceptionz on December 14, 2007 3am

Paper doll icon
Brandon Zylstra's Gravatar

Sorry to nitpick, but you’re missing a % on the second line.

Thanks for the awesome Rails 2.0 morsels. I’m hooked on it.

BTW, I’m able to enlarge the comments textarea in Safari 3 (on a Mac) just fine, although Safari has this feature built-in, so it’s not necessary. By “Mozilla” do you mean all Gecko browsers, or just specifically the browser by that name (now dubbed ocean orangutan or whatever they call it). It would seem odd if Firefox couldn’t do it but Mozilla could. If you don’t have access to a Mac, I could test on Camino, Firefox for Mac, and Opera for Mac.

Posted by: Brandon Zylstra on December 15, 2007 2am

Paper doll icon
Chu Yeow's Gravatar

Thanks for pointing that out Brandon. Fixed :)

And yup by “Mozilla” I mean all Gecko browsers. Actually, that whole “enlarge the comments textarea” thing is a throwback from the past (3-4 years ago). I don’t think it’s really that useful nowadays so I may just remove it. (Oh and I have a Mac, but thanks anyway.)

Posted by: Chu Yeow on December 16, 2007 6pm

Paper doll icon
Tecker.LOG » Blog Archive » ????????stylesheets?JavaScripts?????Rails 2.0?? #3(??)'s Gravatar

[…] ??: Concatenate your stylesheets and JavaScripts in 3 seconds - Rails 2.0 a feature a day #3 […]

Posted by: Tecker.LOG » Blog Archive » ????????stylesheets?JavaScripts?????Rails 2.0?? #3(??) on June 3, 2008 11pm

You can subscribe to the RSS feed for comments on this post.

Sorry, this entry is no longer accepting comments. If you have something you really want to say, you can write me.