-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathservice-worker.js
98 lines (84 loc) · 2.46 KB
/
service-worker.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
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
// Initialize constants
const version = "spanish-quizzer-7";
const resources = [
"./css/app.css",
"./css/filtersPage.css",
"./css/global.css",
"./css/quizzer.css",
"./css/reference.css",
"./css/settingsPage.css",
"./data/verbs.csv",
"./data/vocab.csv",
"./images/arrow-left.svg",
"./images/favicon-32.png",
"./images/favicon-180.png",
"./images/favicon-192-maskable.png",
"./images/favicon-192.png",
"./images/favicon-512-maskable.png",
"./images/favicon-512.png",
"./images/plus.svg",
"./images/settings.svg",
"./images/sound.svg",
"./images/trash.svg",
"./images/x.svg",
"./js/app.js",
"./js/filters.js",
"./js/filtersPage.js",
"./js/global.js",
"./js/quizzer.js",
"./js/reference.js",
"./js/settingsPage.js",
"./vendor/diff.js",
"./vendor/data-table.min.css",
"./vendor/data-table.min.js",
"./vendor/papaparse.js",
"./vendor/vue-router.js",
"./vendor/vue.js",
"./index.html",
"./",
];
self.addEventListener("install", function(event) {
event.waitUntil(async function() {
// Cache resources
const cache = await caches.open(version);
await cache.addAll(resources);
}());
});
self.addEventListener("fetch", function(event) {
// Ignore non-GET requests
if (event.request.method !== "GET") return;
event.respondWith(async function() {
// Look for cached response
const cache = await caches.open(version);
const cachedResponse = await cache.match(event.request);
if (cachedResponse) {
// Update cache in the background
event.waitUntil(cache.add(event.request));
// Returned cached response
return cachedResponse;
}
else {
// Fall back to network
const response = await fetch(event.request);
// Add response to cache
cache.put(event.request, response.clone());
// Return response
return response;
}
}());
});
self.addEventListener("activate", function(event) {
event.waitUntil(
// Remove outdated caches
caches.keys().then(function (keys) {
return Promise.all(
keys.filter(function (key) {
return key != version;
})
.map(function (key) {
return caches.delete(key);
})
);
})
);
});