-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsw.js
More file actions
121 lines (116 loc) · 2.76 KB
/
Copy pathsw.js
File metadata and controls
121 lines (116 loc) · 2.76 KB
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
const cacheName = "dynamic-cache-v1";
const siteResources = [
"/",
"/books",
"/dvds",
"/books/",
"/dvds/",
"index.html",
"books/index.html",
"dvds/index.html",
//Icons
"favicon.ico",
"icons/favicon.png",
"icons/icon-128.png",
"icons/icon-144.png",
"icons/icon-192.png",
"icons/icon-512.png",
"icons/icon-72.png",
"icons/icon-96.png",
//Images
"images/check.svg",
"images/confused.svg",
"images/edit.svg",
"images/menu.svg",
"images/moon.svg",
"images/spinner.svg",
"images/sun.svg",
"images/wordmark-dark.svg",
"images/wordmark.svg",
"images/x.svg",
//Other
"manifest.json",
"pwa.js",
//Scripts
"scripts/home/app.mjs",
"scripts/home/buttons.mjs",
"scripts/home/forms.mjs",
"scripts/home/theme.js",
"scripts/list/addEntry.mjs",
"scripts/list/editEntry.mjs",
"scripts/list/init.mjs",
"scripts/list/removeEntry.mjs",
"scripts/list/results.mjs",
"scripts/createElement.mjs",
"scripts/error.mjs",
"scripts/onlineUI.js",
"scripts/sidepanel.js",
"scripts/signout.js",
"scripts/state.mjs",
"scripts/theme.js",
//Styles
"styles/app.css",
"styles/home.css",
//Fallback
"404/",
"404/index.html",
"404/main.css",
"404/app.js",
"404/arms.svg",
"404/circle.svg",
];
self.addEventListener("install", e => {
console.log("Installing");
e.waitUntil(
caches.open(cacheName)
.then(cache => {
return cache.addAll(siteResources);
})
.catch(err => {
console.error("Error while installing: ", err)
})
);
});
self.addEventListener("activate", e => {
const cacheKeeplist = [cacheName];
e.waitUntil(async function() {
caches.keys().then((keyList) => {
return Promise.all(keyList.map((key) => {
if (cacheKeeplist.indexOf(key) === -1) {
return caches.delete(key);
}
}));
});
if (self.registration.navigationPreload) {
await self.registration.navigationPreload.disable();
}
}());
console.log("Activated");
});
self.addEventListener('fetch', (event) => {
event.respondWith(
caches.match(event.request)
.then(async response => {
const networkResponse = fetch(event.request)
.then(res => {
let clonedResponse = res.clone();
caches.open(cacheName)
.then(cache => {
cache.put(event.request.url, clonedResponse);
})
.catch(err => {
console.log("Failed to save network response");
});
return res;
});
if (siteResources.includes(event.request.url)) {
return response || await networkResponse;
} else {
return await networkResponse || response;
}
}).catch(err => {
console.log(err);
return caches.match('/404/index.html');
})
);
});