Delete deprecated idxDB databases if possible

Former-commit-id: 1f27164b9fb5f9674a081c6ea1f783b6f79a41fc [formerly 1fe9a54a409fd9c7fce2eff03aac496dfb2846ed [formerly 617fac3bd55b852f18d0b0135e29ebf1b3dbafe4]]
Former-commit-id: 31426d4308472b82aa9fa9aee836fec0a2ace6c6
Former-commit-id: b1957276bedcd17227f16c1422ca4c87c0b328fd
This commit is contained in:
Jaifroid 2021-01-14 11:07:56 +00:00
parent 72f85483e4
commit 5eb3b04a13
5 changed files with 44 additions and 14 deletions

View File

@ -4,7 +4,7 @@
// App version number - ENSURE IT MATCHES VALUE IN init.js // App version number - ENSURE IT MATCHES VALUE IN init.js
// DEV: Changing this will cause the browser to recognize that the Service Worker has changed, and it will download and // DEV: Changing this will cause the browser to recognize that the Service Worker has changed, and it will download and
// install a new copy // install a new copy
const appVersion = '1.1.4-RP5'; const appVersion = '1.1.4-RP6';
// Kiwix ZIM Archive Download Server in regex form // Kiwix ZIM Archive Download Server in regex form
// DEV: The server URL is defined in init.js, but is not available to us in SW // DEV: The server URL is defined in init.js, but is not available to us in SW

View File

@ -529,7 +529,7 @@
<table> <table>
<tr> <tr>
<td><input style="float:left; margin-right:1em;" type="button" class="btn btn-primary" value="Select storage" id="btnRescanDeviceStorage" /></td> <td><input style="float:left; margin-right:1em;" type="button" class="btn btn-primary" value="Select storage" id="btnRescanDeviceStorage" /></td>
<td style="width:100%;">Tap to select app's local storage or to select a file or folder</td> <td style="width:100%;">Tap to select app's local storage or to select a file or folder. <b>You can also drag and drop a ZIM file into the app.</b></td>
</tr> </tr>
</table> </table>
</div> </div>

View File

@ -1368,12 +1368,20 @@ define(['jquery', 'zimArchiveLoader', 'uiUtil', 'util', 'cache', 'images', 'sett
//Code below triggers display of modal info box if app is run for the first time, or it has been upgraded to new version //Code below triggers display of modal info box if app is run for the first time, or it has been upgraded to new version
if (settingsStore.getItem('version') !== params.version) { if (settingsStore.getItem('version') !== params.version) {
firstRun = true; firstRun = true;
// Make user re-pick file if we have upgraded (it's less confusing than filehandle errors)
settingsStore.removeItem('listOfArchives'); // Make user re-pick file if we have upgraded (it's less confusing than filehandle errors) - NOT NEEDED, remove in due course
// settingsStore.removeItem('listOfArchives');
// Update the installed version
if (params.PWAInstalled) { if (params.PWAInstalled) {
params.PWAInstalled = params.version; params.PWAInstalled = params.version;
settingsStore.setItem('PWAInstalled', params.PWAInstalled); settingsStore.setItem('PWAInstalled', params.PWAInstalled);
} }
// One-time cleanup of idxDB files to delete deprecated databases if possible
cache.idxDB('deleteNonCurrent', function (result) {
if (result === false) console.log('Unable to delete old idxDB databases (this is normal in non-Chromium browsers');
else console.log('Deleted ' + result + ' deprecated database(s).');
});
// On some platforms, bootstrap's jQuery functions have not been injected yet, so we have to run in a timeout // On some platforms, bootstrap's jQuery functions have not been injected yet, so we have to run in a timeout
setTimeout(function () { setTimeout(function () {
$('#myModal').modal({ $('#myModal').modal({

View File

@ -49,7 +49,7 @@ var params = {};
*/ */
var appstate = {}; var appstate = {};
/******** UPDATE VERSION IN pwabuilder-sw.js TO MATCH VERSION *******/ /******** UPDATE VERSION IN pwabuilder-sw.js TO MATCH VERSION *******/
params['version'] = "1.1.4-RP5"; //DEV: Manually update this version when there is a new release: it is compared to the Settings Store "version" in order to show first-time info, and the cookie is updated in app.js params['version'] = "1.1.4-RP6"; //DEV: Manually update this version when there is a new release: it is compared to the Settings Store "version" in order to show first-time info, and the cookie is updated in app.js
/******* UPDATE THIS ^^^^^^ IN serveice worker!! ********************/ /******* UPDATE THIS ^^^^^^ IN serveice worker!! ********************/
params['packagedFile'] = "wikipedia_en_100_maxi.zim"; //For packaged Kiwix JS (e.g. with Wikivoyage file), set this to the filename (for split files, give the first chunk *.zimaa) and place file(s) in default storage params['packagedFile'] = "wikipedia_en_100_maxi.zim"; //For packaged Kiwix JS (e.g. with Wikivoyage file), set this to the filename (for split files, give the first chunk *.zimaa) and place file(s) in default storage
params['archivePath'] = "archives"; //The directory containing the packaged archive(s) (relative to app's root directory) params['archivePath'] = "archives"; //The directory containing the packaged archive(s) (relative to app's root directory)

View File

@ -23,7 +23,8 @@
'use strict'; 'use strict';
define(['q', 'settingsStore', 'uiUtil'], function(Q, settingsStore, uiUtil) { define(['q', 'settingsStore', 'uiUtil'], function(Q, settingsStore, uiUtil) {
const CACHE = 'kiwix-precache-' + params.version; // Set the database or cache name here const CACHEAPI = 'kiwix-precache-' + params.version; // Set the database or cache name here
const CACHEIDB = 'kiwix-assetsCache'; // For idxDB we don't want the name to change
var objStore = 'kiwix-assets'; // Name of the object store var objStore = 'kiwix-assets'; // Name of the object store
// DEV: Regex below defines the permitted key types for the cache; add further types as needed // DEV: Regex below defines the permitted key types for the cache; add further types as needed
@ -111,7 +112,7 @@ define(['q', 'settingsStore', 'uiUtil'], function(Q, settingsStore, uiUtil) {
case 'cacheAPI': case 'cacheAPI':
type = 'cacheAPI'; type = 'cacheAPI';
description = 'CacheAPI'; description = 'CacheAPI';
caches.open(CACHE).then(function (cache) { caches.open(CACHEAPI).then(function (cache) {
cache.keys().then(function (keys) { cache.keys().then(function (keys) {
callback({'type': type, 'description': description, 'count': keys.length}); callback({'type': type, 'description': description, 'count': keys.length});
}); });
@ -134,7 +135,8 @@ define(['q', 'settingsStore', 'uiUtil'], function(Q, settingsStore, uiUtil) {
* on the database * on the database
* *
* @param {String} keyOrCommand The key of the value to be written or read, or commands 'clear' (clears objStore), * @param {String} keyOrCommand The key of the value to be written or read, or commands 'clear' (clears objStore),
* 'count' (counts number of objects in objStore), 'delete' (deletes a record with key passed in valueOrCallback) * 'count' (counts number of objects in objStore), 'delete' (deletes a record with key passed in valueOrCallback),
* 'deleteNonCurrent' (deletes all databases that do not match CACHEIDB - but only works in Chromium currently)
* @param {Variable} valueOrCallback The value to write, or a callback function for read and command transactions * @param {Variable} valueOrCallback The value to write, or a callback function for read and command transactions
* @param {Function} callback Callback for write transactions only * @param {Function} callback Callback for write transactions only
*/ */
@ -145,9 +147,29 @@ define(['q', 'settingsStore', 'uiUtil'], function(Q, settingsStore, uiUtil) {
rtnFn(false); rtnFn(false);
return; return;
} }
// Delete all non-curren IdxDB databases (only works in Chromium currently)
if (keyOrCommand === 'deleteNonCurrent') {
if (indexedDB.databases) {
var result = 0;
indexedDB.databases().then(function (dbs) {
dbs.forEach(function (db) {
if (db.name !== CACHEIDB) {
result++;
indexedDB.deleteDatabase(db.name);
}
});
}).then(function () {
rtnFn(result);
});
} else {
rtnFn(false);
}
return;
}
// Open (or create) the database // Open (or create) the database
var open = indexedDB.open(CACHE, 1); var open = indexedDB.open(CACHEIDB, 1);
open.onerror = function(e) { open.onerror = function(e) {
// Suppress error reporting if testing (older versions of Firefox support indexedDB but cannot use it with // Suppress error reporting if testing (older versions of Firefox support indexedDB but cannot use it with
@ -222,14 +244,14 @@ define(['q', 'settingsStore', 'uiUtil'], function(Q, settingsStore, uiUtil) {
var rtnFn = callback || valueOrCallback; var rtnFn = callback || valueOrCallback;
// Process commands // Process commands
if (keyOrCommand === 'clear') { if (keyOrCommand === 'clear') {
caches.delete(CACHE).then(rtnFn); caches.delete(CACHEAPI).then(rtnFn);
} else if (keyOrCommand === 'delete') { } else if (keyOrCommand === 'delete') {
caches.open(CACHE).then(function(cache) { caches.open(CACHEAPI).then(function(cache) {
cache.delete(value).then(rtnFn); cache.delete(value).then(rtnFn);
}); });
} else if (value === null) { } else if (value === null) {
// Request retrieval of data // Request retrieval of data
caches.open(CACHE).then(function(cache) { caches.open(CACHEAPI).then(function(cache) {
cache.match('../' + keyOrCommand).then(function(response) { cache.match('../' + keyOrCommand).then(function(response) {
if (!response) { if (!response) {
rtnFn(null); rtnFn(null);
@ -245,7 +267,7 @@ define(['q', 'settingsStore', 'uiUtil'], function(Q, settingsStore, uiUtil) {
}); });
} else { } else {
// Request storing of data in cache // Request storing of data in cache
caches.open(CACHE).then(function(cache) { caches.open(CACHEAPI).then(function(cache) {
// Construct a Response from value // Construct a Response from value
var response = new Response(value); var response = new Response(value);
cache.put('../' + keyOrCommand, response).then(function() { cache.put('../' + keyOrCommand, response).then(function() {