-
Notifications
You must be signed in to change notification settings - Fork 0
/
service-worker.js
34 lines (31 loc) · 1.02 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
//@ts-nocheck
const CACHE_NAME = "arithmetist-v10"
//Delete old caches when the cache-name changes
self.addEventListener("activate", async event => {
const existingCaches = await caches.keys()
const invalidCaches = existingCaches.filter(c => c !== CACHE_NAME)
await Promise.all(invalidCaches.map(ic => caches.delete(ic)))
})
// for local development, skip caching entirely
// for production use, cache everything
// cache is invalidated by publishing a new
// service worker with a new CACHE_NAME.
self.addEventListener("fetch", event => {
event.respondWith(
caches.open(CACHE_NAME).then(function (cache) {
if (self.location.hostname === "localhost") {
return fetch(event.request)
}
return cache.match(event.request).then(function (response) {
if (!!response) {
return response
} else {
return fetch(event.request).then(function (response) {
cache.put(event.request, response.clone())
return response
})
}
})
})
)
})