One trick that all experienced designers rely on, is that the browser will cache css, so that it doesn’t need to be download on subsequent page loads. That is a life saver! On my current work project, the css weighs in at 41k. The downside to that is that sometimes the browser is stupid about caching and won’t redownload even after you have changed your stylesheet.
Here is the trick, put a date at the end of your stylesheet indicating the time you last saved the file. So instead of
<code>&lt;link type="text/css" rel="Stylesheet" media="screen" &raquo; <br /> href="/stylesheets/main.css"/&gt;</code>
You would have:
<code>&lt;link type="text/css" rel="Stylesheet" media="screen" &raquo; <br /> href="/stylesheets/main.css?1186435642"/&gt;</code>
Notice the random string of digits in the query string. That represents seconds from the epoch, a common way to represent time for computers.Obviously a human didn’t put those digits there, Rails did it for me. The built in **stylesheet\_link\_tag** helper, automatically adds those digits. As long as your file doesn’t change, the user’s browser shouldn’t download it twice.In my opinion this is the best solution for this problem. It doesn’t create any extra work for developers or designers and it works the same in production as it does in development.
The only time a browser might miss the cache is after a release. Your css file might not have changed but the modified time might be incremented anyway. I think this is a small price to pay for the simplicity of this approach to problem solving.
Rails also provides this for **javascript\_include\_tag** and **image_tag**.
I think this is one of those really nice “hidden” features of Rails that makes it so easy to work with. When showing some developers Rails for the first time, I was asked, “Why should I use javascriptincludetag or stylesheetlinktag instead of just typing out the html?” This is exactly why. However, I still haven’t been able to figure out why other rails helper methods are better than just typing the html. For example:
<%= linkto “Some Link”, :action => “redirectme” %>
vs.
<a href=”/contoller/redirect_me”>Some Link</a>
What’s the difference?
At CrazyEgg.com we have something similar, but it’s vitally important that our CSS, Javascript and HTML are all in sync and cached based on the revision number.
So we use this to handle the urls to all that stuff:
def cache_url_for_revision(url) url += (url.include?("?") ? "&" : "?") + "__r=#{$revision}" url += '_' + rand(999999).to_s unless RAILS_ENV == "production" url endIt also happily randomizes the url to force cache reset in dev mode.
@Jason
I would say the best reason to use the url helper functions in rails is that it interacts with the route.rb file. So if you add a route that matches a url pattern, all your urls should update use the new route. That is confusing, here is a goodish (a little outdated) chapter, http://manuals.rubyonrails.com/read/chapter/65.
@Thomas
It sounds like you have a good system in place. Using the built in methods to serve js you could have set an environment variable “RAILSASSETID” that would get tacked on to the url instead of the modified time. If you don’t set it in dev, it will always take the last modified time which should insure that you don’t get cached files.
Cheers