Added SW cache clear mechanism.

This commit is contained in:
Michael Mainguy 2024-05-08 09:17:06 -05:00
parent bbe54dc3e3
commit 15fdb938ee
3 changed files with 42 additions and 57 deletions

View File

@ -21,7 +21,15 @@
<img id="loadingGrid" src="/assets/grid6.jpg"/> <img id="loadingGrid" src="/assets/grid6.jpg"/>
<script> <script>
if (typeof navigator.serviceWorker !== 'undefined') { if (typeof navigator.serviceWorker !== 'undefined') {
navigator.serviceWorker.register('/sw.js') if (localStorage.getItem('serviceWorkerVersion') != '8') {
caches.keys().then(cacheNames => {
cacheNames.forEach(cacheName => {
caches.delete(cacheName);
});
});
localStorage.setItem('serviceWorkerVersion', '8');
}
navigator.serviceWorker.register('/sw.js', {updateViaCache: 'none'});
} }
</script> </script>
<script> <script>

View File

@ -1,29 +1,52 @@
importScripts('https://storage.googleapis.com/workbox-cdn/releases/7.1.0/workbox-sw.js'); importScripts('https://storage.googleapis.com/workbox-cdn/releases/7.1.0/workbox-sw.js');
const VERSION = '6'; const VERSION = '8';
const CACHE = "deepdiagram"; const CACHE = "deepdiagram";
const IMAGEDELIVERY_CACHE = "deepdiagram-images"; const IMAGEDELIVERY_CACHE = "deepdiagram-images";
// TODO: replace the following with the correct offline fallback page i.e.: const offlineFallbackPage = "offline.html"; // TODO: replace the following with the correct offline fallback page i.e.: const offlineFallbackPage = "offline.html";
const offlineFallbackPage = "/"; const offlineFallbackPage = "/";
/*self.addEventListener('install', async (event) => {
self.skipWaiting();
});
self.addEventListener('activate', async (event) => {
self.skipWaiting();
self.clients.matchAll({
type: 'window'
}).then(windowClients => {
windowClients.forEach((windowClient) => {
windowClient.navigate(windowClient.url);
});
});
});
*/
self.addEventListener("message", (event) => { self.addEventListener("message", (event) => {
if (event.data && event.data.type === "SKIP_WAITING") { if (event.data && event.data.type === "SKIP_WAITING") {
self.skipWaiting(); self.skipWaiting();
} }
}); });
self.addEventListener('install', async (event) => {
/*self.addEventListener('install', async (event) => {
event.waitUntil( event.waitUntil(
caches.open(CACHE) caches.open(CACHE)
.then((cache) => cache.add(offlineFallbackPage)) .then((cache) => cache.add(offlineFallbackPage))
); );
}); });
*/
if (workbox.navigationPreload.isSupported()) { /*if (workbox.navigationPreload.isSupported()) {
workbox.navigationPreload.enable(); workbox.navigationPreload.enable();
} }*/
workbox.routing.registerRoute( workbox.routing.registerRoute(
new RegExp('/.*\\.png'), new RegExp('/.*\\.wasm'),
new workbox.strategies.CacheFirst({
cacheName: CACHE
})
);
console.warn('workbox');
workbox.routing.registerRoute(
new RegExp('/assets/.*'),
new workbox.strategies.CacheFirst({ new workbox.strategies.CacheFirst({
cacheName: CACHE cacheName: CACHE
}) })
@ -41,24 +64,7 @@ workbox.routing.registerRoute(
cacheName: CACHE cacheName: CACHE
}) })
); );
workbox.routing.registerRoute(
new RegExp('/.*\\.svg'),
new workbox.strategies.CacheFirst({
cacheName: CACHE
})
);
workbox.routing.registerRoute(
new RegExp('/.*\\.jpeg'),
new workbox.strategies.CacheFirst({
cacheName: CACHE
})
);
workbox.routing.registerRoute(
new RegExp('/.*\\.jpg'),
new workbox.strategies.CacheFirst({
cacheName: CACHE
})
);
workbox.routing.registerRoute( workbox.routing.registerRoute(
new RegExp('/.*\\.glb'), new RegExp('/.*\\.glb'),
new workbox.strategies.CacheFirst({ new workbox.strategies.CacheFirst({
@ -71,34 +77,5 @@ workbox.routing.registerRoute(
cacheName: CACHE cacheName: CACHE
}) })
); );
workbox.routing.registerRoute(
new RegExp('/.*\\.js'),
new workbox.strategies.CacheFirst({
cacheName: CACHE
})
);
workbox.routing.registerRoute(
new RegExp('/.*\\.wasm'),
new workbox.strategies.CacheFirst({
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;
}
})());
}
});

View File

@ -15,7 +15,7 @@ import {Introduction} from "./tutorial/introduction";
const webGpu = false; const webGpu = false;
log.setLevel('error', false); log.setLevel('debug', false);
const canvas = (document.querySelector('#gameCanvas') as HTMLCanvasElement); const canvas = (document.querySelector('#gameCanvas') as HTMLCanvasElement);
export class VrApp { export class VrApp {