diff --git a/service-worker.js b/service-worker.js index 6271db7b8..ccdb049ff 100644 --- a/service-worker.js +++ b/service-worker.js @@ -3,7 +3,7 @@ * in order to capture the HTTP requests made by an article, and respond with the * corresponding content, coming from the archive * - * Copyright 2015 Mossroy and contributors + * Copyright 2022 Mossroy, Jaifroid and contributors * License GPL v3: * * This file is part of Kiwix. @@ -53,7 +53,15 @@ const APP_CACHE = 'kiwixjs-appCache-' + appVersion; * Caching is on by default but can be turned off by the user in Configuration * @type {Boolean} */ -var useCache = true; +var useAssetsCache = true; + +/** + * A global Boolean that governs whether the APP_CACHE will be used + * This is an expert setting in Configuration + * @type {Boolean} + */ + var useAppCache = true; + /** * A regular expression that matches the Content-Types of assets that may be stored in ASSETS_CACHE @@ -176,7 +184,10 @@ self.addEventListener('activate', function (event) { let outgoingMessagePort = null; let fetchCaptureEnabled = false; -self.addEventListener('fetch', function (event) { +/** + * Intercept selected Fetch requests from the browser window + */ + self.addEventListener('fetch', function (event) { // Only cache GET requests if (event.request.method !== "GET") return; // Remove any querystring before requesting from the cache @@ -207,20 +218,23 @@ self.addEventListener('fetch', function (event) { } 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(event.request).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(rqUrl) && !/\.zim\w{0,2}$/i.test(rqUrl)) { - event.waitUntil(updateCache(APP_CACHE, event.request, response.clone())); - } - return 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(rqUrl) && !/\.zim\w{0,2}$/i.test(rqUrl)) { + event.waitUntil(updateCache(APP_CACHE, event.request, response.clone())); + } + return response; }).catch(function (error) { - console.debug("[SW] Network request failed and no cache.", error); + console.debug("[SW] Network request failed and no cache.", error); }); } }) ); }); -self.addEventListener('message', function (event) { +/** + * Handle custom commands sent from app.js + */ + self.addEventListener('message', function (event) { if (event.data.action) { if (event.data.action === 'init') { // On 'init' message, we initialize the outgoingMessagePort and enable the fetchEventListener @@ -231,10 +245,18 @@ self.addEventListener('message', function (event) { outgoingMessagePort = null; fetchCaptureEnabled = false; } - if (event.data.action.useCache) { - // Turns caching on or off (a string value of 'on' turns it on, any other string turns it off) - useCache = event.data.action.useCache === 'on'; - console.debug('[SW] Caching was turned ' + event.data.action.useCache); + 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 }); @@ -328,7 +350,7 @@ function removeUrlParameters(url) { */ function fromCache(cache, requestUrl) { // Prevents use of Cache API if user has disabled it - if (!useCache && cache === ASSETS_CACHE) return Promise.reject('disabled'); + if (!useAppCache && cache === APP_CACHE || !useAssetsCache && cache === ASSETS_CACHE) return Promise.reject('disabled'); return caches.open(cache).then(function (cacheObj) { return cacheObj.match(requestUrl).then(function (matching) { if (!matching || matching.status === 404) { @@ -349,7 +371,7 @@ function fromCache(cache, requestUrl) { */ function updateCache(cache, request, response) { // Prevents use of Cache API if user has disabled it - if (!useCache && cache === ASSETS_CACHE) return Promise.resolve(); + if (!useAppCache && cache === APP_CACHE || !useAssetsCache && cache === ASSETS_CACHE) return Promise.resolve(); return caches.open(cache).then(function (cacheObj) { console.debug('[SW] Adding ' + request.url + ' to ' + cache + '...'); return cacheObj.put(request, response); @@ -364,7 +386,7 @@ function updateCache(cache, request, response) { */ function testCacheAndCountAssets(url) { if (regexpExcludedURLSchema.test(url)) return Promise.resolve(['custom', 'custom', 'Custom', '-']); - if (!useCache) return Promise.resolve(['none', 'none', 'None', 0]); + if (!useAssetsCache) return Promise.resolve(['none', 'none', 'None', 0]); return caches.open(ASSETS_CACHE).then(function (cache) { return cache.keys().then(function (keys) { return ['cacheAPI', ASSETS_CACHE, 'Cache API', keys.length]; diff --git a/www/css/app.css b/www/css/app.css index 7683f9a91..ba9a19b75 100644 --- a/www/css/app.css +++ b/www/css/app.css @@ -105,6 +105,10 @@ background: #171717; } +.dark .card-warning .card-header { + background-color: #FFFF00; +} + /* End of app theme: dark */ /* Content themes: _invert, _mwInvert */ @@ -222,6 +226,11 @@ button { margin: 2px; } +.btn-danger { + background-color: lightyellow; + color: darkred; +} + @keyframes spin { 0% { transform: rotate(0deg); } 100% { transform: rotate(360deg); } diff --git a/www/index.html b/www/index.html index daced0ce7..07f900f30 100644 --- a/www/index.html +++ b/www/index.html @@ -106,7 +106,7 @@
Offline archives use the OpenZIM format , - but note that this app has only been tested extensively on Wikipedia, WikiMedia and Stackexchage archives. Video content is in principle supported - if your device, browser or OS understands the format. Some other ZIMs use a proprietary dynamic UI which is only supported in ServiceWorker Mode + but note that this app has only been tested extensively on Wikipedia, WikiMedia, Gutenberg, Stackexchage and TED Talks archives. Video content is + supported if your device, browser or OS understands the format. Some ZIMs use a proprietary dynamic UI which is only supported in ServiceWorker Mode (see below).
@@ -266,7 +266,7 @@
If you need to store a large ZIM archive on an older filesystem formatted as FAT16 or FAT32, you need to be aware of the file size limits of those systems
- (FAT16 < 2GiB; FAT32 < 4GiB). Most modern microSD cards, thumb drives or hard drives are formatted as exFAT or another modern FS such as NTFS, which do not
+ (FAT16 < 2GiB; FAT32 < 4GiB). Most modern microSD cards, thumb drives or hard drives are formatted as exFAT or another modern FS such as NTFS, which do not
have this issue. If your ZIM archive is larger than the FS limit, it is possible to split the archive into several 2GiB-1 or 4GiB-1 files (or smaller).
You will need to give a file extension to each chunk in the right order following this pattern: *.zimaa
, *.zimab
, *.zimac
,
...
, etc.). When you pick this archive in the app, be sure to pick all the chunks, or drag-and-drop them all into the app.
@@ -280,7 +280,7 @@
Depending on your browser or framework, this app may be capable of running in two different modes, which we call - "JQuery Mode" and "ServiceWorker Mode" for short. There is a toggle under Expert Settings in Configuration. + "JQuery Mode" and "ServiceWorker Mode" for short. There is a toggle under Compatibility Settings in Configuration. Here is a technical explanation of what these modes do:
https:
, localhost
,
+ but it soon equals JQuery mode in speed, at least in modern browsers. However, older browsers such as IE11 are
+ incompatible with this mode, and the app must be running in a secure context (https:
, localhost
,
or certain browser extensions). While this mode is not natively supported in Mozilla (Firefox) browser
extensions, we provide a functional workaround by re-launching the extension as a Progressive Web App (PWA).
Note that this mode cannot run with the file:
protocol (but only IE11 and old Edge allow the app to run
@@ -439,14 +440,6 @@ See About (Expert Settings) for an explanation of the difference between these modes:
+See About (Technical Information) for an explanation of the difference between these modes:
Reset the app to default settings and erase all caches:
+