Reputation: 2528
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/
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
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