From 7d1420257e9d5172d27562ee1decf24a34da711f Mon Sep 17 00:00:00 2001 From: Jaifroid Date: Mon, 11 Jan 2021 20:56:50 +0000 Subject: [PATCH] Prevent full disabling of Service Worker in PWA context Former-commit-id: e1287fcc5fe2a70eac9e032b225c5d05e71ff601 [formerly 5b5fc90d2c9aca13adde71727766845179a1fb0e [formerly 64404cb1254695c9e824123186924c69a5fa1d0f]] Former-commit-id: b4a68c0ecc37d119d1686e18602c0297a136114a Former-commit-id: ff1da3af395b43d85cace4b967ff1d0f3192fede --- www/js/app.js | 36 +++++++++++++++++++++--------------- 1 file changed, 21 insertions(+), 15 deletions(-) diff --git a/www/js/app.js b/www/js/app.js index b6233869..a10b5e51 100644 --- a/www/js/app.js +++ b/www/js/app.js @@ -1435,22 +1435,28 @@ define(['jquery', 'zimArchiveLoader', 'uiUtil', 'util', 'cache', 'images', 'sett function setContentInjectionMode(value) { params.contentInjectionMode = value; if (value === 'jquery') { - if (isServiceWorkerReady()) { - // We need to disable the ServiceWorker - // Unregistering it does not seem to work as expected : the ServiceWorker - // is indeed unregistered but still active... - // So we have to disable it manually (even if it's still registered and active) - navigator.serviceWorker.controller.postMessage({ - 'action': 'disable' - }); - messageChannel = null; - // If we're in electron or nwjs, completely remove the SW (but we need to keep it active if we're in a PWA) - if (typeof window.fs !== 'undefined') { - navigator.serviceWorker.getRegistrations().then(function (registrations) { - registrations.forEach(function (registration) { - registration.unregister(); - }); + // Because the "outer" Service Worker still runs in a PWA app, we don't actually disable the SW in this context, but it will no longer + // be intercepting requests + if ('serviceWorker' in navigator && /\/\/(localhost|kiwix.github.io)\//i.test(window.location.href)) { + serviceWorkerRegistration = null; + } else { + if (isServiceWorkerReady()) { + // We need to disable the ServiceWorker + // Unregistering it does not seem to work as expected : the ServiceWorker + // is indeed unregistered but still active... + // So we have to disable it manually (even if it's still registered and active) + navigator.serviceWorker.controller.postMessage({ + 'action': 'disable' }); + messageChannel = null; + // If we're in electron, completely remove the SW (but we need to keep it active if we're in a PWA) + if (typeof window.fs !== 'undefined') { + navigator.serviceWorker.getRegistrations().then(function (registrations) { + registrations.forEach(function (registration) { + registration.unregister(); + }); + }); + } } } refreshAPIStatus();