fix service worker cache bugs

This commit is contained in:
Nolan Lawson 2018-04-04 22:52:04 -07:00
parent 8f24531d94
commit f56f746477
1 changed files with 12 additions and 12 deletions

View File

@ -15,8 +15,6 @@ const webpackAssets = __shell__
.filter(filename => !filename.endsWith('.map'))
.filter(filename => filename !== '/index.html')
const cacheSet = new Set([].concat(assets).concat(webpackAssets))
// `routes` is an array of `{ pattern: RegExp }` objects that
// match the pages in your app
const routes = __routes__
@ -81,17 +79,19 @@ self.addEventListener('fetch', event => {
event.respondWith((async () => {
let sameOrigin = url.origin === self.origin
if (sameOrigin) {
// always serve webpack-generated resources and
// assets from the cache
if (sameOrigin && cacheSet.has(url.pathname)) {
return caches.match(req)
// assets from the cache if possible
let response = await caches.match(req)
if (response) {
return response
}
// for routes, serve the /index.html file from the most recent
// assets cache
if (sameOrigin && routes.find(route => route.pattern.test(url.pathname))) {
if (routes.find(route => route.pattern.test(url.pathname))) {
return caches.match('/index.html')
}
}
// For these GET requests, go cache-first
if (req.method === 'GET' &&