You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

52 lines
1.3 KiB
JavaScript

// This is the "Offline page" service worker
importScripts(
'https://storage.googleapis.com/workbox-cdn/releases/5.1.2/workbox-sw.js'
)
const offlineFallbackPage = 'offline.html'
const PRECACHE_ASSETS = [offlineFallbackPage]
const CACHE = 'voucher-app'
self.addEventListener('message', (event) => {
if (event.data && event.data.type === 'SKIP_WAITING') {
self.skipWaiting()
}
})
self.addEventListener('install', async (event) => {
event.waitUntil(
caches.open(CACHE).then((cache) => {
cache.addAll(PRECACHE_ASSETS)
})
)
})
if (workbox.navigationPreload.isSupported()) {
workbox.navigationPreload.enable()
}
self.addEventListener('fetch', (event) => {
if (event.request.mode === 'navigate') {
event.respondWith(
(async () => {
try {
const preloadResp = await event.preloadResponse
if (preloadResp) {
return preloadResp
}
const networkResp = await fetch(event.request)
return networkResp
} catch (error) {
const cache = await caches.open(CACHE)
const cachedResp = await cache.match(offlineFallbackPage)
return cachedResp
}
})()
)
}
})