FEATURE: Only load service worker for Android

TBD an interface for plugin to enable service workers on desktop if they need them
This commit is contained in:
Sam 2018-02-13 16:23:36 +11:00
parent 074d590abd
commit 14d0450bef
1 changed files with 20 additions and 4 deletions

View File

@ -2,11 +2,27 @@ export default {
name: 'register-service-worker',
initialize() {
// only allow service worker on android for now
if (!/(android)/i.test(navigator.userAgent)) {
// remove old service worker
if ('serviceWorker' in navigator && navigator.serviceWorker.getRegistrations) {
navigator.serviceWorker.getRegistrations().then((registrations) => {
for(let registration of registrations) {
registration.unregister();
};
});
}
} else {
const isSecure = (document.location.protocol === 'https:') ||
(location.hostname === "localhost");
if (isSecure && ('serviceWorker' in navigator)) {
navigator.serviceWorker.register(`${Discourse.BaseUri}/service-worker.js`);
}
}
}
};