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
1 changed files with 44 additions and 57 deletions

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))
.then(() => {
self.skipWaiting() 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,32 +44,25 @@ self.addEventListener('fetch', event => {
return return
} }
event.respondWith((async function () {
// always serve assets and webpack-generated files from cache // always serve assets and webpack-generated files from cache
if (cached.has(url.pathname)) { if (cached.has(url.pathname)) {
event.respondWith(caches.match(req)) return caches.match(req)
return
} }
// 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}`)
}
// For these, go cache-first.
if (CACHE_FIRST.some(pattern => url.pathname.startsWith(pattern))) {
event.respondWith(caches
.open(`offline${timestamp}`)
.then(async cache => {
let response = await cache.match(req) let response = await cache.match(req)
if (response) { if (response) {
// update asynchronously // update asynchronously
@ -86,10 +74,9 @@ self.addEventListener('fetch', event => {
response = await fetch(req) response = await fetch(req)
cache.put(req, response.clone()) cache.put(req, response.clone())
return response return response
}))
return
} }
// for everything else, go network-only // for everything else, go network-only
event.respondWith(fetch(req)) return fetch(req)
})())
}) })