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 From 8751155e24f020802d1c387af19318edceaa39d2 Mon Sep 17 00:00:00 2001 From: Kit La Touche Date: Wed, 6 Nov 2024 22:59:29 -0500 Subject: Add comment explaining why this isn't happy in Firefox --- ui/service-worker.js | 6 ++++++ 1 file changed, 6 insertions(+) (limited to 'ui/service-worker.js') diff --git a/ui/service-worker.js b/ui/service-worker.js index e29fab3..033c8d9 100644 --- a/ui/service-worker.js +++ b/ui/service-worker.js @@ -3,6 +3,12 @@ /// /// +// Because of this line, this service worker won't run in dev mode in Firefox. +// Only Safari, Edge, Chrome can run it at the moment, because only they +// support modules in service workers. +// +// That's okay! If you run `tools/run` with HI_DEV unset, you will get the +// bundled version, and can work on it. Or just use Safari. import { build, files, version } from '$service-worker'; // Create a unique cache name for this deployment -- cgit v1.2.3 From 9c0fcc5534b77d9fd1d712c9472c57d01c8c5274 Mon Sep 17 00:00:00 2001 From: Kit La Touche Date: Fri, 8 Nov 2024 22:08:02 -0500 Subject: Remove stray reference to "hi" --- ui/service-worker.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'ui/service-worker.js') diff --git a/ui/service-worker.js b/ui/service-worker.js index 033c8d9..fc619d9 100644 --- a/ui/service-worker.js +++ b/ui/service-worker.js @@ -7,7 +7,7 @@ // Only Safari, Edge, Chrome can run it at the moment, because only they // support modules in service workers. // -// That's okay! If you run `tools/run` with HI_DEV unset, you will get the +// That's okay! If you run `tools/run` with PILCROW_DEV unset, you will get the // bundled version, and can work on it. Or just use Safari. import { build, files, version } from '$service-worker'; -- cgit v1.2.3 From dfca11aceffaafc105509a2800d70eb9c9bed2e7 Mon Sep 17 00:00:00 2001 From: Kit La Touche Date: Mon, 11 Nov 2024 13:51:42 -0500 Subject: Actually return things in the cache, from the cache Service worker basics I guess. --- ui/service-worker.js | 13 +++++++++++++ 1 file changed, 13 insertions(+) (limited to 'ui/service-worker.js') diff --git a/ui/service-worker.js b/ui/service-worker.js index fc619d9..e2143b3 100644 --- a/ui/service-worker.js +++ b/ui/service-worker.js @@ -39,3 +39,16 @@ self.addEventListener('activate', (event) => { event.waitUntil(deleteOldCaches()); }); + +// The simplest possible use of the caches above: +async function cacheFirst(request) { + const responseFromCache = await caches.match(request); + if (responseFromCache) { + return responseFromCache; + } + return fetch(request); +}; + +self.addEventListener("fetch", (event) => { + event.respondWith(cacheFirst(event.request)); +}); -- cgit v1.2.3