generated from 11ty/eleventy-base-blog
/
serviceworker.js
59 lines (55 loc) · 1.51 KB
/
serviceworker.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
const version = 'V2.11',
staticCacheName = `${version}staticfiles`
addEventListener('install', (installEvent) => {
skipWaiting()
installEvent.waitUntil(
caches.open(staticCacheName).then((staticCache) => {
// Nice to Cache
staticCache.addAll([
'404.html',
'index.html',
'about/index.html',
'now/index.html',
'writing/index.html',
'/assets/images/global/header-default.jpg',
'/assets/images/global/header-dark.jpg',
'/assets/images/global/favicon.ico',
'/assets/images/global/profile.jpg',
'/assets/images/icons/icon-192.png',
'/assets/images/icons/icon-512.png',
])
// Need to Cache
return staticCache.addAll([
'/assets/css/main.css',
'/assets/javascript/main.js',
])
}),
)
})
// For clearing out old caches
addEventListener('activate', (activateEvent) => {
activateEvent.waitUntil(
// Clean the old caches up
caches
.keys()
.then((cacheNames) => {
return Promise.all(
cacheNames.map((cacheName) => {
if (cacheName !== staticCacheName) {
// Time to kill!
return caches.delete(cacheName)
}
}),
)
})
.then(() => clients.claim()),
)
})
addEventListener('fetch', (fetchEvent) => {
const request = fetchEvent.request
fetchEvent.respondWith(
caches.match(request).then((responseFromCache) => {
return responseFromCache || fetch(request)
}),
)
})