- We design and build extraordinary applications for companies looking to make the next great idea a reality.
- learn more
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
Leave a comment
About Pathfinder
Recent
- Walk-Through Test Coverage
- Where minimalism fails: The problem with Apple’s less-is-more approach
- jQuery goodness with ASP .NET
- Design Thinking
- Bullseye Diagram
- Roles Testing For Security
- Blackbird takes the pain out of JavaScript logging
- Making GWT JSON not Quite so Painful
- IDEA - preconference workshop 06 Oct 08
- HTML5, Ajax history management, and The Ajax Experience 2008 Boston
Archives
- 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


