forked from cybrespace/pinafore
rewrite service worker to use async/await
This commit is contained in:
parent
154f7067ec
commit
1b194745c0
|
@ -11,29 +11,24 @@ const cached = new Set(toCache)
|
|||
const routes = __routes__
|
||||
|
||||
self.addEventListener('install', event => {
|
||||
event.waitUntil(
|
||||
caches
|
||||
.open(ASSETS)
|
||||
.then(cache => cache.addAll(toCache))
|
||||
.then(() => {
|
||||
event.waitUntil((async function () {
|
||||
let cache = await caches.open(ASSETS)
|
||||
await cache.addAll(toCache)
|
||||
self.skipWaiting()
|
||||
})
|
||||
)
|
||||
})())
|
||||
})
|
||||
|
||||
self.addEventListener('activate', event => {
|
||||
event.waitUntil(
|
||||
caches.keys().then(async keys => {
|
||||
event.waitUntil((async function () {
|
||||
let keys = await caches.keys()
|
||||
// delete old caches
|
||||
for (const key of keys) {
|
||||
for (let key of keys) {
|
||||
if (key !== ASSETS) {
|
||||
await caches.delete(key)
|
||||
}
|
||||
}
|
||||
|
||||
await self.clients.claim()
|
||||
})
|
||||
)
|
||||
})())
|
||||
})
|
||||
|
||||
const CACHE_FIRST = [
|
||||
|
@ -49,32 +44,25 @@ self.addEventListener('fetch', event => {
|
|||
return
|
||||
}
|
||||
|
||||
event.respondWith((async function () {
|
||||
// always serve assets and webpack-generated files from cache
|
||||
if (cached.has(url.pathname)) {
|
||||
event.respondWith(caches.match(req))
|
||||
return
|
||||
return caches.match(req)
|
||||
}
|
||||
|
||||
// for pages, you might want to serve a shell `index.html` file,
|
||||
// which Sapper has generated for you. It's not right for every
|
||||
// app, but if it's right for yours then uncomment this section
|
||||
|
||||
if (url.origin === self.origin && routes.find(route => route.pattern.test(url.pathname))) {
|
||||
event.respondWith(caches.match('/index.html'))
|
||||
return
|
||||
if (url.origin === self.origin &&
|
||||
routes.find(route => route.pattern.test(url.pathname))) {
|
||||
return caches.match('/index.html')
|
||||
}
|
||||
|
||||
// For non-GET requests, go network-only
|
||||
if (req.method !== 'GET') {
|
||||
event.respondWith(fetch(req))
|
||||
return
|
||||
}
|
||||
|
||||
// For these, go cache-first.
|
||||
if (CACHE_FIRST.some(pattern => url.pathname.startsWith(pattern))) {
|
||||
event.respondWith(caches
|
||||
.open(`offline${timestamp}`)
|
||||
.then(async cache => {
|
||||
// For these GET requests, go cache-first
|
||||
if (req.method === 'GET' &&
|
||||
CACHE_FIRST.some(pattern => url.pathname.startsWith(pattern))) {
|
||||
let cache = await caches.open(`offline${timestamp}`)
|
||||
let response = await cache.match(req)
|
||||
if (response) {
|
||||
// update asynchronously
|
||||
|
@ -86,10 +74,9 @@ self.addEventListener('fetch', event => {
|
|||
response = await fetch(req)
|
||||
cache.put(req, response.clone())
|
||||
return response
|
||||
}))
|
||||
return
|
||||
}
|
||||
|
||||
// for everything else, go network-only
|
||||
event.respondWith(fetch(req))
|
||||
return fetch(req)
|
||||
})())
|
||||
})
|
||||
|
|
Loading…
Reference in New Issue