Whenever we do some modification to the existing .js and .css files and move them to a production environment, those changes may not get reflected in the browser of the customer who is using our software. This is because those .css and .js files have been cached in the browser. When the user accesses the URL, it will take the cached .js and .css files. That is the reason those new changes will not appear to the user. The user either has to clear the browser cookie & reload the page or else he or she has to do a hard refresh of the page by pressing Ctrl+F5.
But, we cannot ask each and every user to clear the cookie of their browser. We cannot even ask them to go for a hard refresh of the page. So, we can hack this through an approach called Cache Busting.
Cache Busting
It is a unique string appended to the path of the .js file or .css file in the form of a query string.
Ex: /jsScripts/myUpdates.js?v=1
When a webpage is downloaded, then its associated files are also downloaded and stored in the browser's cache on the user’s machine.This has to happen as this process improves the performance where the page doesn't need to download these associated files again and again whenever the page is refreshed.
Now, there can be an issue with this process. Whenever we do some changes to any of the existing .js or .css files and update the production environment, the client's browser may not be able to get to see upon refreshing the page as it has cached those files during previous page refresh. As a developer, we need to ensure that client's browser should get these updates in existing files without much struggle.
Therefore, cache buster query string can be updated so that the browser doesn't recognize the file in the cache memory and downloads the new file on a refresh of the page.
When to use this mechanism
Do not do for all .js, .css files in your solution package. Analyze which files often get modified and apply a cache busting mechanism only to those files. Because caching is essential for performance point of view. For every refresh of the page, if all of its associated .js, .css files get downloaded, then it will decrease the performance. Hence, select only a few files which often get modified.
Static Cache busting
When a file is updated, all references to that file could be manually updated to increment sof version numbers such as 1, 2, 3, etc. But this is not an ideal approach as we need to do lots of manual work to change version number every time we modify the file.
- Ex: /jsScripts/myUpdates.js?v=1
- Ex: /jsScripts/myUpdates.js?v=2
- Ex: /jsScripts/myUpdates.js?v=3
I endorse this approach as it helped me from a deployment and code management perspective.
Suppose, I have done some changes today and upgraded the site offline when the client is not using the site. Assume that the client has used the site yesterday and if I append today's date and time as a query string to the path of the file, then it searches for the file with query string equal to today's date and time when the client has loaded the page. Since it doesn't recognize the file in the cache, then it will download the file from the server, hence the user can see the latest version of file.
Here, the user doesn't need to go for a hard refresh through ctl+f5 or deleting the cookie.
Inside the <head> tag load all those files which you think get updated more often.
- <script type="text/javascript" language="javascript">
- var versionUpdate = (new Date()).getTime();
- var script = document.createElement("script");
- script.type = "text/javascript";
- script.src = "/myProduct/scripts/myUpdates.js?v=" + versionUpdate;
- document.body.appendChild(script);
- </script>
Note that, in the above snippet, I have done DOM manipulation rather than using document.write (Script_path_with_query_string); , because document.write is a bad practice and you should try to avoid it.

Ahmad HabibPosted Apr 3, 2020, 11:11 AM
Does this work for Angular8?
Mankgwanyane TlakaPosted Oct 1, 2019, 12:04 AM
Thank you Pradeepa, I was stuck on this for about 3 hours and I have to deliver this project to a client soon.
Dan BarryPosted Sep 27, 2019, 4:32 PM
When using MVC I create a varaible "DateTime dt = DateTime.Now; and then at the end of the ... <link rel="stylesheet" href="@baseUrl/Content/CustomCSS/modal.css?v=@dt" type="text/css" /> add a querystring as such: <link rel="stylesheet" href="@baseUrl/Content/CustomCSS/modal.css?v=@dt" type="text/css?v=@dt" /> FYI: string baseUrl = Request.Url.Scheme + "://" + Request.Url.Authority + Request.ApplicationPath.TrimEnd('/');
bhavani mPosted Mar 26, 2019, 1:28 AM
But in react js this technique is not working that means adding date to js and css files.not updating changes without refresh can you suggest me any other solution. Thanks in Advance.
ramesh csePosted Sep 20, 2018, 12:22 AM
Its very useful for everyone
gana VSOPPosted Jul 19, 2018, 8:50 PM
Anyone tested? is it working
Sundaram SubramanianPosted Mar 12, 2018, 7:59 AM
Well written buddy . Keep good post
Hiten PandyaPosted Mar 10, 2018, 4:23 AM
I have one question how do we perform cache busting in bundleconfig file .Thank you for sharing this article. It is very useful information for day to day life.