From 5b3ae24fa19989f8a8bbaadaaaedec11d85dee13 Mon Sep 17 00:00:00 2001 From: Owen Jacobson Date: Wed, 26 Nov 2025 17:55:44 -0500 Subject: Remove mostly-unsued fetch handler. This was added as part of the original service worker spike, without much consideration for design goals or correctness, and while it _works_, it doesn't meet any specific needs. We can get most of the same behaviour by letting the browser handle fetches directly. The main thing we lose is offline rendering of the Pilcrow UI, but that only worked partially and only by accident. We should build that from the ground up. --- ui/service-worker.js | 50 -------------------------------------------------- 1 file changed, 50 deletions(-) diff --git a/ui/service-worker.js b/ui/service-worker.js index cb32d0d..2917ae7 100644 --- a/ui/service-worker.js +++ b/ui/service-worker.js @@ -3,56 +3,6 @@ /// /// -// 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 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'; - -// 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()); -}); - -// 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)); -}); - self.addEventListener('push', (event) => { event.waitUntil( self.registration.showNotification('Test notification', { -- cgit v1.2.3 From 357cf6e7306d9090194c3304612a021b6e9e9df0 Mon Sep 17 00:00:00 2001 From: Owen Jacobson Date: Fri, 28 Nov 2025 16:54:41 -0500 Subject: Update service workers while the application is open. The default behaviour of a service worker is to check for updates any time the site associated with the worker is visited, but to only activate the new worker once no clients are open for that site. That effectively means that you need to open the site, close the site, and then open the site again to use new service worker capabilities, which is counterintuitive at best. I believe that the spec works this way for good reasons: probably because it means that the running service worker never changes out from underneath of a page while it's open, and only changes _between_ visits to a site. That's a good default. However, it does make testing service worker changes more difficult, as you need to restart the client application twice to see the results (or, at least, restart it once and then exit it). Since most of what we intend to use a service worker for mediates live app behaviour, it will be convenient to see those effects immediately when the service worker changes. --- ui/service-worker.js | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/ui/service-worker.js b/ui/service-worker.js index 2917ae7..446fbc8 100644 --- a/ui/service-worker.js +++ b/ui/service-worker.js @@ -3,6 +3,14 @@ /// /// +self.addEventListener('install', (event) => { + self.skipWaiting(); +}); + +self.addEventListener('activate', (event) => { + event.waitUntil(self.clients.claim()); +}); + self.addEventListener('push', (event) => { event.waitUntil( self.registration.showNotification('Test notification', { -- cgit v1.2.3