mirror of
https://github.com/kiwix/kiwix-js-pwa.git
synced 2025-09-10 12:53:54 -04:00
Add improvements from nwjs branch
Former-commit-id: c2a1e9fb7dc342699cd324dc40a9cbe89df1c8ae [formerly ea1d60c4aa9df2c813cd918d3eb6ec7317694b86] Former-commit-id: 851ab23f17e00c7a422645bee147769c4cef0bd0
This commit is contained in:
parent
e921819ffc
commit
cc8688fa2d
@ -130,7 +130,7 @@
|
|||||||
<None Include="kiwix.pfx" />
|
<None Include="kiwix.pfx" />
|
||||||
<None Include="KiwixWebApp_StoreKey.pfx" />
|
<None Include="KiwixWebApp_StoreKey.pfx" />
|
||||||
<Content Include="manifest.json" />
|
<Content Include="manifest.json" />
|
||||||
<Content Include="service-worker.js" />
|
<Content Include="pwabuilder-sw.js" />
|
||||||
<Content Include="www\-\static\main.css" />
|
<Content Include="www\-\static\main.css" />
|
||||||
<Content Include="www\-\s\css_modules\content.parsoid.css" />
|
<Content Include="www\-\s\css_modules\content.parsoid.css" />
|
||||||
<Content Include="www\-\s\css_modules\ext.cite.a11y.css" />
|
<Content Include="www\-\s\css_modules\ext.cite.a11y.css" />
|
||||||
|
@ -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
|
// 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
|
// 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
|
// '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) {
|
self.addEventListener("install", function (event) {
|
||||||
console.log("[SW] Install Event processing");
|
console.log("[SW] Install Event processing");
|
||||||
@ -240,9 +240,9 @@ function intercept(event) {
|
|||||||
}, [messageChannel.port2]);
|
}, [messageChannel.port2]);
|
||||||
});
|
});
|
||||||
} else {
|
} 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
|
// 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;
|
return response;
|
||||||
}).catch(function (error) {
|
}).catch(function (error) {
|
||||||
console.log("[SW] Network request failed and no cache.", error);
|
console.log("[SW] Network request failed and no cache.", error);
|
||||||
|
@ -1345,14 +1345,15 @@ define(['jquery', 'zimArchiveLoader', 'uiUtil', 'util', 'utf8', 'images', 'cooki
|
|||||||
'action': 'disable'
|
'action': 'disable'
|
||||||
});
|
});
|
||||||
messageChannel = null;
|
messageChannel = null;
|
||||||
// If we're not in a PWA context, completely unregister the SW
|
// If we're in electron or nwjs, completely remove the SW (but we need to keep it active if we're in a PWA)
|
||||||
if (!/^https|\/localhost/i.test(window.location.href)) {
|
// NOTE: We can't do this, because IE11 crashes just seeing "of", even though it will never run the code!
|
||||||
navigator.serviceWorker.getRegistrations().then(function(registrations) {
|
// if (typeof window.fs !== 'undefined') {
|
||||||
for (var registration of registrations) {
|
// navigator.serviceWorker.getRegistrations().then(function(registrations) {
|
||||||
registration.unregister();
|
// for (var registration of registrations) {
|
||||||
}
|
// registration.unregister();
|
||||||
});
|
// }
|
||||||
}
|
// });
|
||||||
|
// }
|
||||||
}
|
}
|
||||||
refreshAPIStatus();
|
refreshAPIStatus();
|
||||||
} else if (value === 'serviceworker') {
|
} else if (value === 'serviceworker') {
|
||||||
@ -2414,7 +2415,7 @@ define(['jquery', 'zimArchiveLoader', 'uiUtil', 'util', 'utf8', 'images', 'cooki
|
|||||||
htmlContent = -1;
|
htmlContent = -1;
|
||||||
// DEV: You should deal with the rare possibility that the cachedStartPage is not in the same namespace as the main page dirEntry...
|
// 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)
|
// 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;
|
htmlContent = /<html[^>]*>/.test(responseTxt) ? responseTxt : 0;
|
||||||
if (htmlContent) {
|
if (htmlContent) {
|
||||||
console.log('Article retrieved from storage cache...');
|
console.log('Article retrieved from storage cache...');
|
||||||
|
@ -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)
|
// 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) {
|
fs.open(file.path, 'r', function (err, fd) {
|
||||||
if (err) {
|
if (err) {
|
||||||
console.error('Could not find file!', err);
|
|
||||||
reject(err);
|
reject(err);
|
||||||
} else {
|
} else {
|
||||||
var size = end - begin;
|
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);
|
if (err) reject(err);
|
||||||
else resolve(data);
|
|
||||||
fs.close(fd, function (err) {
|
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 {
|
} else {
|
||||||
var reader = new FileReader();
|
var reader = new FileReader();
|
||||||
reader.readAsArrayBuffer(file.slice(begin, end));
|
reader.readAsArrayBuffer(file.slice(begin, end));
|
||||||
reader.onload = function(e) {
|
reader.addEventListener('load', function(e) {
|
||||||
resolve(new Uint8Array(e.target.result));
|
resolve(new Uint8Array(e.target.result));
|
||||||
};
|
});
|
||||||
reader.onerror = reader.onabort = function(e) {
|
reader.addEventListener('error', reject);
|
||||||
reject(e);
|
reader.addEventListener('abort', reject);
|
||||||
};
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
@ -21,7 +21,7 @@
|
|||||||
*/
|
*/
|
||||||
'use strict';
|
'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
|
* 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
|
* @returns {Promise} Promise
|
||||||
*/
|
*/
|
||||||
function readFileSlice(file, begin, size) {
|
function readFileSlice(file, begin, size) {
|
||||||
// var deferred = q.defer();
|
return FileCache.read(file, begin, begin + size);
|
||||||
// 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);
|
|
||||||
// }
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
Loading…
x
Reference in New Issue
Block a user