-
Notifications
You must be signed in to change notification settings - Fork 116
/
service-worker.js
70 lines (62 loc) · 1.53 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
const NAME = 'htmltemplate-v2.0.1'
const FILES = [
'./index.html',
'./server.js',
'./src/404.html',
'./src/css/style.css',
'./src/css/modules/footer.css',
'./src/css/modules/header.css',
'./src/css/modules/loader.css',
'./src/css/modules/main.css',
'./src/js/script.js',
'./src/js/assets/index.js',
'./src/js/modules/create-timer.js',
'./src/js/modules/loader.js',
'./img/logo.png',
'./img/icons/64x64.png',
'./img/icons/70x70.png',
'./img/icons/128x128.png',
'./img/icons/150x150.png',
'./img/icons/256x256.png',
'./img/icons/310x150.png',
'./img/icons/310x310.png',
'./img/icons/512x512.png',
'./img/icons/600x600.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(
caches
.match(e.request)
.then(
(response) =>
response ||
fetch(e.request).then((response) =>
caches.open(NAME).then((cache) => {
if (e.request.method === 'GET') {
cache.put(e.request, response.clone())
}
return response
})
)
)
.catch(() => caches.match('./src/404.html'))
)
})