-
Notifications
You must be signed in to change notification settings - Fork 1
/
sw.js
54 lines (49 loc) · 1.2 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
const VERSION = 3;
const FILES = [
'src/utils/misc.js',
'src/utils/history.js',
'src/utils/model.js',
'src/utils/baseElement.js',
'src/models/dictionary.js',
'src/models/history.js',
'src/components/wordList/index.js',
'src/components/searchInput/index.js',
'src/components/header/index.js',
'src/pages/home.js',
'src/normalize.css',
'src/root.css',
];
const cacheName = `vocab-${VERSION}`;
self.addEventListener('install', event => {
event.waitUntil(
caches.open(cacheName).then(cache => {
return cache.addAll(FILES);
})
);
});
self.addEventListener('fetch', event => {
const url = new URL(event.request.url);
const matchedCache = (() => {
switch (url.hostname) {
case 'unpkg.com':
return 'unpkg';
case 'fonts.google.com':
return 'fonts';
default:
return cacheName;
}
})();
event.respondWith(
caches.open(matchedCache).then(cache => {
return cache.match(event.request).then(response => {
return (
response ||
fetch(event.request).then(response => {
cache.put(event.request, response.clone());
return response;
})
);
});
})
);
});