Skip to content

Instantly share code, notes, and snippets.

@mrenty
Forked from developit/sw-update-hook.js
Created November 2, 2017 06:40
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save mrenty/81d5450edea085f979f46d50699f341f 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