summaryrefslogtreecommitdiff
path: root/worker.js
diff options
context:
space:
mode:
authorAndrew Dolgov <[email protected]>2018-02-14 14:50:56 +0300
committerAndrew Dolgov <[email protected]>2018-02-14 14:50:56 +0300
commitd03b93a9ecc33cb7cb47484d0bd8a0b495d359d9 (patch)
tree3ef06432fca5793e8ed4cd54b37f0b3f6a89a185 /worker.js
parent66ba91e6b1892c02f0ff0777a2ee3a427cba4319 (diff)
rework offline cache to use a static URL list to make recovery possible
in situation where offline storage has been deleted but service worker install event hasn't happened yet
Diffstat (limited to 'worker.js')
-rw-r--r--worker.js45
1 files changed, 23 insertions, 22 deletions
diff --git a/worker.js b/worker.js
index fec8908..09660ed 100644
--- a/worker.js
+++ b/worker.js
@@ -1,11 +1,7 @@
//importScripts('lib/localforage.min.js');
-var CACHE_NAME = 'epube-v1';
-
-self.addEventListener('install', function(event) {
- event.waitUntil(
- caches.open(CACHE_NAME).then(function(cache) {
- var urls = [
+const CACHE_NAME = 'epube-v1';
+const CACHE_URLS = [
'read.html',
'js/common.js',
'js/read.js',
@@ -38,7 +34,12 @@ self.addEventListener('install', function(event) {
'lib/qtip2/jquery.qtip.min.js',
];
- return cache.addAll(urls.map(url => new Request(url, {credentials: 'same-origin'})));
+
+
+self.addEventListener('install', function(event) {
+ event.waitUntil(
+ caches.open(CACHE_NAME).then(function(cache) {
+ return cache.addAll(CACHE_URLS.map(url => new Request(url, {credentials: 'same-origin'})));
})
);
});
@@ -62,26 +63,26 @@ self.addEventListener('message', function(event){
caches.open(CACHE_NAME).then(function(cache) {
var promises = [];
- cache.keys().then(function(keys) {
- for (var i = 0; i < keys.length; i++) {
+ for (var i = 0; i < CACHE_URLS.length; i++) {
- if (keys[i].url.match("backend.php"))
- continue;
+ if (CACHE_URLS[i].match("backend.php"))
+ continue;
- //console.log(keys[i]);
+ //console.log(CACHE_URLS[i]);
- var promise = fetch(keys[i]).then(function(resp) {
- if (resp.status == 200) {
- cache.put(resp.url, resp);
- } else if (resp.status == 404) {
- cache.delete(resp.url);
- }
- });
+ var promise = fetch(CACHE_URLS[i]).then(function(resp) {
+ //console.log(resp);
- promises.push(promise);
+ if (resp.status == 200) {
+ cache.put(resp.url, resp);
+ } else if (resp.status == 404) {
+ cache.delete(resp.url);
+ }
+ });
- }
- });
+ promises.push(promise);
+
+ }
Promise.all(promises).then(function() {
console.log('all done');