Disable service worker initializer

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.

1 Like

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.

2 Likes

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.

1 Like

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.

3 Likes
       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();
            }
3 Likes

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.

2 Likes

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

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.

2 Likes

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

https://github.com/discourse/discourse/commit/abbc639e0d362ae90b52c8b5c6922d9e610777f8

@Jacky_Cheng Let me know if this fixes it for you :slight_smile:

3 Likes

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 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.

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

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

3 Likes

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

3 Likes

Just a FYI that I’ve submitted a PR to once again enable the service worker on Apple platforms so that push notifications start working as per my findings here.

2 Likes