# Disable service worker initializer

**URL:** https://meta.discourse.org/t/disable-service-worker-initializer/116392
**Category:** Development
**Created:** [April 30, 2019, 3:59am UTC](https://meta.discourse.org/t/disable-service-worker-initializer/116392 "2019-04-30T03:59:31Z")
**Posts on this page:** 13
**Page:** 1

<div class="post-metadata">

### Author: ![Jacky\_Cheng](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/jacky_cheng/32/139694_2.png) [@Jacky\_Cheng](https://meta.discourse.org/u/Jacky_Cheng)
#### Post date: [April 30, 2019, 3:59am UTC](https://meta.discourse.org/t/disable-service-worker-initializer/116392/1 "2019-04-30T03:59:31Z")

</div>

Is there any way to disable service worker initializer ?  
Because my web served at `example.com` with it’s own registered service worker, I embed discourse forum which under the same domain `example.com/forum` for SSO.  
Discourse forum would unregister all my parent page service worker.

---

<div class="post-metadata">

### Author: ![sam](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/sam/32/102149_2.png) [@sam](https://meta.discourse.org/u/sam)
#### Post date: [April 30, 2019, 4:04am UTC](https://meta.discourse.org/t/disable-service-worker-initializer/116392/2 "2019-04-30T04:04:41Z")

</div>

I don’t think we have a “disable service worker” switch, you may be able to swing something in a theme component, it makes sense to have a switch or at least a white list of service worker js files that we never de-register for the sub folder case.

---

<div class="post-metadata">

### Author: ![Jacky\_Cheng](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/jacky_cheng/32/139694_2.png) [@Jacky\_Cheng](https://meta.discourse.org/u/Jacky_Cheng)
#### Post date: [April 30, 2019, 4:06am UTC](https://meta.discourse.org/t/disable-service-worker-initializer/116392/3 "2019-04-30T04:06:15Z")

</div>

Thanks for your reply.  
In my opinion, it would be better to register and unregister service worker under the base-url scope, not the whole domain.

---

<div class="post-metadata">

### Author: ![sam](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/sam/32/102149_2.png) [@sam](https://meta.discourse.org/u/sam)
#### Post date: [April 30, 2019, 4:09am UTC](https://meta.discourse.org/t/disable-service-worker-initializer/116392/4 "2019-04-30T04:09:42Z")

</div>

Correction, we only de-register stuff that we know is ours:

> <https://github.com/discourse/discourse/blob/2863abefdda8ca6fbef244761e89130013f48d15/app/assets/javascripts/discourse/initializers/register-service-worker.js.es6#L17-L24>

One spinner is that we de-register apple cause it is buggy at the moment:

> <https://github.com/discourse/discourse/blob/2863abefdda8ca6fbef244761e89130013f48d15/app/assets/javascripts/discourse/initializers/register-service-worker.js.es6#L12-L12>

As to using scope here, I think it makes sense to make the change to Discourse provided scope is well supported.

---

<div class="post-metadata">

### Author: ![Jacky\_Cheng](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/jacky_cheng/32/139694_2.png) [@Jacky\_Cheng](https://meta.discourse.org/u/Jacky_Cheng)
#### Post date: [April 30, 2019, 4:20am UTC](https://meta.discourse.org/t/disable-service-worker-initializer/116392/5 "2019-04-30T04:20:50Z")

</div>

```
       if (
          registration.active &&
          !registration.active.scriptURL.includes(
            Discourse.ServiceWorkerURL
          )
        ) {
          registration.unregister();
        }

```

shouldn’t this if condition to be this one to only unregister Discourse ServiceWorker ?

```
           if (
              registration.active &&
              registration.active.scriptURL.includes(
                Discourse.ServiceWorkerURL
              )
            ) {
              registration.unregister();
            }

```

---

<div class="post-metadata">

### Author: ![sam](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/sam/32/102149_2.png) [@sam](https://meta.discourse.org/u/sam)
#### Post date: [April 30, 2019, 5:03am UTC](https://meta.discourse.org/t/disable-service-worker-initializer/116392/6 "2019-04-30T05:03:49Z")

</div>

Yeah reading through this we are being mega aggressive with de-registrations due to historic moves of our url.

@tgxworld any thoughts here? should we be more accurate here.

---

<div class="post-metadata">

### Author: ![Jacky\_Cheng](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/jacky_cheng/32/139694_2.png) [@Jacky\_Cheng](https://meta.discourse.org/u/Jacky_Cheng)
#### Post date: [May 2, 2019, 9:34am UTC](https://meta.discourse.org/t/disable-service-worker-initializer/116392/7 "2019-05-02T09:34:05Z")

</div>

Could you provide some workaround now ?  
I saw some tutorials about plugins?  
Can I write a plugin to overwrite the initialize/js file ?

---

<div class="post-metadata">

### Author: ![sam](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/sam/32/102149_2.png) [@sam](https://meta.discourse.org/u/sam)
#### Post date: [May 3, 2019, 12:28am UTC](https://meta.discourse.org/t/disable-service-worker-initializer/116392/8 "2019-05-03T00:28:08Z")

</div>

We need to fix the file so we only de-register our stuff, maybe look for a specific string.

@tgxworld can you add this to your list, should be an easy change.

---

<div class="post-metadata">

### Author: ![tgxworld](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/tgxworld/32/106117_2.png) [@tgxworld](https://meta.discourse.org/u/tgxworld)
#### Post date: [May 7, 2019, 5:12am UTC](https://meta.discourse.org/t/disable-service-worker-initializer/116392/10 "2019-05-07T05:12:53Z")

</div>

Looks like registration didn’t consider subfolder support as well. Fixed in

[https://github.com/discourse/discourse/commit/abbc639e0d362ae90b52c8b5c6922d9e610777f8](https://github.com/discourse/discourse/commit/abbc639e0d362ae90b52c8b5c6922d9e610777f8)

@Jacky_Cheng Let me know if this fixes it for you 🙂

---

<div class="post-metadata">

### Author: ![Jacky\_Cheng](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/jacky_cheng/32/139694_2.png) [@Jacky\_Cheng](https://meta.discourse.org/u/Jacky_Cheng)
#### Post date: [May 7, 2019, 6:02am UTC](https://meta.discourse.org/t/disable-service-worker-initializer/116392/11 "2019-05-07T06:02:46Z")

</div>

Thanks for your reply.  
I forgot to bring up that Safari now can support service-worker.  
So  
`const isApple = !!navigator.platform.match(/(Mac|iPhone|iPod|iPad)/i);`  
should be remove Mac condition in my opinion.  
Also in [discourse/app/assets/javascripts/discourse/initializers/register-service-worker.js.es6 at abbc639e0d362ae90b52c8b5c6922d9e610777f8 · discourse/discourse · GitHub](https://github.com/discourse/discourse/tree/abbc639e0d362ae90b52c8b5c6922d9e610777f8/app/assets/javascripts/discourse/initializers/register-service-worker.js.es6) the else condition would also unregister service worker, it should add the url condition.

---

<div class="post-metadata">

### Author: ![tgxworld](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/tgxworld/32/106117_2.png) [@tgxworld](https://meta.discourse.org/u/tgxworld)
#### Post date: [May 7, 2019, 6:14am UTC](https://meta.discourse.org/t/disable-service-worker-initializer/116392/12 "2019-05-07T06:14:11Z")

</div>

> [@Jacky\_Cheng](#):
>
> I forgot to bring up that Safari now can support service-worker.

Yup we know but Discourse is not ready to support Service workers on Safari yet because it has been problematic for us so far.

> [@Jacky\_Cheng](#):
>
> the else condition would also unregister service worker, it should add the url condition.

It does consider the URL condition because I extracted both of the unregister function calls into a common function.

---

<div class="post-metadata">

### Author: ![Jacky\_Cheng](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/jacky_cheng/32/139694_2.png) [@Jacky\_Cheng](https://meta.discourse.org/u/Jacky_Cheng)
#### Post date: [May 7, 2019, 6:20am UTC](https://meta.discourse.org/t/disable-service-worker-initializer/116392/13 "2019-05-07T06:20:58Z")

</div>

> [@tgxworld](#):
>
> It does consider the URL condition because I extracted both of the unregister function calls into a common function.

Oh I see.  
I think this would fix my problem.  
Thanks a lot.

---

<div class="post-metadata">

### Author: ![robbiet480](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/robbiet480/32/121789_2.png) [@robbiet480](https://meta.discourse.org/u/robbiet480)
#### Post date: [June 22, 2019, 5:32am UTC](https://meta.discourse.org/t/disable-service-worker-initializer/116392/14 "2019-06-22T05:32:28Z")

</div>

Just a FYI that I’ve [submitted a PR](https://github.com/discourse/discourse/pull/7792) to once again enable the service worker on Apple platforms so that push notifications start working as per my findings [here](https://meta.discourse.org/t/discourse-push-notifications-for-desktop/86941/52).
