Skip to content

Instantly share code, notes, and snippets.

@gnab
Last active December 18, 2015 07:39
Show Gist options
  • Save gnab/5748429 to your computer and use it in GitHub Desktop.
Save gnab/5748429 to your computer and use it in GitHub Desktop.
Reload until application cache is in sync (noupdate). If you don't reload after app cache is updated, you'll still be using old html/css/js until the next time you reload. Should be called initially with a callback that loads your application like you normally would.
function whenCacheInSync(callback) {
// 1. Checking
var onChecking = function () {
window.applicationCache.removeEventListener('checking', onChecking);
window.applicationCache.addEventListener('noupdate', onNoUpdate);
window.applicationCache.addEventListener('downloading', onDownloading);
};
// 1A. No updates, load app
var onNoUpdate = function () {
window.applicationCache.removeEventListener('noupdate', onNoUpdate);
window.applicationCache.removeEventListener('downloading', onDownloading);
callback();
};
// 1B. Updates available, need to download...
var onDownloading = function () {
window.applicationCache.removeEventListener('noupdate', onNoUpdate);
window.applicationCache.removeEventListener('downloading', onDownloading);
window.applicationCache.addEventListener('updateready', onUpdateReady);
};
// 2. Updates downloaded, reload...
var onUpdateReady = function () {
window.location.reload();
};
window.applicationCache.addEventListener('checking', onChecking);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment