Add improvements from nwjs branch

Former-commit-id: c2a1e9fb7dc342699cd324dc40a9cbe89df1c8ae [formerly ea1d60c4aa9df2c813cd918d3eb6ec7317694b86]
Former-commit-id: 851ab23f17e00c7a422645bee147769c4cef0bd0
This commit is contained in:
Jaifroid 2020-02-26 09:55:30 +00:00
parent e921819ffc
commit cc8688fa2d
5 changed files with 24 additions and 53 deletions

View File

@ -130,7 +130,7 @@
<None Include="kiwix.pfx" />
<None Include="KiwixWebApp_StoreKey.pfx" />
<Content Include="manifest.json" />
<Content Include="service-worker.js" />
<Content Include="pwabuilder-sw.js" />
<Content Include="www\-\static\main.css" />
<Content Include="www\-\s\css_modules\content.parsoid.css" />
<Content Include="www\-\s\css_modules\ext.cite.a11y.css" />

View File

@ -75,7 +75,7 @@ const precacheFiles = [
// DEV: add any URL schemata that should be excluded from caching with the Cache API to the regex below
// As of 08-2019 the chrome-extension: schema is incompatible with the Cache API
// 'example-extension' is included to show how to add another schema if necessary
var excludedURLSchema = /^(?:chrome-extension|file|example-extension):/i;
var excludedURLSchema = /^(?:file|chrome-extension|example-extension):/i;
self.addEventListener("install", function (event) {
console.log("[SW] Install Event processing");
@ -240,9 +240,9 @@ function intercept(event) {
}, [messageChannel.port2]);
});
} else {
if (!excludedURLSchema.test(event.request.url)) return fetch(event.request).then(function (response) {
return fetch(event.request).then(function (response) {
// If request was success, add or update it in the cache
event.waitUntil(updateCache(event.request, response.clone()));
if (!excludedURLSchema.test(event.request.url)) event.waitUntil(updateCache(event.request, response.clone()));
return response;
}).catch(function (error) {
console.log("[SW] Network request failed and no cache.", error);

View File

@ -1345,14 +1345,15 @@ define(['jquery', 'zimArchiveLoader', 'uiUtil', 'util', 'utf8', 'images', 'cooki
'action': 'disable'
});
messageChannel = null;
// If we're not in a PWA context, completely unregister the SW
if (!/^https|\/localhost/i.test(window.location.href)) {
navigator.serviceWorker.getRegistrations().then(function(registrations) {
for (var registration of registrations) {
registration.unregister();
}
});
}
// If we're in electron or nwjs, completely remove the SW (but we need to keep it active if we're in a PWA)
// NOTE: We can't do this, because IE11 crashes just seeing "of", even though it will never run the code!
// if (typeof window.fs !== 'undefined') {
// navigator.serviceWorker.getRegistrations().then(function(registrations) {
// for (var registration of registrations) {
// registration.unregister();
// }
// });
// }
}
refreshAPIStatus();
} else if (value === 'serviceworker') {
@ -2414,7 +2415,7 @@ define(['jquery', 'zimArchiveLoader', 'uiUtil', 'util', 'utf8', 'images', 'cooki
htmlContent = -1;
// DEV: You should deal with the rare possibility that the cachedStartPage is not in the same namespace as the main page dirEntry...
// Ideally include the namespace in params.cachedStartPage and adjust/test code (not hard)
uiUtil.XHR(dirEntry.namespace + '/' + encodeURIComponent(params.cachedStartPage), 'text', function (responseTxt, status) {
uiUtil.XHR(dirEntry.namespace + '/' + encodeURIComponent(encodeURIComponent(params.cachedStartPage).replace(/%2F/, '/')).replace(/%2F/, '/'), 'text', function (responseTxt, status) {
htmlContent = /<html[^>]*>/.test(responseTxt) ? responseTxt : 0;
if (htmlContent) {
console.log('Article retrieved from storage cache...');

View File

@ -160,15 +160,15 @@ define(['q'], function(Q) {
// We are reading a packaged file and have to use Electron fs.read (so we don't have to pick the file)
fs.open(file.path, 'r', function (err, fd) {
if (err) {
console.error('Could not find file!', err);
reject(err);
} else {
var size = end - begin;
fs.read(fd, new Uint8Array(size), 0, size, begin, function (err, bytesRead, data) {
var arr = Buffer.alloc(size) || new Uint8Array(size);
fs.read(fd, arr, 0, size, begin, function (err, bytesRead, data) {
if (err) reject(err);
else resolve(data);
fs.close(fd, function (err) {
if (err) console.log('Could not close file...', err);
if (err) reject(err);
else return resolve(data);
});
});
}
@ -176,12 +176,11 @@ define(['q'], function(Q) {
} else {
var reader = new FileReader();
reader.readAsArrayBuffer(file.slice(begin, end));
reader.onload = function(e) {
reader.addEventListener('load', function(e) {
resolve(new Uint8Array(e.target.result));
};
reader.onerror = reader.onabort = function(e) {
reject(e);
};
});
reader.addEventListener('error', reject);
reader.addEventListener('abort', reject);
}
});
};

View File

@ -21,7 +21,7 @@
*/
'use strict';
define(['q', 'filecache'], function(q, FileCache) {
define(['q', 'filecache'], function(Q, FileCache) {
/**
* Utility function : return true if the given string ends with the suffix
@ -211,36 +211,7 @@ define(['q', 'filecache'], function(q, FileCache) {
* @returns {Promise} Promise
*/
function readFileSlice(file, begin, size) {
// var deferred = q.defer();
// if (file.readMode === 'electron') {
// // We are reading a packaged file and have to use Electron fs.read (so we don't have to pick the file)
// fs.open(file.path, 'r', function (err, fd) {
// if (err) {
// console.error('Could not find file!', err);
// } else {
// fs.read(fd, Buffer.alloc(size), 0, size, begin, function (err, bytesRead, data) {
// if (err) deferred.reject(err);
// else deferred.resolve(data);
// fs.close(fd, function (err) {
// if (err) console.log('Could not close file...', err);
// });
// });
// }
// });
// } else {
// We are reading a picked file, so use vanilla JS methods
// var reader = new FileReader();
// reader.onload = function (e) {
// deferred.resolve(new Uint8Array(e.target.result));
// };
// reader.onerror = reader.onabort = function (e) {
// deferred.reject(e);
// };
// reader.readAsArrayBuffer(file.slice(begin, begin + size));
// }
// return deferred.promise;
return FileCache.read(file, begin, begin + size);
// }
return FileCache.read(file, begin, begin + size);
}
/**