Nickersoft / push-fcm-plugin

Official Firebase Cloud Messaging plugin for Push.js v1.0 :fire:
https://pushjs.org
42 stars 13 forks source link

Register a second SW on the same scope #2

Closed ThomasPerraudin closed 6 years ago

ThomasPerraudin commented 6 years ago

I already have a service worker on my application, registered on the default scope "./" It doesn't work with firebase-messaging-sw.js certainly because it is registered on the same scope. Do you have a solution to register both service workers ?

Nickersoft commented 6 years ago

I actually wasn't aware you couldn't have multiple service workers registered in the same scope, though I assume that makes sense. You can change the location of the Firebase service worker script using:

Push.config({
  FCM: {
    serviceWorkerLocation: './yourLocation'
  }
});

If this helps. That way the scope of the Firebase service worker is limited to a subdirectory. Outside of that, it becomes more of a general service worker registration issue than an issue with this plugin.

ThomasPerraudin commented 6 years ago

I agree it's a general issue with service workers. I think it happens when both service workers try to work with the same event listeners.

Changing the serviceWorkerLocation triggers an error :-(

The path of the provided scope ('/') is not under the max scope allowed ('/scripts/'). Adjust the scope, move the Service Worker script, or use the Service-Worker-Allowed HTTP header to allow the scope.

Nickersoft commented 6 years ago

So according to MDN:

The max scope for a service worker is the location of the worker

which sounds related to your issue. Could you describe your service worker setup in more detail / provide some code? Are you installing this plugin via NPM?

ThomasPerraudin commented 6 years ago

I can provide you the full code : ccm.zip You will find the application service worker at the bottom of app.js

Nickersoft commented 6 years ago

Hmm.. unfortunately, it does seem to boil down to the issue of overlapping ServiceWorker scopes. In fact, it seems there is an active discussion going on over on the W3 ServiceWorker spec repo about this very thing (see here and here). The best advice I can give, despite it being untested, is creating a single ServiceWorker called firebase-messaging-sw.js that uses the importScripts() method to import the two ServiceWorkers. You would just have to check to make sure you're not registering it twice. Sorry I can't be of more assistance.. unfortunately, I believe it's one of the limitations of ServiceWorkers. I'd suggest that you alter the scope, but then you wouldn't be able to register notifications from the homepage and whatnot.

ThomasPerraudin commented 6 years ago

I've also seen that discution in the W3 repo. And I've tried to use importScripts but it doesn't fix the problem. I hope the next generation of service workers will be far better, I'm sad to discover these big issues.