Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to version files in the <HEAD> section?

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.

like image 317
rebelliard Avatar asked Aug 11 '10 17:08

rebelliard


3 Answers

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.

like image 102
WildJoe Avatar answered Oct 04 '22 21:10

WildJoe


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.

like image 41
spinon Avatar answered Oct 04 '22 22:10

spinon


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.

like image 20
jigfox Avatar answered Oct 04 '22 21:10

jigfox