-
Notifications
You must be signed in to change notification settings - Fork 0
/
pokeapi-js-wrapper-sw.js
28 lines (25 loc) · 1015 Bytes
/
pokeapi-js-wrapper-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
const imgRe = /https:\/\/raw\.githubusercontent\.com\/PokeAPI\/sprites\/[\/-\w\d]+\/[\d\w-]+\.(?:png|svg|gif)/
const version = 1
self.addEventListener('fetch', function (event) {
if (event.request.url.match(imgRe)) {
event.respondWith(caches.match(event.request).then(function (response) {
if (response) {
return response
}
return fetch(event.request).then(function (response) {
if (event.request.url.match(imgRe)) {
caches.open("pokeapi-js-wrapper-images-" + version).then(function (cache) {
// The response is opaque, if it fails cache.add() will reject it
cache.add(event.request.url)
})
}
return response;
}).catch(function (error) {
console.error(error)
})
}))
}
})
self.addEventListener('install', function(event) {
self.skipWaiting()
})