diff --git a/src/service-worker.js b/src/service-worker.js index 818a96ba7..71a967738 100644 --- a/src/service-worker.js +++ b/src/service-worker.js @@ -53,18 +53,20 @@ self.addEventListener('fetch', event => { const url = new URL(event.request.url) const isHttp = url.protocol.startsWith('http') - const isDevServerRequest = - url.hostname === self.location.hostname && url.port !== self.location.port - const isStaticAsset = url.host === self.location.host && staticAssets.has(url.pathname) + const isSameOrigin = url.host === self.location.host + const isStaticAsset = isSameOrigin && staticAssets.has(url.pathname) const skipBecauseUncached = event.request.cache === 'only-if-cached' && !isStaticAsset - if (isHttp && !isDevServerRequest && !skipBecauseUncached) { - event.respondWith( - (async () => { - const cachedAsset = isStaticAsset && (await caches.match(event.request)) + if (!isHttp || !isSameOrigin || skipBecauseUncached) return - return cachedAsset || fetchAndCache(event.request) - })() - ) - } + event.respondWith( + (async () => { + let cachedAsset + if (isStaticAsset) { + cachedAsset = await caches.match(event.request) + } + + return cachedAsset || fetchAndCache(event.request) + })() + ) })