Whenever I put out a new version of CSS or javascript files for any of our web applications, I manually add a ?v=#
to the URL in the head of the HTML (# is a version number, like 3 or 10) to make sure the users browser cache isn't standing in the way of the changes.
But some applications have multiple CSS and multiple JS files in the head and it is tedious to keep track of all changed files.
We use the ASP.NET platform on an IIS server, so using the answer to What is an elegant way to force browsers to reload cached CSS/JS files? is not possible but I am looking for something similar. Preferably all javascript.
I like the suggestion in that post to use a md5 hash but I cannot seem to find a way to calculate the checksum of a CSS or JS file.
The code would look something like this (using jQuery):
$.each($("script[type='text\javascript']"),function(){
$(this).attr("src",$(this).attr("src") + "?v=" + checkMD5Sum($(this).attr("src")) );
});
function checkMD5Sum(url){
//get the hash or something to auto version the file
//I could send the url or filename to a webmethod to get the MD5 check sum, but doing this after page load is not usefull, so what to do?
}
Or perhaps using a postbuild process in Visual Studio, but I have never done that, so I am reluctant to try.
Edit
Going to try this in the web.config as per Adding Caching Profiles
<configuration>
<system.webServer>
<caching enabled="true">
<profiles>
<add extension=".js" policy="CacheUntilChange" />
<add extension=".css" policy="CacheUntilChange" />
</profiles>
</caching>
</system.webServer>
</configuration>