Reputation: 471
I have hard time understanding how service workers behave. I have an Angular progressive web application in the root directory of my domain. This folder contains all JavaScript files. Then there is a subfolder for each of my clients which contains the index.html file, the manifest.json and some images related to this client. Thus the index files points to JavaScript files in parent folder.
Now I want to have a service worker which caches the JavaScript files in the root and the index files in each subfolder.
For now I have put the service worker in the root but for every client it will cache all other clients files which I don't want to. And whenever I need to create a new subfolder I have to update the service worker in the root. Is there any way to have like multiple service workers, one for each sub folder? (I know it cannot fetches files in parent directory)
Upvotes: 1
Views: 1749
Reputation: 14866
Yes, you can register multiple service workers for different paths by specifying option scope
.
navigator.serviceWorker.register('/sw.js', {
scope: '/path/to/folder1'
}).then(function(registration) {
console.log('Service worker registration succeeded. This service worker will catch all http requests under /path/to/folder1');
});
navigator.serviceWorker.register('/sw.js', {
scope: '/path/to/folder2'
}).then(function(registration) {
console.log('Service worker registration succeeded. This service worker will catch all http requests under /path/to/folder2');
});
Related MDN article.
https://developer.mozilla.org/en-US/docs/Web/API/ServiceWorkerContainer/register
Upvotes: 2