views:

184

answers:

2

I've noticed that on some websites (including SO) the link to the CSS will look like:

<link rel="stylesheet" href="http://sstatic.net/so/all.css?v=6638"&gt; 

I would say its safe to assume that ?v=6638 tells the browser to load version 6638 of the css file. But can I do this on my websites and can I include different versions of my CSS file just by changing the numbers?

+11  A: 

That loads all.css with a different query string so that if version 6637, for instance, is already cached on your machine, you'll get the new one (6638). Changing that number (in this case) will not give you a different file.

This is just a cache trick so they can send the file down with no expiration (i.e. you never have to ask for it again), because when it does change, the "file name", changes.


That said, you could make it so you load a different version based on the query string parameter. Doing so would be slightly non-trivial and akin to how you get different questions when you pass a different question ID to the URL of this page.

Michael Haren
So does the browser just savse the number from the last time you visit and then if it doesn't match the next time, it reloads the CSS file, otherwise it just uses the cached one?
Bob Dylan
Yes, more or less. The thing here is that the files are sent down with extra data (headers) that say "never ask for this file again--it never changes". Since they do that, the site is faster (one less request each load). BUT when the file *does* change, they communicate that by changing its name.
Michael Haren
Try to ignore that the version number is a querystring parameter--think of the whole thing--path, name, query string-- as part of the file name. When any of it change, it's treated as a completely unique "file" or resource.
Michael Haren
Thanks! Also the edit to your question was great, thanks for clarifying.
Bob Dylan
A: 

Similar answers and some more links/detailed explanations are given in this question about javascript caching.

Paul Rowland