-
Notifications
You must be signed in to change notification settings - Fork 0
/
sw.js
238 lines (228 loc) · 9.74 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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
// f06yBEAST
// Service Worker scoped per file location,
// so root, /, is global scope
// https://developers.google.com/web/fundamentals/getting-started/primers/service-workers
// https://developers.google.com/web/ilt/pwa/caching-files-with-service-worker
// @ MDN
// https://developer.mozilla.org/en-US/docs/Web/API/Service_Worker_API/Using_Service_Workers
;(() => {
const
noLOG = false //... no logging if true.
,log = (msg, flag = false) => {
if (noLOG) return
const title = msg.title
delete msg.title
if (typeof msg.url === 'string') {
msg.url = msg.url.replace(location.origin, '')
}
flag ? console.error(title, msg) : console.log(title, msg)
}
,logErr = msg => log(msg, true)
/**********************************
* Change 'name' to purge cache.
*********************************/
,appCaches = [
{
name: "shell-etag-2",
type: "shell",
urls: [
"/scripts/bundle.min.js",
"/styles/bundle.min.css"
]
},
{
name: "content-etag-1",
type: "content",
urls: [
"/"
]
}
]
,cache404 = '/app/404'
,cacheOffline = '/app/offline'
// CACHE FUNCTIONs
// function cacheNamePerType(t) {
// const _index = appCaches.findIndex(function (ac) {
// return ac.type === t
// })
// return appCaches[_index].name
// }
//console.log(cacheNamePerType('shell'))
/************
* Install
***********/
self.addEventListener('install', ev => {
ev.waitUntil(caches.keys()
.then(keysList => {
return Promise.all(appCaches.map(ac => {
// If this cache key is not in caches (SW object),
// then create it and add all its urls
if (keysList.indexOf(ac.name) === -1) {
caches.open(ac.name)
.then(cache => {
log({
title: 'CACHEd',
cache: ac.name,
})
// Fail silently on all cache except 'shell'
if (ac.type === 'shell') {
return cache.addAll(ac.urls)
} else {
cache.addAll(ac.urls)
}
}).catch(reason => {
return Promise.reject({
title: 'FAILed @ INSTALL',
cache: ac.name,
err: reason,
})
})
} else {
log({
title: 'FOUND : already-cached',
cache: ac.name,
})
return true
}
}
))
.then(() => {
log({
title: 'INSTALLed',
cache:appCaches,
})
return self.skipWaiting() //... activate NOW
})
.catch(logErr)
})
)
})
// ACTIVATE/UPDATE
//================
self.addEventListener('activate', ev => {
// Protect all newly installed caches; delete all others, per name(s).
const whiteList = appCaches.map(ac => ac.name)
ev.waitUntil(
caches.keys()
.then(keysList => {
return Promise.all(
keysList.map(key => {
if (whiteList.indexOf(key) === -1) {
log({
title: 'DELETEd CACHE @',
cache: key,
})
return caches.delete(key)
}
})
)
})
.catch(reason => {
return Promise.reject({
title: 'FAILed @ Activate/Update',
err: reason,
})
})
)
.then(() => {
log({
title: 'ACTIVATEd',
cacheWhitelist: whiteList,
})
return self.clients.claim()
})
.catch(logErr)
})
/**************************************************************************
* Fetch
*
* Aside from handling fetch, this event listener is used by browsers to
* validate PWA functionality. If absent or faux (caches nothing),
* then browser vendors might not allow the Add2HomeScreen button popup.
**************************************************************************/
self.addEventListener('fetch', ev => {
log({
title: 'Fetch INTERCEPTed @',
url: ev.request.url,
})
ev.respondWith(
caches.match(ev.request)
.then(resp => {
// STRATEGY: 'Cache falling back to network'
// https://developers.google.com/web/fundamentals/instant-and-offline/offline-cookbook/#cache-falling-back-to-network
// try CACHE
if (resp) {
log({
title: 'FETCHed from CACHE @',
url: resp.url
})
return resp // from CACHE
}
// if NOT in CACHE, then FETCH from NET
log({
title: 'Try NET @',
url: ev.request.url,
})
return fetch(ev.request) //'ev.request.clone()' ???
.then(resp => {
// on FAIL [of fetch from NET] ...
if(!resp || resp.status !== 200 || resp.type !== 'basic') {
logErr({
title: 'FAILed @ NET',
status: resp.status,
req: ev.request.url,
resp: resp.url,
text: resp.statusText,
type: resp.type,
})
if (resp.status === 404) {
return caches.match(cache404) // if FAIL 404
}
return resp // if FAIL other than 404
}
// On SUCCESS of fetch from NET,
// ADD it to transient CACHE
log({
title: 'FETCHed from NET',
url: ev.request.url,
})
const dynamicCache = 'content'
return caches.open(dynamicCache)
.then(cache => {
// Exclude 'some' page from cache.
if (ev.request.url.indexOf('some') < 0) {
cache.put(ev.request.url, resp.clone())
.then(() => {
log({
title: 'ADDed to CACHE',
cache: dynamicCache,
url: ev.request.url,
})
}).catch(reason => {
logErr({
title: 'FAILed @ CACHE',
cache: dynamicCache,
req: ev.request.url,
resp: resp.url,
err: reason,
})
})
}
return resp //... from NET
})
})
.catch(reason => { // if fetch() FAILed
logErr({
title: 'FAILed @ FETCH',
err: reason,
req: ev.request.url,
resp: resp.url,
})
throw error // https://developer.mozilla.org/en-US/docs/Web/API/FetchEvent/respondWith
}) // END CACHE MATCH
})
// IF offline AND url is NOT in cache
.catch(() => caches.match(cacheOffline))
)
})
})()