Update service-worker.js

This commit is contained in:
Arhey 2022-05-08 21:01:28 +03:00
parent 35b78f84cb
commit 15a3e5f54e

View file

@ -53,20 +53,18 @@ 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 isSameOrigin = url.host === self.location.host
const isStaticAsset = isSameOrigin && staticAssets.has(url.pathname)
const skipBecauseUncached = event.request.cache === 'only-if-cached' && !isStaticAsset
if (isHttp && isSameOrigin && !isDevServerRequest && !skipBecauseUncached) {
event.respondWith(
(async () => {
const cachedAsset = isStaticAsset && (await caches.match(event.request))
if (!isHttp || !isSameOrigin || skipBecauseUncached) return
event.respondWith(
(async () => {
let cachedAsset
if (isStaticAsset) {
cachedAsset = await caches.match(event.request)
}
return cachedAsset || fetchAndCache(event.request)
})()
)
return cachedAsset || fetchAndCache(event.request)
})()
)
}
})