-
Notifications
You must be signed in to change notification settings - Fork 0
/
sw.js
73 lines (64 loc) · 2.03 KB
/
sw.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
//importScripts('js/idb.js');
var staticCacheName = 'restrev-v2';
self.addEventListener('install', function(event) {
event.waitUntil(
caches.open(staticCacheName).then(function(cache) {
//console.log("caches.open worked. Adding assets");
return cache.addAll([
"/",
"/index.html",
"/restaurant.html",
"/sw.js",
"/css/styles.css",
"/js/dbhelper.js",
"/js/sw-register.js",
"/js/main.js",
"/js/restaurant_info.js",
"/js/idb.js",
]).catch(function(error) {
console.log("caches.open failed: " + error);
});
})
);
});
self.addEventListener('activate', function(event) {
//console.log("activate");
event.waitUntil(
caches.keys().then(function(cacheNames) {
return Promise.all(
cacheNames.filter(function(cacheName) {
return cacheName.startsWith('restrev-') && cacheName != staticCacheName;
}).map(function(cacheName) {
return caches.delete(cacheName);
})
);
})
);
});
//Per https://developer.mozilla.org/en-US/docs/Web/API/FetchEvent, the
//event that is handed to this listener will do it's default fetch action
//unless somewhere in the listener there is a event.respondWith(...). Note
//that event.respondWith(...) returns a Promise.
//
//Note that dbhelper expects the response in the format that it comes from the
//server. Also note that I can change that :)
self.addEventListener('fetch', function(event) {
//const url = new URL(event.request.url);
event.respondWith(
caches.match(event.request).then(function(response) {
if (response) {
//console.log("Returning cached response for " + url);
return response;
} else {
//var i = url.pathname.search('/jpg|mapbox|leaflet/');
//if (i == -1) console.log("fetch " +i+ url.pathname);
return(fetch(event.request));
}
})
);
});
self.addEventListener('message', function(event) {
if (event.data.action === 'skipWaiting') {
self.skipWaiting();
}
});