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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With