views:

79

answers:

3

I have seen on various websites how developers version their css/javascripts files by specifying querystrings similar to:

<head>
    <link rel="stylesheet" href="css/style.css?v=1">
    <script src="js/helper.js?v=1">
</head>

How is that done? Is it a good practice? I've been searching around but apparently, I'm not looking for the right terms. If it matters, I'm using ASP.NET.

Edit:: I just noticed (via Firebug) that if I "version" my files (?v=1) they will always be loading and will always override the cache. Is there a way around that?

Thanks in advance.

+2  A: 

This helps with caching when you want it to and forcing to download when you don't. Files are cached based on their path. So if the path is the same then it can pull from cache. But if they are different, hence a new version, then it would not use the cache but should pull the new file. At least that is how I have used this.

spinon
+4  A: 

They're not really versioned. We do that because certain browsers won't always request the stylesheets properly (they won't even check for a last modified) so to force them to make a new request, you can bump the number in your html file that references it. It's kind of a hack, but it works.

WildJoe
That's interesting. Can you elaborate on which browsers are problematic? Do you get the same behaviour if you use an E-tag instead of or in conjunction with a last modified date?
Peter Ruderman
It's mostly a problem of caching. If the browser downloads `style.css`, and then a page requests it later, the browser will most likely use the version it already has downloaded. If, however, it sees `style.css?v=1` instead of `style.css`, it will see it as a different file and download the new version.
Ryan Kinal
Right, but the browser is supposed supply the last modified date and/or e-tag of the file stored in its cache when requesting that file from the server. This gives the server a chance to return HTTP304 (not modified) instead of the entire file. I'd like to know more about when and why this mechanism fails.
Peter Ruderman
@WildJoe: Fascinating. That being said, "Can you elaborate on which browsers are problematic?" ?
Rafael Belliard
I don't remember exactly, but they're all old (like IE5/FF1 old). You may also encounter problems if your webserver (or a separate caching layer, like a proxy) caches static content. This trick gets around all of them, but you should remove the ?v=1 if you want it to cache later.
WildJoe
@WildJoe: I just noticed (via Firebug) that if I "version" my files (?v=1) they will **always** be loading and will always override the cache. Is there a way around that?
Rafael Belliard
+1  A: 

They are doing this to make caching for the Browsers more reliable. You can add the version manually, and increment it every time you change the file. This way the Browser thinks it's got a new file and downloads it for sure.

I don't know the way how to do this automatically in ASP.NET, Ruby on Rails for example checks the last changed timestamp on the file and adds this as version number to the file. I'm sure you'll be able to do something similar in ASP.NET.

jigfox
I just noticed (via Firebug) that if I "version" my files (?v=1) they will always be loading and will always override the cache. Is there a way around that?
Rafael Belliard