42 lines
1.1 KiB
JavaScript
42 lines
1.1 KiB
JavaScript
const CACHE = "who-need-help-static-v2"
|
|
const SHELL = [
|
|
"/assets/css/app.css",
|
|
"/assets/js/app.js",
|
|
"/images/logo.svg",
|
|
"/images/pwa-192.png",
|
|
"/images/pwa-512.png"
|
|
]
|
|
|
|
self.addEventListener("install", event => {
|
|
event.waitUntil(
|
|
caches.open(CACHE).then(cache => cache.addAll(SHELL)).then(() => self.skipWaiting())
|
|
)
|
|
})
|
|
|
|
self.addEventListener("activate", event => {
|
|
event.waitUntil(
|
|
caches.keys().then(keys =>
|
|
Promise.all(keys.filter(key => key !== CACHE).map(key => caches.delete(key)))
|
|
).then(() => self.clients.claim())
|
|
)
|
|
})
|
|
|
|
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
|
|
})
|
|
)
|
|
)
|
|
}
|
|
})
|