tweak service worker

This commit is contained in:
Nolan Lawson 2018-01-30 22:00:25 -08:00
parent b33d4bd955
commit f015e0e2a9

View file

@ -36,11 +36,6 @@ self.addEventListener('activate', event => {
) )
}) })
const NETWORK_ONLY = [
'/oauth',
'/api/v1/timelines'
]
const CACHE_FIRST = [ const CACHE_FIRST = [
'/system/accounts/avatars' '/system/accounts/avatars'
] ]
@ -69,17 +64,14 @@ self.addEventListener('fetch', event => {
return return
} }
// Non-GET and for certain endpoints (e.g. OAuth), go network-only // For non-GET requests, go network-only
if (req.method !== 'GET' || if (req.method !== 'GET') {
NETWORK_ONLY.some(pattern => url.pathname.startsWith(pattern))) {
//console.log('Using network-only for', url.href)
event.respondWith(fetch(req)) event.respondWith(fetch(req))
return return
} }
// For these, go cache-first. // For these, go cache-first.
if (CACHE_FIRST.some(pattern => url.pathname.startsWith(pattern))) { if (CACHE_FIRST.some(pattern => url.pathname.startsWith(pattern))) {
//console.log('Using cache-first for', url.href)
event.respondWith(caches event.respondWith(caches
.open(`offline${timestamp}`) .open(`offline${timestamp}`)
.then(async cache => { .then(async cache => {
@ -98,26 +90,6 @@ self.addEventListener('fetch', event => {
return return
} }
// for everything else, go network-only
// for everything else, try the network first, falling back to event.respondWith(fetch(req))
// cache if the user is offline. (If the pages never change, you
// might prefer a cache-first approach to a network-first one.)
event.respondWith(caches
.open(`offline${timestamp}`)
.then(async cache => {
try {
//console.log('Using network-first for', url.href)
const response = await fetch(req)
cache.put(req, response.clone())
return response
} catch (err) {
const response = await cache.match(req)
if (response) {
return response
}
throw err
}
})
)
}) })