Skip to content

Instantly share code, notes, and snippets.

@btopro
Last active August 28, 2020 14:47
Show Gist options
  • Save btopro/5504dd72f8ff50ec2c71f9f7a0490abf to your computer and use it in GitHub Desktop.
Save btopro/5504dd72f8ff50ec2c71f9f7a0490abf to your computer and use it in GitHub Desktop.
Pretty standard service worker reloading methodology we use
if ('serviceWorker' in navigator) {
var sitePath = '/';
// discover this path downstream of the root of the domain
var swScope = window.location.pathname.substring(0, window.location.pathname.indexOf(sitePath)) + sitePath;
if (swScope != document.head.getElementsByTagName('base')[0].href) {
document.head.getElementsByTagName('base')[0].href = swScope;
}
window.addEventListener('load', function () {
navigator.serviceWorker.register('service-worker.js', {
scope: swScope
}).then(function (registration) {
registration.onupdatefound = function () {
// The updatefound event implies that registration.installing is set; see
// https://slightlyoff.github.io/ServiceWorker/spec/service_worker/index.html#service-worker-container-updatefound-event
var installingWorker = registration.installing;
installingWorker.onstatechange = function () {
switch (installingWorker.state) {
case 'installed':
if (!navigator.serviceWorker.controller) {
window.dispatchEvent(new CustomEvent('simple-toast-show', {
bubbles: true,
cancelable: false,
detail: {
text: 'Pages you view are cached for offline use.',
duration: 8000
}
}));
}
break;
case 'redundant':
throw Error('The installing service worker became redundant.');
break;
}
};
};
}).catch(function (e) {
console.warn('Service worker registration failed:', e);
});
// Check to see if the service worker controlling the page at initial load
// has become redundant, since this implies there's a new service worker with fresh content.
if (navigator.serviceWorker.controller) {
navigator.serviceWorker.controller.onstatechange = function(event) {
if (event.target.state === 'redundant') {
var b = document.createElement('paper-button');
b.appendChild(document.createTextNode('Reload'));
b.raised = true;
b.addEventListener('click', function(e){ window.location.reload(true); });
window.dispatchEvent(new CustomEvent('simple-toast-show', {
bubbles: true,
cancelable: false,
detail: {
text: 'A site update is available. Reload for latest content.',
duration: 12000,
slot: b,
clone: false
}
}));
}
};
}
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment