Nathanwoodburn.github.io/pwa/sw.js

70 lines
1.9 KiB
JavaScript
Raw Normal View History

2025-01-29 18:51:28 +11:00
// This is the service worker with the combined offline experience (Offline page + Offline copy of pages)
const CACHE = "pwabuilder-offline-page";
importScripts('https://storage.googleapis.com/workbox-cdn/releases/5.1.2/workbox-sw.js');
2025-01-29 18:57:07 +11:00
const PRECACHE_ASSETS = [
'/',
2025-01-29 20:53:46 +11:00
'/404',
'/assets/bootstrap/css/bootstrap.min.css',
'https://fonts.googleapis.com/css?family=Lora:400,700,400italic,700italic&display=swap',
'https://fonts.googleapis.com/css?family=Cabin:700&display=swap',
'https://fonts.googleapis.com/css?family=Anonymous+Pro&display=swap',
'https://fonts.googleapis.com/css?family=Roboto:300,400,500,700',
'/assets/css/styles.min.css',
'/assets/css/404.min.css',
'/assets/css/profile.min.css',
'/assets/bootstrap/js/bootstrap.min.js',
'/assets/js/script.min.js',
'/assets/js/404.min.js',
'/assets/img/favicon/favicon-16x16.png',
'/assets/img/favicon/android-chrome-192x192.png'
2025-01-29 18:57:07 +11:00
]
2025-01-29 18:51:28 +11:00
self.addEventListener("message", (event) => {
if (event.data && event.data.type === "SKIP_WAITING") {
self.skipWaiting();
}
});
self.addEventListener('install', async (event) => {
event.waitUntil(
caches.open(CACHE)
2025-01-29 18:57:07 +11:00
.then((cache) => cache.addAll(PRECACHE_ASSETS))
2025-01-29 18:51:28 +11:00
);
});
if (workbox.navigationPreload.isSupported()) {
workbox.navigationPreload.enable();
}
workbox.routing.registerRoute(
new RegExp('/*'),
new workbox.strategies.StaleWhileRevalidate({
cacheName: CACHE
})
);
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);
2025-01-29 18:57:07 +11:00
const cachedResp = await cache.match('/404');
2025-01-29 18:51:28 +11:00
return cachedResp;
}
})());
}
});