3

After deploying a new release of an ASP.NET website, is there a way to force clients' browsers to refresh cached items, especially for images, CSS, and Javascript files?

The problem is that I can’t go around telling everybody that uses the site to hit CTRL+F5. How can I force the browsers to do it?

Tohid
  • 5,185
  • 7
  • 42
  • 75
Nada
  • 81
  • 2
  • 4
  • If you use bundles, it will all be handles automatically. Otherwise, give you scripts a version number –  Feb 13 '17 at 09:22
  • Possible duplicate of [What does appending "?v=1" to CSS and Javascript URLs in link and script tags do?](http://stackoverflow.com/questions/3466989/what-does-appending-v-1-to-css-and-javascript-urls-in-link-and-script-tags-do) – Jasen Feb 13 '17 at 17:59
  • What you're looking for is called "Cache Busting". You can read up on that and see different methods for implementing a strategy. Most involve appending something to your filenames to force a re-pull from the server. – Ian Feb 13 '17 at 19:51
  • @StephenMuecke I use bundles, but the cache isn`t updated – Nada Feb 14 '17 at 09:37
  • Then I assume you must have disabled it by setting either `BundleTable.EnableOptimizations = false;` in `RegisterBundles` or `` in the `web.config` file. And suggest you read the _Bundle Caching_ section of [Bundling and Minification](https://docs.microsoft.com/en-us/aspnet/mvc/overview/performance/bundling-and-minification), in particular note: _If any file in the bundle changes, the ASP.NET optimization framework will generate a new token, guaranteeing that browser requests for the bundle will get the latest bundle_ –  Feb 15 '17 at 02:22
  • I already know that it generates a new token if any file is updated. I recognized that it is working with firefox and Microsoft edge but not with chrome. Do you know where is the problem? @StephenMuecke – Nada Feb 15 '17 at 12:13
  • Explained the problem – Tohid Feb 15 '17 at 20:02

1 Answers1

4

Nada,

I have faced this issue on sites in the past, mainly FireFox and Chrome.

For each CSS and Javascript file I append a parameter ?1d= to the URL. When I deploy a change to the site that includes CSS or JavaScript it will pickup the new ID# and the end user will get the current CSS and JavaScript without the need of the hard CTRL+F5 Reset.

src="/Scripts/myJavaScript.js?id=71.11"

href="/Styles/MyStyles.css?id=71.11"
Patrick
  • 1,545
  • 12
  • 20