I cannot count how many times in my life as a developer that I have been contacted after a deployment by a client that is unable to see any of the changes that were deployed (or something isn't working). There are a variety of ways to respond to this but it usually involves a process of holding a key and pressing another or going into options.
I'm not terribly fond of this process, but thankfully the latest release of ASP.NET allows me to easily get rid of this issue through the use of TagHelpers.
Caching and Issues with It
Caching issues generally result from a browser holding onto an older version of a file after it has been changed.
This is usually a good thing for everyone as the server is happy because it doesn't have to serve out a file that it has already served to a client and the client is happy because they experienced zero load-time for the file. It sounds a bit like a win-win scenario, however, when some of these files that were being cached are changed after a release or deployment, things can go horribly, horribly wrong.
Non-cached objects of an application will now be talking to older versions of files, script files may be relying on DOM elements that either no longer exist or simply don't exist yet, CSS rules can change and cause your application to look strange or worse, your application may not simply work at all.
Fixing Caching Issues
Caching issues are usually resolved at the client-level or more specifically by the end-user. The following is an exchange that you may have taken part in previously or just an example of one that could occur:
- Developer: "Well, we deployed the update this morning. Everything should be working along with all of the new features."
- Client: "I just tried accessing it and everything looks funny, besides that, nothing seems to work."
- Developer: "You may need to clear your cache for the changes to take effect."
- Client: "Cache? Haven't I paid you folks enough already?"
- Developer: "No, the cache in your browser. It's storing the older files prior to the update and you need to get rid of them."
- Client: "Well how do I do that?"
- Developer: "You can either go into Options within your browser and choose the Clear Temporary Files option or hold the CTRL key and press F5. You should see a flicker and then it should work."
- Client: "Yep. I just did that and it seemed to fix it. Now do I need to do this every time that I open the application or site?"
The issue with this scenario is that it should not be the responsibility of the end user to have to clear their cache every time something happens.
How Caching Can Mess You Up
Files are cached in different configurable ways, but the most common case where a file is being cached unnecessarily is also the most common way the file is referenced.
Let's take the popular client-side library jQuery for example and assume we are referencing it within our web application:
- <script src="~/Scripts/jquery.js"></script>
The problem comes in when the developer decides to upgrade the version of jQuery and we aren't generally talking about minor updates which will not introduce major breaking changes, but let's use the leap from jQuery 1.x to jQuery 2.x. This release changed all sorts of APIs, removed support for older browsers and did quite a few other things that could break an application.
Now if the user has the previous version cached and you revisit the application after changing quite a bit of your markup, the newer markup should be picked up, but the older jQuery file is still going to be used, which is not what we want.
Removing Caching Entirely
One of the most common solutions to get around this is to simply remove caching entirely. This is usually done by appending a randomly generated query string parameter that uses something like a timestamp as seen below :
- <!-- This might render something like "~/Scripts/jquery.js?dt=123456789" -->
- <script src="~/Scripts/jquery.js?dt=@(DateTime.Now.Ticks)"></script>

Muhammad Aqib ShehzadPosted Dec 19, 2016, 4:24 AM
Fantastic piece of write up. Rion Williams is there any possibility to cache bust angular2 using this approach.
Bhushan GawalePosted Aug 26, 2016, 5:21 AM
Great write up Rion! just curious, how does it keep the track of which file is changed and when to change the version number.
Ramesh PalaniappanPosted Aug 25, 2016, 10:52 AM
Nice
Vignesh ManiPosted Aug 25, 2016, 6:31 AM
Good one