iPhone Optimization: stylesheet_link_tag caching

Posted by Marc Love
on Friday, June 29

This change to the Rails core is a couple months old, but especially relevant when developing for the iPhone. One of the ways to optimize the performance of apps delivered to iPhones is to reduce the number of requests required for each action. If you like to split up your css files in logical order like I do, then you might find the stylesheet_link_tag caching useful.

Before:
stylesheet_link_tag :all # returns =>
  # <link href="/stylesheets/style1.css"  media="screen" rel="Stylesheet" type="text/css" />
  # <link href="/stylesheets/styleB.css"  media="screen" rel="Stylesheet" type="text/css" />
  # <link href="/stylesheets/styleX2.css" media="screen" rel="Stylesheet" type="text/css" />

stylesheet_link_tag "shop", "cart", "checkout" # returns =>
#  <link href="/stylesheets/shop.css"  media="screen" rel="Stylesheet" type="text/css" />
#  <link href="/stylesheets/cart.css"  media="screen" rel="Stylesheet" type="text/css" />
#  <link href="/stylesheets/checkout.css" media="screen" rel="Stylesheet" type="text/css" />
After:
stylesheet_link_tag :all, :cache => true # returns =>
  # <link href="/stylesheets/all.css"  media="screen" rel="Stylesheet" type="text/css" />

stylesheet_link_tag "shop", "cart", "checkout", :cache => "payment" # returns =>
#  <link href="/stylesheets/payment.css"  media="screen" rel="Stylesheet" type="text/css" />

You need to have ActionController::Base.perform_caching = true for this to work.

Comments

Leave a response

Comment