From c68ebc39096d93867058f011b4e6313f53128819 Mon Sep 17 00:00:00 2001 From: Kit La Touche Date: Tue, 5 Nov 2024 22:22:22 -0500 Subject: Start to make this a PWA --- ui/service-worker.js | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 ui/service-worker.js (limited to 'ui/service-worker.js') diff --git a/ui/service-worker.js b/ui/service-worker.js new file mode 100644 index 0000000..e29fab3 --- /dev/null +++ b/ui/service-worker.js @@ -0,0 +1,35 @@ +/// +/// +/// +/// + +import { build, files, version } from '$service-worker'; + +// Create a unique cache name for this deployment +const CACHE = `cache-${version}`; + +const ASSETS = [ + ...build, // the app itself + ...files // everything in `static` +]; + +self.addEventListener('install', (event) => { + // Create a new cache and add all files to it + async function addFilesToCache() { + const cache = await caches.open(CACHE); + await cache.addAll(ASSETS); + } + + event.waitUntil(addFilesToCache()); +}); + +self.addEventListener('activate', (event) => { + // Remove previous cached data from disk + async function deleteOldCaches() { + for (const key of await caches.keys()) { + if (key !== CACHE) await caches.delete(key); + } + } + + event.waitUntil(deleteOldCaches()); +}); -- cgit v1.2.3