12

I arrive to this problem quite a lot of times, where some of the users have a corrupt application cache (HTML 5).

I do update the manifest file every time there is a new release still some times some users get a corrupt application cache.

I such a case I want to fully clear what is there in their application cache and load all the fresh content from the server.

Is there a way to that using Javascript?

tusharmath
  • 10,622
  • 12
  • 56
  • 83
  • Did you take a look at this post? http://stackoverflow.com/questions/1011605/clear-the-cache-in-javascript or this one: http://stackoverflow.com/questions/8155064/how-to-programmatically-empty-browser-cache – Rob Angelier Aug 05 '12 at 15:20
  • so we can't update the HTML 5 version of application cache also? – tusharmath Aug 05 '12 at 15:22

2 Answers2

13

According to the following article on

http://www.w3schools.com/html5/html5_app_cache.asp

there are three ways on wich the application cache will be reset, these are:

  1. The user clears the browser cache
  2. The manifest file is modified
  3. The application cache is programmatically updated

More information about programmatically updating the application cache can be found here:

http://www.html5rocks.com/en/tutorials/appcache/beginner/

It looks something like this:

var appCache = window.applicationCache;

appCache.update(); //this will attempt to update the users cache and changes the application cache status to 'UPDATEREADY'.

if (appCache.status == window.applicationCache.UPDATEREADY) {
  appCache.swapCache(); //replaces the old cache with the new one.
}
Rob Angelier
  • 2,335
  • 16
  • 29
  • I am still facing the same problem, I think because the manifest file has effectively not changed. Thus the stored content does not get updated. Only the manifest file is re-fetched. – tusharmath Aug 05 '12 at 15:43
  • 2
    @Andreas they are just one of the many sites who provide this information, and its correct. That could be your problem, the cache won't be cleared if you don't apply to one of the above conditions. – Rob Angelier Aug 05 '12 at 15:46
  • The first link is broken – Davide Cannizzo Aug 25 '18 at 18:43
  • The Application Cache [is now deprecated](https://developer.mozilla.org/en-US/docs/Web/API/Window/applicationCache): you should use a [service worker](https://developer.mozilla.org/en-US/docs/Web/API/Service_Worker_API/Using_Service_Workers) to cache the files instead. – Anderson Green Feb 10 '22 at 21:03
7

This one is quite old but as I see a wrong answer being up-voted, I felt like giving some hint....

If ones has the trouble of looking at the spec, you can see that there's no way for code to force the browser to reload the cache, unless there's a change in the manifest, and that's when "appCache.status == window.applicationCache.UPDATEREADY" is true.

Look here http://www.w3.org/TR/2011/WD-html5-20110525/offline.html

"updateready The resources listed in the manifest have been newly redownloaded, and the script can use swapCache() to switch to the new cache."

So, reading it carefully, you find that the applicationCache gets to that status when the resources where just downloaded... that is.. a previous "downloading" event occurred... and previous to that one a "checking"....

Pedro Cardoso
  • 373
  • 3
  • 7