I have a service worker script that is registering repeatedly over multiple site levels.
In other words the same service worker is registered for www.site.ca/, www.site.ca/text-text, www.site.ca/example-example, and etc.
The site is built on php with different urls being produced depending on the content so kind of API-like. The reason that the service worker registration is on those pages is because most of the site traffic lands on those pages rather than the home page. The result being the same service worker being registered with different ids over different pages.
Does anyone have a way to prevent multiple registrations of the same script over multiple sub levels?
EDIT:
The purpose of the service worker is to set up notifications even if the user isn't on the site. Also the issue I'm having would negatively affect any attempts to update the service worker in the future since there would be multiple copies to update. The result being that only one copy would ever be updated when the user lands in the site again on a specific page. There would be no guarantee that all the copies of that service worker file would be updated. Also the result of the multiple copies has resulted in users getting stacks of notifications from the site so it is a problem.
EDIT:
The below is what I have so far and to my understanding scope defines where it gets registered. However the problem is that it needs to be able to register wherever a person enters the site thus the scope was set to '/'.
The issue I want to solve is to prevent subsequent registers of the same service worker file.
Localhost is a placeholder for the actual site.
navigator.serviceWorker.register('localhost/service-worker.js', {scope: './'})
.then(initialiseState);
In Chrome dev tools for debugging, the resulting service workers are in the format like this:
Scope: localhost/url/stuff/
Registration ID: 110
Active worker:
Installation Status: ACTIVATED
Running Status: STOPPED
Script: localhost/service-worker.js
...
Scope: localhost/
Registration ID: 111
Active worker:
Installation Status: ACTIVATED
Running Status: STOPPED
Script: localhost/service-worker.js
...
Scope: localhost/url
Registration ID: 112
Active worker:
Installation Status: ACTIVATED
Running Status: STOPPED
Script: localhost/service-worker.js
...
Ideally I want to somehow just keep it to one entry somehow.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…