-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsw.js
69 lines (63 loc) · 2.08 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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
// const staticCacheName =
const cacheData = {
static: {
name: 'static-eng-dictionary',
urls: [
'./files/fonts/poppins400.woff2',
'./files/fonts/poppins500.woff2',
'./files/fonts/poppins600.woff2',
'./files/fonts/poppins700.woff2',
'./files/fonts/poppins800.woff2'
]
},
dynamic: {
name: 'dynamic-eng-dictionary'
},
noUpdate: {
name: "no-update-eng-dictionary",
urls: [
]
}
}
self.addEventListener('install', event => {
event.waitUntil(
caches.open(cacheData.static.name).then(cache => {
cache.addAll(cacheData.static.urls)
})
)
})
self.addEventListener('fetch', event => {
if (cacheData.static.urls.includes(event.request.url))
event.respondWith(
caches.match(event.request).then(cacheResponse => {
return cacheResponse || fetch(event.request)
})
)
// Only Load for the first time
else if (event.request.url.includes('/images/illustration/')) {
// console.log("Illustration Load")
event.respondWith(
caches.match(event.request).then(cacheResponse => {
const fetchUrl = fetch(event.request).then(fetchResponse => {
return caches.open(cacheData.static.name).then(cache => {
cache.put(event.request, fetchResponse.clone())
return fetchResponse
})
})
return cacheResponse || fetchUrl
})
)
}
else
event.respondWith(
caches.match(event.request).then(cacheResponse => {
const fetchUrl = fetch(event.request).then(fetchResponse => {
return caches.open(cacheData.dynamic.name).then(cache => {
cache.put(event.request, fetchResponse.clone())
return fetchResponse
})
})
return cacheResponse || fetchUrl
})
)
})