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

46 lines
1.0 KiB
JavaScript

3 years ago
async function initCache() {
const cache = await caches.open('v1')
await cache.addAll([
'/',
'/index.html',
'/app.js',
'/components/page.js',
'/components/layout.js',
'/components/header.js',
'/components/nav-menu.js'
])
3 years ago
}
self.addEventListener("install", event => {
event.waitUntil(initCache())
3 years ago
})
async function cacheFirst(request) {
if (request.url.includes('/-/frame')) {
const url = new URL(request.url)
if (url.pathname === '/-/frame') {
const html = url.searchParams.get('html')
const csp = url.searchParams.get('csp')
return new Response(html, {
headers: {
'Content-Type': 'text/html',
'Content-Security-Policy': csp,
}
})
}
}
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))
})
self.addEventListener('activate', event => {
event.waitUntil(clients.claim())
3 years ago
})