From ee34151b9fb0e6344c99dd8896df82659ce98be0 Mon Sep 17 00:00:00 2001 From: Jaifroid Date: Fri, 20 Oct 2023 14:28:22 +0100 Subject: [PATCH] Some fixes --- service-worker.js | 113 ++++++++++++++++++++++------------------------ www/js/app.js | 4 +- 2 files changed, 57 insertions(+), 60 deletions(-) diff --git a/service-worker.js b/service-worker.js index 8e1df4de4..c44d35717 100644 --- a/service-worker.js +++ b/service-worker.js @@ -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', @@ -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 diff --git a/www/js/app.js b/www/js/app.js index 4766994ad..1d3cc6597 100644 --- a/www/js/app.js +++ b/www/js/app.js @@ -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 {