Combine Multiple CSS/JS Files Into a Single File in Rails
One of the nice features in rails that many people overlook is the ability to combine multiple CSS or javascript files into a single file. Why would you want to do this? Well, it results in fewer HTTP requests being made by a client. The end result is that the page will load faster in most browsers, and your YSlow score will be bumped up a bit.
How do you turn this feature on? It is simple really; it just involves setting a single option in two rails helper calls which you're most likely already using.
If you ever took a look at the documentation for stylesheet_link_tag and javascript_include_tag, you may have noticed that there is a :cache option. This option will basically tell rails to, instead of including all the files individually, combine them into a single file and include that in the HTML. If you set :cache to true, it will set the name of the file to all.css or all.js. If you set :cache to a string value (e.g. "foo"), then the file created will be that name (foo.js).
In order for this to work, ActionController::Base.perform_caching needs to be set to true in the appropriate environment's config file (by default, it is enabled for production but disabled for development).
Why the rails team decided to call the option "cache" instead of something like "combine_files" is beyond me, but I have a feeling that it is probably the biggest reason many people seem to not even know this feature is there.
Topics: Ruby on Rails
Comments: 1 so far
Leave a comment
About Pathfinder
Follow the Blog
-
Get a monthly update on best practices for delivering successful software.
Subscribe via email
Subscribe via RSS
Categories
Topics
Archives
- July 2009
- June 2009
- May 2009
- April 2009
- March 2009
- February 2009
- January 2009
- December 2008
- November 2008
- October 2008
- September 2008
- August 2008
- July 2008
- June 2008
- May 2008
- April 2008
- March 2008
- February 2008
- January 2008
- December 2007
- November 2007
- October 2007
- September 2007
- August 2007
- July 2007
- June 2007
- May 2007
- April 2007
- March 2007
- February 2007
- January 2007
- December 2006
- November 2006
- October 2006
- September 2006
- August 2006
- July 2006
- June 2006
- May 2006
- April 2006
- March 2006
Blogroll
Recent
- Elements of Testing Style
- Aesthetics and Web Design
- Asterisk-Java Testing with Groovy
- 3 Misuses of Code Comments
- Fluently NHibernate
- Digging a Hole and Covering it with Leaves — The Software Development Version
- The Importance of User Experience - Do You Understand It in Your Bones?
- Writing Your Own Protocol With NSURLProtocol
- What’s In Your Dock: iPhone edition
- Feature Fatigue


Thanks for the tip Anthony. Helped me on my way.
regards
Greg
Comment by Greg, Wednesday, January 7, 2009 @ 10:32 pm