From d936fa49065d8d1f72ebf8d316ed16a093b0045b Mon Sep 17 00:00:00 2001 From: Offroaders123 <65947371+Offroaders123@users.noreply.github.com> Date: Sun, 30 Jul 2023 02:29:45 -0700 Subject: [PATCH] Dovetail Service Worker Brought over the improved Service Worker I wrote for Dovetail and STE. Async-await is so cool bro! And functional programming, rather. --- public/service-worker.js | 76 +++++++++++++++++++++++++++------------- src/Flatlands.ts | 2 +- 2 files changed, 53 insertions(+), 25 deletions(-) diff --git a/public/service-worker.js b/public/service-worker.js index 6f4d334..d33e653 100644 --- a/public/service-worker.js +++ b/public/service-worker.js @@ -3,35 +3,63 @@ var self = /** @type { ServiceWorkerGlobalScope } */ (/** @type { unknown } */ (globalThis)); -const Flatlands = { - version: "Flatlands v0.14.1", - cache: true -}; +const NAME = "Flatlands"; +const VERSION = "v0.14.2"; +const CACHE_NAME = /** @type { const } */ (`${NAME} ${VERSION}`); self.addEventListener("activate",event => { - event.waitUntil(caches.keys().then(async keys => { - const results = keys.map(async key => { - if (key.startsWith("Flatlands") && key !== Flatlands.version){ - return caches.delete(key); - } - }); - await Promise.all(results); - await self.clients.claim(); - })); + event.waitUntil(removeOutdatedVersions()); +}); + +self.addEventListener("fetch",event => { + event.respondWith(matchRequest(event.request)); }); -self.addEventListener("fetch",async event => { - event.respondWith((async () => { - const cached = await caches.match(event.request); - if (cached !== undefined) return cached; +/** + * Clears out old versions of the app from Cache Storage. + * + * @returns { Promise } +*/ +async function removeOutdatedVersions(){ + const keys = await caches.keys(); - const fetched = await fetch(event.request); + await Promise.all(keys.map(async key => { + const isOutdatedVersion = key.startsWith(NAME) && key !== CACHE_NAME; - if (Flatlands.cache){ - const cache = await caches.open(Flatlands.version); - await cache.put(event.request,fetched.clone()); + if (isOutdatedVersion){ + await caches.delete(key); } + })); + + await clients.claim(); +} + +/** + * Matches a network request with it's cached counterpart from Cache Storage. + * + * If it hasn't been cached yet, it will fetch the network for a response, cache a clone, then return the response. + * + * @param { Request } request + * @returns { Promise } +*/ +async function matchRequest(request){ + let response = await caches.match(request); + if (response !== undefined) return response; + + response = await fetch(request); + await cacheRequest(request,response); + + return response; +} - return fetched; - })()); -}); \ No newline at end of file +/** + * Adds a network request and response to Cache Storage. + * + * @param { Request } request + * @param { Response } response + * @return { Promise } +*/ +async function cacheRequest(request,response){ + const cache = await caches.open(CACHE_NAME); + await cache.put(request,response.clone()); +} \ No newline at end of file diff --git a/src/Flatlands.ts b/src/Flatlands.ts index 6efa512..ca8aa06 100644 --- a/src/Flatlands.ts +++ b/src/Flatlands.ts @@ -1,5 +1,5 @@ export default class Flatlands { - static version = "v0.14.1"; + static version = "v0.14.2"; static environment = { get touchDevice(): boolean {