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>&amp;lt;link type=&quot;text/css&quot; rel=&quot;Stylesheet&quot; media=&quot;screen&quot; &amp;raquo; <br />
href=&quot;/stylesheets/main.css&quot;/&amp;gt;</code>

You would have:

<code>&amp;lt;link type=&quot;text/css&quot; rel=&quot;Stylesheet&quot; media=&quot;screen&quot; &amp;raquo; <br />
href=&quot;/stylesheets/main.css?1186435642&quot;/&amp;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**.

Post Information

Tags:

We're Reading

Feeds/Syndication

3 Responses to “CSS Versioning with Rails”

Leave a Reply