Created
June 13, 2018 18:51
-
-
Save AlexandreBonaventure/d765ea79b522c8bc3e49f0e060adb381 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Eventbus from '../launcher/services/eventbus' | |
// This comes from https://developers.google.com/web/tools/workbox/guides/advanced-recipes | |
// It allows activating the new service worker right away on reload | |
if ('serviceWorker' in navigator) { | |
window.addEventListener('load', function () { | |
navigator.serviceWorker.register('/sbvr-service-worker.js') | |
.then(function (registration) { | |
// Track updates to the Service Worker. | |
if (!navigator.serviceWorker.controller) { | |
// The window client isn't currently controlled so it's a new service | |
// worker that will activate immediately | |
return | |
} | |
// When the user asks to refresh the UI, we'll need to reload the window | |
var preventDevToolsReloadLoop | |
navigator.serviceWorker.addEventListener('controllerchange', function (event) { | |
// Ensure refresh is only called once. | |
// This works around a bug in "force update on reload". | |
if (preventDevToolsReloadLoop) return | |
preventDevToolsReloadLoop = true | |
window.location.reload() | |
}) | |
Eventbus.$on('sw:check-update', () => { | |
registration.update && registration.update() | |
}) | |
onNewServiceWorker(registration, function () { | |
showRefreshUI(registration) | |
}) | |
}) | |
}) | |
} | |
function showRefreshUI (registration) { | |
// var button = document.createElement('button') | |
// button.style.position = 'absolute' | |
// button.style.bottom = '24px' | |
// button.style.left = '24px' | |
// button.textContent = 'This site has updated. Please click here to see changes.' | |
// | |
// button.addEventListener('click', function () { | |
// if (!registration.waiting) { | |
// // Just to ensure registration.waiting is available before | |
// // calling postMessage() | |
// return | |
// } | |
// | |
// button.disabled = true | |
registration.waiting.postMessage('skipWaiting') | |
// }) | |
// document.body.appendChild(button) | |
}; | |
function onNewServiceWorker (registration, callback) { | |
if (registration.waiting) { | |
// SW is waiting to activate. Can occur if multiple clients open and | |
// one of the clients is refreshed. | |
return callback() | |
} | |
function listenInstalledStateChange () { | |
registration.installing.addEventListener('statechange', function (event) { | |
if (event.target.state === 'installed') { | |
// A new service worker is available, inform the user | |
callback() | |
} | |
}) | |
}; | |
if (registration.installing) { | |
return listenInstalledStateChange() | |
} | |
// We are currently controlled so a new SW may be found... | |
// Add a listener in case a new SW is found, | |
registration.addEventListener('updatefound', listenInstalledStateChange) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
How could I use your logic without using (import Eventbus from '../launcher/services/eventbus')?
Use native functions.