2

I'm building a asp.net website (use HTML, C#, Jquery ..).
Then. Website is published.
Then. I have some bug need to fix in javascript code file.
I fixed it and republished website.
But, browser save cache or another browsing data, and the user did not get fixed version.
So, how can I clear browsing data or cache of user manually (by javascript, jquery or C#) when I change some litle code or fix small bug in js file? I cannot say with user so "U must clear browsing data to get new version !"
Thanks.

Đồng Vọng
  • 159
  • 2
  • 10

4 Answers4

1

This may not directly answer your question but if you are using any bundler like webpack or task runner like gulp, you can actually generate the file with a hashkey.

for example the bundler will generate a production version of the js file as

myFile.d587bbd6e38337f5accd.js

d587bbd6e38337f5accd is the hash key generated by the bundler & it will be injected by the bundler itself. In such case every release will have a js file with modified hash key. So it wont load from cache

You can check this LINK to know about it

brk
  • 43,022
  • 4
  • 37
  • 61
1

In case of javascript use cacheBustingUrl whose format is given below which is append to Date.Now() which will solve the problem of caching.

   <script>
         var scriptUrl = "/site/js/script.js",
         cacheBustingUrl = scriptUrl + "?" + Date.now(),
         newScriptElement = document.createElement("script");
         newScriptElement.setAttribute("src",cacheBustingUrl);
         document.body.appendChild(newScriptElement);
    </script>

This should append a new script element like

 <script src="/site/js/script.js?1404918388711"></script>

to the end of the page, which the browser will load like any other script tag.

Virendra Singh
  • 278
  • 3
  • 9
0

You can try setting a refresh on your page using window.location.reload(true) in theory this will ignore all the cached files and retrieve new copies of your website to the user.

Maybe create check that will refresh the page if its not up to date.

Community
  • 1
  • 1
SergejV
  • 398
  • 1
  • 7
  • 15
0

no browser can allow you to remove its cache.its a very big security aspect but you can prevent cashing by passing this meta in your HTML

<meta http-equiv='cache-control' content='no-cache'>
<meta http-equiv='expires' content='0'>
<meta http-equiv='pragma' content='no-cache'> 

Also You have to turn off auto complete textbox by

formname.setAttribute( "autocomplete", "off" );
PedFoot
  • 3
  • 4