We are actively developing a website using .Net and MVC and our testers are having fits trying to get the latest stuff to test. Every time we modify the style sheet or external javascript files, testers need to do a hard refresh (ctrl+F5 in IE) in order to see the latest stuff.
Is it possible for me to force their browsers to get the latest version of these files instead of them relying on their cached versions? We're not doing any kind of special caching from IIS or anything.
Once this goes into production, it will be hard to tell clients that they need to hard refresh in order to see the latest changes.
Thanks!
I came up against this too and found what I consider to be a very satisfying solution.
Note that using query parameters
.../foo.js?v=1
supposedly means that the file will apparently not be cached by some proxy servers. It's better to modify the path directly.We need the browser to force a reload when the content changes. So, in the code I wrote, the path includes an MD5 hash of the file being referenced. If the file is republished to the web server but has the same content, then its URL is identical. What's more, it's safe to use an infinite expiry for caching too, as the content of that URL will never change.
This hash is calculated at runtime (and cached in memory for performance), so there's no need to modify your build process. In fact, since adding this code to my site, I haven't had to give it much thought.
You can see it in action at this site: Dive Seven - Online Dive Logging for Scuba Divers
In CSHTML/ASPX files
This generates markup resembling:
In Global.asax.cs
We need to create a route to serve the content at this path:
ContentController
This class is quite long. The crux of it is simple, but it turns out that you need to watch for changes to the file system in order to force recalculation of cached file hashes. I publish my site via FTP and, for example, the
bin
folder is replaced before theContent
folder. Anyone (human or spider) that requests the site during that period will cause the old hash to be updated.The code looks much more complex than it is due to read/write locking.
Helper Methods
You can remove the attributes if you don't use ReSharper.
Feedback appreciated!
What you might do is to call your JS file with a random string each time the page refresh. This way you are sure it's always fresh.
You just need to call it this way "/path/to/your/file.js?
<
random-number>
"Example: jquery-min-1.2.6.js?234266
Rather than a build number or random number, append the last-modified date of the file to the URL as querystring programmatically. This will prevent any accidents where you forget to modify the querystring manually, and will allow the browser to cache the file when it has not changed.
Example output could look like this:
Since you mention only your testers complaining, Have you considered having them turn off their local browser cache, so that it checks every time for new content? It will slow their browsers a touch... but unless you are doing usability testing every time, this is probably a whole lot easier than postfixing the filename, adding a querystring param, or modifying the headers.
This works in 90% of the cases in our test environments.
In your references to CSS and Javascript files, append a version query string. Bump it everytime you update the file. This will be ignored by the web site, but web browsers will treat it as a new resource and re-load it.
For example:
You need to modify the names of the external files you refer to. For e.g. add the build number at the end of each file, like style-1423.css and make the numbering a part of your build automation so that the files and the references are deployed with a unique name each time.