Debug Offline Application Cache


/ Published in: JavaScript
Save to your folder(s)

Logs offline application cache states. Swaps cache if updated version is available.


Copy this code and paste it in your HTML
  1. var cacheStatusValues = [];
  2. cacheStatusValues[0] = 'uncached';
  3. cacheStatusValues[1] = 'idle';
  4. cacheStatusValues[2] = 'checking';
  5. cacheStatusValues[3] = 'downloading';
  6. cacheStatusValues[4] = 'updateready';
  7. cacheStatusValues[5] = 'obsolete';
  8.  
  9. var cache = window.applicationCache;
  10. cache.addEventListener('cached', logEvent, false);
  11. cache.addEventListener('checking', logEvent, false);
  12. cache.addEventListener('downloading', logEvent, false);
  13. cache.addEventListener('error', logEvent, false);
  14. cache.addEventListener('noupdate', logEvent, false);
  15. cache.addEventListener('obsolete', logEvent, false);
  16. cache.addEventListener('progress', logEvent, false);
  17. cache.addEventListener('updateready', logEvent, false);
  18.  
  19. function logEvent(e) {
  20. var online, status, type, message;
  21. online = (navigator.onLine) ? 'yes' : 'no';
  22. status = cacheStatusValues[cache.status];
  23. type = e.type;
  24. message = 'online: ' + online;
  25. message+= ', event: ' + type;
  26. message+= ', status: ' + status;
  27. if (type == 'error' && navigator.onLine) {
  28. message+= ' (prolly a syntax error in manifest)';
  29. }
  30. console.log(message);
  31. }
  32.  
  33. window.applicationCache.addEventListener(
  34. 'updateready',
  35. function(){
  36. window.applicationCache.swapCache();
  37. console.log('swap cache has been called');
  38. },
  39. false
  40. );
  41.  
  42. setInterval(function(){cache.update()}, 10000);

URL: http://jonathanstark.com/blog/debugging-html-5-offline-application-cache

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.