rewrite service worker to use async/await

This commit is contained in:
Nolan Lawson 2018-03-15 00:03:22 -07:00
parent 154f7067ec
commit 1b194745c0

View file

@ -11,29 +11,24 @@ const cached = new Set(toCache)
const routes = __routes__ const routes = __routes__
self.addEventListener('install', event => { self.addEventListener('install', event => {
event.waitUntil( event.waitUntil((async function () {
caches let cache = await caches.open(ASSETS)
.open(ASSETS) await cache.addAll(toCache)
.then(cache => cache.addAll(toCache)) self.skipWaiting()
.then(() => { })())
self.skipWaiting()
})
)
}) })
self.addEventListener('activate', event => { self.addEventListener('activate', event => {
event.waitUntil( event.waitUntil((async function () {
caches.keys().then(async keys => { let keys = await caches.keys()
// delete old caches // delete old caches
for (const key of keys) { for (let key of keys) {
if (key !== ASSETS) { if (key !== ASSETS) {
await caches.delete(key) await caches.delete(key)
}
} }
}
await self.clients.claim() await self.clients.claim()
}) })())
)
}) })
const CACHE_FIRST = [ const CACHE_FIRST = [
@ -49,47 +44,39 @@ self.addEventListener('fetch', event => {
return return
} }
// always serve assets and webpack-generated files from cache event.respondWith((async function () {
if (cached.has(url.pathname)) { // always serve assets and webpack-generated files from cache
event.respondWith(caches.match(req)) if (cached.has(url.pathname)) {
return return caches.match(req)
} }
// for pages, you might want to serve a shell `index.html` file, // for pages, you might want to serve a shell `index.html` file,
// which Sapper has generated for you. It's not right for every // which Sapper has generated for you. It's not right for every
// app, but if it's right for yours then uncomment this section // 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))) { if (url.origin === self.origin &&
event.respondWith(caches.match('/index.html')) routes.find(route => route.pattern.test(url.pathname))) {
return return caches.match('/index.html')
} }
// For non-GET requests, go network-only // For these GET requests, go cache-first
if (req.method !== 'GET') { if (req.method === 'GET' &&
event.respondWith(fetch(req)) CACHE_FIRST.some(pattern => url.pathname.startsWith(pattern))) {
return let cache = await caches.open(`offline${timestamp}`)
} let response = await cache.match(req)
if (response) {
// For these, go cache-first. // update asynchronously
if (CACHE_FIRST.some(pattern => url.pathname.startsWith(pattern))) { fetch(req).then(response => {
event.respondWith(caches cache.put(req, response.clone())
.open(`offline${timestamp}`) })
.then(async cache => {
let response = await cache.match(req)
if (response) {
// update asynchronously
fetch(req).then(response => {
cache.put(req, response.clone())
})
return response
}
response = await fetch(req)
cache.put(req, response.clone())
return response return response
})) }
return response = await fetch(req)
} cache.put(req, response.clone())
return response
}
// for everything else, go network-only // for everything else, go network-only
event.respondWith(fetch(req)) return fetch(req)
})())
}) })