views:

95

answers:

4

I have seen quite a lot of websites doing this (even stackoverflow itself) within their generated HTML source, accessing a specific version of a CSS or JavaScript file with GET parameters. What's the point of it?

Example:

<link rel="stylesheet" href="http://sstatic.net/so/all.css?v=6230"&gt;
<script type="text/javascript" 
        src="http://sstatic.net/so/js/master.js?v=6180"&gt;&lt;/script&gt;

Is it simply a manner of coherence or best practice? Is it simply so that clients with old cached versions on their browsers are forced to update their outdated version?

+4  A: 

Is it simply so that clients with old cached versions on their browsers are forced to update their outdated version?

Exactly.

Check out this question for details, further links and discussion and this question on how Stack Overflow itself employs the method.

Pekka
Thanks a lot for the information. I guess I guessed right :P. Again, a good practice seems to fetch the repository revision for the version number. I am using git, maybe git describe would do it but I'm wondering if there is still a popular way for generating the number without too much code change (changing version each time the css/js file is modified would be a bit tedious). I am thinking of using the file's modification date maybe?
Steven Rosato
@Steven Rosato Good question. I don't know of such a way, you could of course use the general revision number but that would trigger the reload quite often. Anyway, if you can use URL rewriting, you can have every `/12345/style.css` point to the same `style.css` file, saving you the hassle of creating the actual directory when checking in. That way, you would have to worry only about how to call the style sheet. Maybe worth an extra question.
Pekka
+1  A: 

Yes, it is for bursting browser and proxy caches. There's no other purpose.

Well, theoretically you can dynamically generate javascript and then you'll need those parameters. JSONP works that way for example. But mostly it is for invalidate caches.

vava
A: 

It's to force the browser get the new version and not simply use a cached, older version. That's it.

Brock Batsell
A: 

This is used with far future expiration for CSS/Javascript. This allows the site to tell your browser to cache the data for a very long time (e.g. 5 years). When the css or js is updated then the version number would change, forcing a cache miss.

Browsers cache the css/js by the full query string.

hobodave