I have been checking out push notifications for the web and have run into the following snippet
navigator.serviceWorker
How does this resolve if there are multiple service workers? Does it return the last registered service worker?
The documentation isn't really clear to me on this one.
It acts like a proxy server exists between your app, the browser and the network. Among other things, service workers allow apps to continue functioning offline in case the user loses internet connection. Improve performance of the website: Service worker helps the website to load offline.
So, it is sufficient to only call navigator. serviceWorker. register() on your main entry point, and not call it again on any other pages, if you're sure that all users will pass through that entry point at least once. That being said, there's no harm in calling navigator.
You can look at Service Worker Detector, a Chrome extension that detects if a website registers a Service Worker by reading the navigator. serviceWorker. controller property. It might also work in other browsers supporting Web Extensions, but it looks like it is not yet distributed as such.
serviceWorker is undefined or navigator. serviceWorker. register is returning "Cannot read property 'register' of undefined" it's likely the issue is that the Service Worker is not running in a secure context. Service workers are required to run in a Secure Context (MDN Chromium), i.e. localhost and/or https .
Jake Archibald's "The Service Worker Lifecycle" is a fantastic resource, and if you haven't read it, I recommend doing so.
Each service worker has its own scope. By default, and at its most permissive, the scope is the same directory that the service worker script is served from. If needed, you can pass in a more restrictive scope when you call navigator.serviceWorker.register()
.
It's possible to register multiple service workers with different scopes for a given origin. (Aside: If you try to register multiple service workers with the same scope for a given origin, even if the service workers have different URLs, the subsequent registrations will be treated like service worker updates, not as separate registrations.)
If multiple service workers are registered, then in order to determine which one controls a give client page, the scopes are checked. The service worker whose registered scope is most specific, i.e. is the longest matching scope, is the one which will control a given client page.
So, for example, given the following two service worker registrations with corresponding scopes:
/path/to/sw.js
, registered with scope /path/to/
/path/to/subdir/sw.js
, registered with scope /path/to/subdir/
If the client page's URL is /path/to/index.html
, then SW 1 will control the client, since /path/to/
is the longest matching scope.
If the client page's URL is /path/to/subdir/index.html
, then SW 2 will control the client. Even though SW 1's scope matches, SW 2's scope matches as well, and SW 2's scope is more specific/longer.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With