5

I developed a web application in asp.net mvc 4 where I'm serving some resource files like css, js, images etc.

And to always serve an updated file, i appended a random string into query string. That query string value changes when I publish my application. So each publish activity changes that string.

But browser does not downloads the files even with changed URL. Browser downloads the file first time and always serves from cached copy. Below is my code.

<script src="~/Content/js/userdefined/dashboard.js?t=@Constants.RandomString" defer></script>

public static class Constants
{
    public static readonly string RandomString = Guid.NewGuid().ToString().ToLower();
}

So each time application gets published, it generates a new string which is working as expected.

Can anyone help in this like why browser is not downloading updated file even when url gets changed?

Dharman
  • 30,962
  • 25
  • 85
  • 135
Jitendra Pancholi
  • 7,897
  • 12
  • 51
  • 84
  • Have you tried to disable cache by setting IIS manager-> configuration manager->system.webserver->cache controlmode to DisableCache – Jokies Ding Apr 03 '20 at 07:21
  • I deployed the application on MS Azure in App Services web app. How to disable that? – Jitendra Pancholi Apr 03 '20 at 09:33
  • I would try to replace `@Constants.RandomString` by `@Guid.NewGuid()` which should generate new random string on every call to ensure that proper js got loaded. If works, ensure that static `@Constants.RandomString` got updated after each publishing which might be not the case (i.e. static instance will be same if publish don't touch web.config or \bin). – user2316116 Apr 12 '20 at 15:42
  • @user2316116 That's not the case. I already checked with each publish, Constants.RandomString generates the new string but still js does not downloaded automatically. I have to do hard-refresh – Jitendra Pancholi Apr 13 '20 at 20:13
  • Have you already looked at [this](https://stackoverflow.com/questions/49547/how-do-we-control-web-page-caching-across-all-browsers) stackoverflow page? – Chiel Apr 17 '20 at 14:00
  • If you published your application via Visual Studio Publish, please take a look at this link https://stackoverflow.com/a/53695878/1084987. You might have make sure that previous files are deleted and new files are propagated in the AppService – Clyde Apr 18 '20 at 03:01

1 Answers1

-1

I'm assuming that the browser is not updating due to it already having information in it's cache. Lots of browsers have this issue when someone is updating CSS that is not directly inside of the web document, and is being loaded from another file. To reset the page's information completely, you can do Control + Shift + R for a hard reset.

coffee
  • 159
  • 1
  • 5
  • Please read the question carefully. You cannot asks your users to do hard-refresh and also how do they that I have published a new build? – Jitendra Pancholi Apr 13 '20 at 20:14