You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
pages/sw.js

39 lines
765 B
JavaScript

3 years ago
async function initCache() {
const cache = await caches.open('v1')
await cache.addAll([
'/',
'/index.html',
'/app.js',
'/components/project.js',
])
3 years ago
}
self.addEventListener("install", event => {
event.waitUntil(initCache())
3 years ago
})
async function cacheFirst(request) {
const resp = await caches.match(request)
if (resp) {
return resp
} else {
return fetch(request)
}
3 years ago
}
self.addEventListener('fetch', event => {
event.respondWith(cacheFirst(event.request))
})
async function reloadAll() {
const windows = await clients.matchAll()
await Promise.allSettled(
windows.map(async client => {
await client.navigate(client.url)
})
)
}
self.addEventListener('activate', event => {
event.waitUntil(reloadAll())
3 years ago
})