title: How to prevent the same service worker from registering over multiple pages?
body: <p>I have a service worker script that is registering repeatedly over multiple site levels.</p>
  
  <p>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.</p>
  
  <p>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. </p>
  
  <p>Does anyone have a way to prevent multiple registrations of the same script over multiple sub levels?</p>
  
  <p>EDIT:</p>
  
  <p>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.</p>
  
  <p>EDIT:</p>
  
  <p>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 '/'.</p>
  
  <p>The issue I want to solve is to prevent subsequent registers of the same service worker file.</p>
  
  <p>Localhost is a placeholder for the actual site.</p>
  
  <pre><code>navigator.serviceWorker.register('localhost/service-worker.js', {scope: './'})
  .then(initialiseState);
  </code></pre>
  
  <p>In Chrome dev tools for debugging, the resulting service workers are in the format like this:</p>
  
  <p>Scope: localhost/url/stuff/<br>
  Registration ID: 110<br>
  Active worker:<br>
  Installation Status: ACTIVATED<br>
  Running Status: STOPPED<br>
  Script: localhost/service-worker.js<br>
  ...</p>
  
  <p>Scope: localhost/<br>
  Registration ID: 111<br>
  Active worker:<br>
  Installation Status: ACTIVATED<br>
  Running Status: STOPPED<br>
  Script: localhost/service-worker.js<br>
  ...</p>
  
  <p>Scope: localhost/url<br>
  Registration ID: 112<br>
  Active worker:<br>
  Installation Status: ACTIVATED<br>
  Running Status: STOPPED<br>
  Script: localhost/service-worker.js<br>
  ...  </p>
  
  <p>Ideally I want to somehow just keep it to one entry somehow.</p>