From 14d0450bef79a63adba0da65cf422f0d5c1f4ef0 Mon Sep 17 00:00:00 2001 From: Sam Date: Tue, 13 Feb 2018 16:23:36 +1100 Subject: [PATCH] FEATURE: Only load service worker for Android TBD an interface for plugin to enable service workers on desktop if they need them --- .../register-service-worker.js.es6 | 24 +++++++++++++++---- 1 file changed, 20 insertions(+), 4 deletions(-) diff --git a/app/assets/javascripts/discourse/initializers/register-service-worker.js.es6 b/app/assets/javascripts/discourse/initializers/register-service-worker.js.es6 index 7c16106ddae..281d44554b3 100644 --- a/app/assets/javascripts/discourse/initializers/register-service-worker.js.es6 +++ b/app/assets/javascripts/discourse/initializers/register-service-worker.js.es6 @@ -2,11 +2,27 @@ export default { name: 'register-service-worker', initialize() { - const isSecure = (document.location.protocol === 'https:') || - (location.hostname === "localhost"); + // only allow service worker on android for now + if (!/(android)/i.test(navigator.userAgent)) { - if (isSecure && ('serviceWorker' in navigator)) { - navigator.serviceWorker.register(`${Discourse.BaseUri}/service-worker.js`); + // 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`); + } } } };