updated service worker.

This commit is contained in:
Michael Mainguy 2024-05-07 09:20:38 -05:00
parent 2debefd556
commit 7ccca76119
2 changed files with 56 additions and 9 deletions

View File

@ -19,6 +19,11 @@
</head> </head>
<body> <body>
<img id="loadingGrid" src="/assets/grid6.jpg"/> <img id="loadingGrid" src="/assets/grid6.jpg"/>
<script>
if (typeof navigator.serviceWorker !== 'undefined') {
navigator.serviceWorker.register('/sw.js')
}
</script>
<script> <script>
/* /*
var SpeechRecognition = SpeechRecognition || webkitSpeechRecognition var SpeechRecognition = SpeechRecognition || webkitSpeechRecognition

View File

@ -1,7 +1,9 @@
const VERSION = '0'; importScripts('https://storage.googleapis.com/workbox-cdn/releases/7.1.0/workbox-sw.js');
const CACHE = "pwabuilder-offline"; const VERSION = '2';
const PRECACHE_ASSETS = [] const CACHE = "deepdiagram";
importScripts('https://storage.googleapis.com/workbox-cdn/releases/6.4.1/workbox-sw.js');
// TODO: replace the following with the correct offline fallback page i.e.: const offlineFallbackPage = "offline.html";
const offlineFallbackPage = "/";
self.addEventListener("message", (event) => { self.addEventListener("message", (event) => {
if (event.data && event.data.type === "SKIP_WAITING") { if (event.data && event.data.type === "SKIP_WAITING") {
@ -9,7 +11,16 @@ self.addEventListener("message", (event) => {
} }
}); });
self.addEventListener('install', async (event) => {
event.waitUntil(
caches.open(CACHE)
.then((cache) => cache.add(offlineFallbackPage))
);
});
if (workbox.navigationPreload.isSupported()) {
workbox.navigationPreload.enable();
}
workbox.routing.registerRoute( workbox.routing.registerRoute(
new RegExp('/.*\\.png'), new RegExp('/.*\\.png'),
new workbox.strategies.StaleWhileRevalidate({ new workbox.strategies.StaleWhileRevalidate({
@ -34,9 +45,40 @@ workbox.routing.registerRoute(
cacheName: CACHE cacheName: CACHE
}) })
); );
workbox.routing.registerRoute( workbox.routing.registerRoute(
new RegExp('/login'), new RegExp('/.*\\.css'),
new workbox.strategies.NetworkFirst() new workbox.strategies.StaleWhileRevalidate({
) cacheName: CACHE
})
);
workbox.routing.registerRoute(
new RegExp('/.*\\.js'),
new workbox.strategies.StaleWhileRevalidate({
cacheName: CACHE
})
);
workbox.routing.registerRoute(
new RegExp('/.*\\.wasm'),
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);
const cachedResp = await cache.match(offlineFallbackPage);
return cachedResp;
}
})());
}
});