Purge and update html5 application cache through javascript

tusharmath picture tusharmath · Aug 5, 2012 · Viewed 15.3k times · Source

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?

Answer

Rob Angelier picture Rob Angelier · Aug 5, 2012

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