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..1e690a829dd 100644 --- a/app/assets/javascripts/discourse/initializers/register-service-worker.js.es6 +++ b/app/assets/javascripts/discourse/initializers/register-service-worker.js.es6 @@ -1,12 +1,44 @@ +function deRegister() { + if (navigator.serviceWorker.getRegistrations) { + navigator.serviceWorker.getRegistrations().then(r => { + r.forEach(reg => { + if (reg.active && reg.active.scriptURL.indexOf('service-worker.js') > 0) { + reg.unregister(); + } + }); + }); + } +} + export default { name: 'register-service-worker', - initialize() { + initialize(container) { + const siteSettings = container.lookup('site-settings:main'); const isSecure = (document.location.protocol === 'https:') || (location.hostname === "localhost"); if (isSecure && ('serviceWorker' in navigator)) { - navigator.serviceWorker.register(`${Discourse.BaseUri}/service-worker.js`); + + let agents = siteSettings.service_worker_user_agents.split('|'); + + let allowed = false; + + if (agents[0].length > 0) { + const userAgent = (navigator.userAgent || "unknown").toLowerCase(); + for(let i=0; i