Skip to content

Commit

Permalink
Dovetail Service Worker
Browse files Browse the repository at this point in the history
Brought over the improved Service Worker I wrote for Dovetail and STE. Async-await is so cool bro! And functional programming, rather.
  • Loading branch information
Offroaders123 committed Jul 30, 2023
1 parent eafc5a0 commit d936fa4
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 25 deletions.
76 changes: 52 additions & 24 deletions public/service-worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -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<void> }
*/
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<Response> }
*/
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;
})());
});
/**
* Adds a network request and response to Cache Storage.
*
* @param { Request } request
* @param { Response } response
* @return { Promise<void> }
*/
async function cacheRequest(request,response){
const cache = await caches.open(CACHE_NAME);
await cache.put(request,response.clone());
}
2 changes: 1 addition & 1 deletion src/Flatlands.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
export default class Flatlands {
static version = "v0.14.1";
static version = "v0.14.2";

static environment = {
get touchDevice(): boolean {
Expand Down

0 comments on commit d936fa4

Please sign in to comment.