'Service Worker - excluding path
Is there a possibility to exclude some paths in service worker?
For example, I have my application: www.application.com and an admin panel www.application.com/superadmin
I want to disable SW in /superadmin/* path.
I saw that there is a scope option, but in my case, it's must be /
https://developer.mozilla.org/en-US/docs/Web/Manifest/serviceworker
Maybe you have any ideas or experience on how to solve this problem?
Solution 1:[1]
There is no way to fully disable a serviceworker in that situation (although you can always have the service worker ignore the fetch requests for a subpath) - there is a proposal to enable more granular scoping of serviceworkers in the future though: https://github.com/wanderview/service-worker-scope-pattern-matching/blob/master/explainer.md
Solution 2:[2]
There is a way. We can add an array list of items to exclude, and add a search into the fetch event listener.
Include and exclude method below for completeness.
var offlineInclude = [
'', // index.html
'site.css',
'js/sitejs.js'
];
var offlineExclude = [
'/bigimg.png', //exclude a file
'/networkimages/smallimg.png', //exclude a file in a specific directory
'/admin/' //exclude a directory
];
self.addEventListener("install", function(event) {
console.log('WORKER: install event in progress.');
event.waitUntil(
caches
.open(version + 'fundamentals')
.then(function(cache) {
return cache.addAll(offlineInclude);
})
.then(function() {
console.log('WORKER: install completed');
})
);
});
self.addEventListener("fetch", function(event) {
console.log('WORKER: fetch event in progress.');
if (event.request.method !== 'GET') {
console.log('WORKER: fetch event ignored.', event.request.method, event.request.url);
return;
}
for (let i = 0; i < offlineExclude.length; i++)
{
if (event.request.url.indexOf(offlineExclude[i]) !== -1)
{
console.log('WORKER: fetch event ignored. URL in exclude list.', event.request.url);
return false;
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | dontcallmedom |
| Solution 2 |
