diff options
| author | Kit La Touche <kit@transneptune.net> | 2024-11-05 22:22:22 -0500 |
|---|---|---|
| committer | Kit La Touche <kit@transneptune.net> | 2024-11-05 22:22:22 -0500 |
| commit | c68ebc39096d93867058f011b4e6313f53128819 (patch) | |
| tree | 41eb253d6d9d81dcb6c7f244ab4c65a6023b87e4 /ui/service-worker.js | |
| parent | 2f67205b83009c874f4254a4789b1945668b3056 (diff) | |
Start to make this a PWA
Diffstat (limited to 'ui/service-worker.js')
| -rw-r--r-- | ui/service-worker.js | 35 |
1 files changed, 35 insertions, 0 deletions
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 @@ +/// <reference types="@sveltejs/kit" /> +/// <reference no-default-lib="true"/> +/// <reference lib="esnext" /> +/// <reference lib="webworker" /> + +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()); +}); |
