Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Purge and update html5 application cache through javascript

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?

like image 753
tusharmath Avatar asked Aug 05 '12 15:08

tusharmath


2 Answers

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.
}
like image 162
Rob Angelier Avatar answered Nov 08 '22 07:11

Rob Angelier


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"....

like image 37
Pedro Cardoso Avatar answered Nov 08 '22 06:11

Pedro Cardoso