forked from harryheman/modern-html-starter-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
service-worker.js
58 lines (52 loc) · 1.22 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
const NAME = 'cache-name-v1'
const FILES = [
'./index.html',
'./css/style.css',
'./css/modules/footer.css',
'./css/modules/header.css',
'./css/modules/loader.css',
'./css/modules/main.css',
'./script.js',
'./js/src/assets.js',
'./js/modules/create-timer.js',
'./js/modules/loader.js',
'./icons/64x64.png',
'./icons/128x128.png',
'./icons/150x150.png',
'./icons/256x256.png',
'./icons/512x512.png',
]
self.addEventListener('install', (e) => {
e.waitUntil(caches.open(NAME).then((cache) => cache.addAll(FILES)))
self.skipWaiting()
})
self.addEventListener('activate', (e) => {
e.waitUntil(
caches.keys().then((keys) =>
Promise.all(
keys.map((key) => {
if (key !== NAME) {
return caches.delete(key)
}
})
)
)
)
self.clients.claim()
})
self.addEventListener('fetch', (e) => {
e.respondWith(
fetch(e.request)
.then(
(response) =>
response ||
fetch(e.request).then((response) =>
caches.open(NAME).then((cache) => {
cache.put(e.request, response.clone())
return response
})
)
)
.catch(() => caches.match('./404.html'))
)
})