Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

navigator.serviceWorker when multiple service workers are regisered

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.

like image 728
Thihara Avatar asked Jan 16 '17 05:01

Thihara


People also ask

When should a serviceWorker be used?

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.

Should I register service worker on every page?

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.

How do I find out if a service is already registered?

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.

Why Navigator serviceWorker is undefined?

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 .


1 Answers

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:

  • SW 1: /path/to/sw.js, registered with scope /path/to/
  • SW 2: /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.

like image 58
Jeff Posnick Avatar answered Sep 28 '22 07:09

Jeff Posnick