Rails Speed Tip: link_tag Caching

06 Mar 2007 | Tags rails programming tips

Have you ever tried concatenating your JavaScript and CSS files for performance improvements? The idea is that latency is a bigger issue than file size when loading web pages, so stuffing all your JavaScript into a monolithic file for deployment should improve performance.

I wrote a rake task to do this for some of my applications (such as tiktrac). This is slightly more cumbersome than a feature I spied in the ActionPack changelog:

Added caching option to AssetTagHelper#stylesheet_link_tag and
AssetTagHelper#javascript_include_tag [DHH]. Examples:

stylesheet_link_tag :all, :cache => true # when ActionController::Base.perform_caching is false =>
stylesheet_link_tag :all, :cache => true # when ActionController::Base.perform_caching is true =>

…when caching is on, all.css is the concatenation of style1.css, styleB.css,
and styleX2.css. Same deal for JavaScripts.

Read more in the asset_tag_helper.rb source.