34 lines
1016 B
JavaScript
34 lines
1016 B
JavaScript
const CACHE = "who-need-help-static-v1"
|
|
const SHELL = ["/assets/css/app.css", "/assets/js/app.js", "/images/logo.svg"]
|
|
|
|
self.addEventListener("install", event => {
|
|
event.waitUntil(caches.open(CACHE).then(cache => cache.addAll(SHELL)))
|
|
})
|
|
|
|
self.addEventListener("activate", event => {
|
|
event.waitUntil(
|
|
caches.keys().then(keys =>
|
|
Promise.all(keys.filter(key => key !== CACHE).map(key => caches.delete(key)))
|
|
)
|
|
)
|
|
})
|
|
|
|
self.addEventListener("fetch", event => {
|
|
const url = new URL(event.request.url)
|
|
const isStatic =
|
|
url.origin === self.location.origin &&
|
|
(url.pathname.startsWith("/assets/") || url.pathname.startsWith("/images/"))
|
|
|
|
if (event.request.method === "GET" && isStatic) {
|
|
event.respondWith(
|
|
caches.match(event.request).then(cached =>
|
|
cached || fetch(event.request).then(response => {
|
|
const copy = response.clone()
|
|
caches.open(CACHE).then(cache => cache.put(event.request, copy))
|
|
return response
|
|
})
|
|
)
|
|
)
|
|
}
|
|
})
|