fix service worker cache bugs

This commit is contained in:
Nolan Lawson 2018-04-04 22:52:04 -07:00
parent 8f24531d94
commit f56f746477

View file

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