Skip to content

Instantly share code, notes, and snippets.

@joshwiens
Forked from developit/sw-update-hook.js
Created November 2, 2017 02:07
Show Gist options
  • Save joshwiens/328f2abf47f2e244f7457b0c01daa4d1 to your computer and use it in GitHub Desktop.
Save joshwiens/328f2abf47f2e244f7457b0c01daa4d1 to your computer and use it in GitHub Desktop.
if (typeof window!=='undefined' && navigator.serviceWorker && navigator.serviceWorker.controller) {
let reloadOnNext = false;
let pushState = history.pushState;
history.pushState = function(state, title, url) {
if (reloadOnNext===true) location.href = url;
else pushState.call(this, state, title, url);
};
navigator.serviceWorker.controller.addEventListener('statechange', e => {
if (e.target.state==='redundant') {
reloadOnNext = true;
}
});
setInterval( () => {
navigator.serviceWorker.ready.then( reg => reg.update() );
}, 3e4);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment