I have Service Worker that load file from BrowserFS if the path contain __browserfs__
, simplified code like this:
function loadDependecies() {
self.skipWaiting().then(function() {
if (!self.fs) {
self.importScripts('https://cdn.jsdelivr.net/npm/browserfs');
BrowserFS.configure({ fs: 'IndexedDB', options: {} }, function (err) {
if (err) {
console.log(err);
} else {
self.fs = BrowserFS.BFSRequire('fs');
self.path = BrowserFS.BFSRequire('path');
}
});
}
});
}
self.addEventListener('install', loadDependecies);
self.addEventListener('activate', loadDependecies);
self.addEventListener('fetch', function (event) {
event.respondWith(new Promise(function(resolve, reject) {
if (local) {
console.log('serving ' + path + ' from browserfs');
if (!self.fs) {
(function loop() {
if (!self.fs) {
setTimeout(loop, 400);
} else {
serve();
}
})();
} else {
serve();
}
} else {
if (event.request.cache === 'only-if-cached' && event.request.mode !== 'same-origin') {
return;
}
//request = credentials: 'include'
fetch(event.request).then(resolve).catch(reject);
}
}));
});
and when I didn't interact with the app for a while, open it again and then try to fetch local file It was keep loading probably because of the infinite loop, I've needed to reload the service worker by hand (using dev tools) to get the page.
So my question is how can I properly call importScripts
to load BrowserFS in Service Worker?