Skip to content

Commit

Permalink
Some fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
Jaifroid committed Oct 20, 2023
1 parent 34145d1 commit ee34151
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 60 deletions.
113 changes: 55 additions & 58 deletions service-worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ const precacheFiles = [
'www/js/lib/load.js',
'www/js/lib/wabac.js',
'www/js/lib/wombat.js',
'www/topFrame.html'
'www/topFrame.html',
'node_modules/bootstrap/dist/js/bootstrap.bundle.min.js',
'node_modules/bootstrap/dist/js/bootstrap.bundle.min.js.map',
'node_modules/bootstrap/dist/css/bootstrap.min.css',
Expand Down Expand Up @@ -292,65 +292,62 @@ function handleZIMFetchEvent (event) {
// especially .js assets, where it may be significant). Anchor targets are irreleveant in this context.
if (cache === APP_CACHE) rqUrl = strippedUrl;
// event.respondWith(
// First see if the content is in the cache
return fromCache(cache, rqUrl).then(function (response) {
// The response was found in the cache so we respond with it
return response;
}, function () {
// The response was not found in the cache so we look for it in the ZIM
// and add it to the cache if it is an asset type (css or js)
if (cache === ASSETS_CACHE && regexpZIMUrlWithNamespace.test(strippedUrl)) {
const range = event.request.headers.get('range');
return fetchUrlFromZIM(urlObject, range).then(function (response) {
// Add css or js assets to ASSETS_CACHE (or update their cache entries) unless the URL schema is not supported
if (regexpCachedContentTypes.test(response.headers.get('Content-Type')) &&
!regexpExcludedURLSchema.test(event.request.url)) {
event.waitUntil(updateCache(ASSETS_CACHE, rqUrl, response.clone()));
}
return response;
}).catch(function (msgPortData) {
console.error('Invalid message received from app.js for ' + strippedUrl, msgPortData);
return msgPortData;
});
} else {
// It's not an asset, or it doesn't match a ZIM URL pattern, so we should fetch it with Fetch API
return fetch(rqUrl).then(function (response) {
// If request was successful, add or update it in the cache, but be careful not to cache the ZIM archive itself!
if (!regexpExcludedURLSchema.test(event.request.url) && !/\.zim\w{0,2}$/i.test(strippedUrl)) {
event.waitUntil(updateCache(APP_CACHE, rqUrl, response.clone()));
}
return response;
}).catch(function (error) {
console.debug('[SW] Network request failed and no cache.', error);
});
}
})
// );
}
var oldValue;
if (event.data.action.assetsCache) {
// Turns caching on or off (a string value of 'enable' turns it on, any other string turns it off)
oldValue = useAssetsCache;
useAssetsCache = event.data.action.assetsCache === 'enable';
if (useAssetsCache !== oldValue) console.debug('[SW] Use of assetsCache was switched to: ' + useAssetsCache);
}
if (event.data.action.appCache) {
// Enables or disables use of appCache
oldValue = useAppCache;
useAppCache = event.data.action.appCache === 'enable';
if (useAppCache !== oldValue) console.debug('[SW] Use of appCache was switched to: ' + useAppCache);
}
if (event.data.action === 'getCacheNames') {
event.ports[0].postMessage({ app: APP_CACHE, assets: ASSETS_CACHE });
}
if (event.data.action.checkCache) {
// Checks and returns the caching strategy: checkCache key should contain a sample URL string to test
testCacheAndCountAssets(event.data.action.checkCache).then(function (cacheArr) {
event.ports[0].postMessage({ type: cacheArr[0], name: cacheArr[1], description: cacheArr[2], count: cacheArr[3] });
// First see if the content is in the cache
return fromCache(cache, rqUrl).then(function (response) {
// The response was found in the cache so we respond with it
return response;
}).catch(function () {
// The response was not found in the cache so we look for it in the ZIM
// and add it to the cache if it is an asset type (css or js)
if (cache === ASSETS_CACHE && regexpZIMUrlWithNamespace.test(strippedUrl)) {
const range = event.request.headers.get('range');
return fetchUrlFromZIM(urlObject, range).then(function (response) {
// Add css or js assets to ASSETS_CACHE (or update their cache entries) unless the URL schema is not supported
if (regexpCachedContentTypes.test(response.headers.get('Content-Type')) &&
!regexpExcludedURLSchema.test(event.request.url)) {
event.waitUntil(updateCache(ASSETS_CACHE, rqUrl, response.clone()));
}
return response;
}).catch(function (msgPortData) {
console.error('Invalid message received from app.js for ' + strippedUrl, msgPortData);
return msgPortData;
});
} else {
// It's not an asset, or it doesn't match a ZIM URL pattern, so we should fetch it with Fetch API
return fetch(rqUrl).then(function (response) {
// If request was successful, add or update it in the cache, but be careful not to cache the ZIM archive itself!
if (!regexpExcludedURLSchema.test(event.request.url) && !/\.zim\w{0,2}$/i.test(strippedUrl)) {
event.waitUntil(updateCache(APP_CACHE, rqUrl, response.clone()));
}
return response;
}).catch(function (error) {
console.debug('[SW] Network request failed and no cache.', error);
});
}
}
});
// var oldValue;
// if (event.data.action.assetsCache) {
// // Turns caching on or off (a string value of 'enable' turns it on, any other string turns it off)
// oldValue = useAssetsCache;
// useAssetsCache = event.data.action.assetsCache === 'enable';
// if (useAssetsCache !== oldValue) console.debug('[SW] Use of assetsCache was switched to: ' + useAssetsCache);
// }
// if (event.data.action.appCache) {
// // Enables or disables use of appCache
// oldValue = useAppCache;
// useAppCache = event.data.action.appCache === 'enable';
// if (useAppCache !== oldValue) console.debug('[SW] Use of appCache was switched to: ' + useAppCache);
// }
// if (event.data.action === 'getCacheNames') {
// event.ports[0].postMessage({ app: APP_CACHE, assets: ASSETS_CACHE });
// }
// if (event.data.action.checkCache) {
// // Checks and returns the caching strategy: checkCache key should contain a sample URL string to test
// testCacheAndCountAssets(event.data.action.checkCache).then(function (cacheArr) {
// event.ports[0].postMessage({ type: cacheArr[0], name: cacheArr[1], description: cacheArr[2], count: cacheArr[3] });
// });
// }
})
}

/**
* Handles URLs that need to be extracted from the ZIM archive
Expand Down
4 changes: 2 additions & 2 deletions www/js/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -1673,12 +1673,12 @@ function archiveReadyCallback (archive) {
}
// Add the correct prefixes so that the wabac Service Worker can recognize and transform the iframe document when loaded
var zimitPrefix = '';
if (selectedArchive._file.zimType === 'zimit' && /[CA]/.test(dirEntry.namespace)) {
if (selectedArchive.zimType === 'zimit' && /[CA]/.test(dirEntry.namespace)) {
zimitPrefix = 'mp_/';
encodedUrl = 'https://' + encodeURI(dirEntry.url);
}
// We put the ZIM filename as a prefix in the URL, so that browser caches are separate for each ZIM file
iframeArticleContent.src = '../' + selectedArchive._file.name + '/' + dirEntry.namespace + '/' + zimitPrefix + encodedUrl;
iframeArticleContent.src = '../' + selectedArchive.file.name + '/' + dirEntry.namespace + '/' + zimitPrefix + encodedUrl;
// Wombat = new window._WBWombat(iframeArticleContent.contentWindow, {});
// Wombat.initIframeWombat(iframeArticleContent);
} else {
Expand Down

0 comments on commit ee34151

Please sign in to comment.