forked from cybrespace/pinafore
fix service worker cache bugs
This commit is contained in:
parent
8f24531d94
commit
f56f746477
|
@ -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,17 +79,19 @@ self.addEventListener('fetch', event => {
|
||||||
event.respondWith((async () => {
|
event.respondWith((async () => {
|
||||||
let sameOrigin = url.origin === self.origin
|
let sameOrigin = url.origin === self.origin
|
||||||
|
|
||||||
|
if (sameOrigin) {
|
||||||
// always serve webpack-generated resources and
|
// always serve webpack-generated resources and
|
||||||
// assets from the cache
|
// assets from the cache if possible
|
||||||
if (sameOrigin && cacheSet.has(url.pathname)) {
|
let response = await caches.match(req)
|
||||||
return caches.match(req)
|
if (response) {
|
||||||
|
return response
|
||||||
}
|
}
|
||||||
|
|
||||||
// for routes, serve the /index.html file from the most recent
|
// for routes, serve the /index.html file from the most recent
|
||||||
// assets cache
|
// 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')
|
return caches.match('/index.html')
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// For these GET requests, go cache-first
|
// For these GET requests, go cache-first
|
||||||
if (req.method === 'GET' &&
|
if (req.method === 'GET' &&
|
||||||
|
|
Loading…
Reference in New Issue