Rajan Lagah
Rajan Lagah

Reputation: 2528

Service worker scope is not matching

I have service-worker.js in public folder.
When i register service worker i have to say

navigator.serviceWorker.register('./public/csw.js')

But the problem is now scope of service worker is set to http://localhost:2009/public and i want to catch fetch request for http://localhost:2009/


If i try to change the scope of service worker

navigator.serviceWorker.register('./public/csw.js',{scope:'/'})

That create error because service worker is called from file in other dir i.e root/src/index.js
I am not using CRA

Upvotes: 2

Views: 945

Answers (1)

dontcallmedom
dontcallmedom

Reputation: 2480

A Service Worker can only manage a scope below its own path for security reasons. You need to move csw.js to the root folder if you want it to handle all requests on the host. Alternatively, you could set an HTTP header Service-Worker-Allowed: / on the csw.js path (assuming your hosting allows you to do that)

Upvotes: 4

Related Questions