aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--appcache.js38
1 files changed, 18 insertions, 20 deletions
diff --git a/appcache.js b/appcache.js
index e4b9bd5..8b117f4 100644
--- a/appcache.js
+++ b/appcache.js
@@ -42,33 +42,31 @@ class AppCache {
42 "pointless/viewer/screen/timer.js" 42 "pointless/viewer/screen/timer.js"
43 ]; 43 ];
44 44
45 const appCache = this; 45 self.addEventListener("install", event => event.waitUntil(this.onInstall()));
46 self.addEventListener("install", function(event) { 46 self.addEventListener("fetch", event => event.respondWith(this.onFetch(event.request)));
47 event.waitUntil(appCache._onInstall()); 47 }
48 });
49 48
50 self.addEventListener("fetch", function(event) { 49 onInstall() {
51 event.respondWith(appCache._onFetch(event.request)); 50 return caches.open(this.cacheName)
52 }); 51 .then(cache => cache.addAll(this.filesToCache));
53 } 52 }
54 53
55 _onInstall() { 54 onFetch(request) {
56 const self = this; 55 return caches.open(this.cacheName)
57 return caches.open(this.cacheName).then(function(cache) { 56 .then(cache => this._serve(cache, request));
58 return cache.addAll(self.filesToCache);
59 });
60 } 57 }
61 58
62 _onFetch(request) { 59 _serve(cache, request) {
63 return navigator.onLine ? this._fetchUpdate(request) : caches.match(request); 60 return cache.match(request).then(cachedResponse => {
61 const update = this._fetchUpdate(cache, request);
62 return cachedResponse || update;
63 });
64 } 64 }
65 65
66 _fetchUpdate(request) { 66 _fetchUpdate(cache, request) {
67 return caches.open(this.cacheName).then(function(cache) { 67 return fetch(request).then(networkResponse => {
68 return fetch(request).then(function(response) { 68 cache.put(request, networkResponse.clone());
69 cache.put(request, response.clone()); 69 return networkResponse;
70 return response;
71 });
72 }); 70 });
73 } 71 }
74} 72}