Update service-worker.js

#4
This commit is contained in:
Arhey 2022-05-08 19:53:39 +03:00
parent edcd3f5b4a
commit dbafe60db2

View file

@ -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)
})()
)
})