Attic is a cozy space with lofty ambitions. attic.social
11
fork

Configure Feed

Select the types of activity you want to include in your feed.

basic service worker

+55
+55
src/service-worker.ts
··· 1 + // https://svelte.dev/docs/kit/service-workers 2 + // Disables access to DOM typings like `HTMLElement` which are not available 3 + // inside a service worker and instantiates the correct globals 4 + /// <reference no-default-lib="true" /> 5 + /// <reference lib="esnext" /> 6 + /// <reference lib="webworker" /> 7 + // Ensures that the `$service-worker` import has proper type definitions 8 + /// <reference types="@sveltejs/kit" /> 9 + 10 + import { build, files, version } from "$service-worker"; 11 + 12 + const self = globalThis.self as unknown as ServiceWorkerGlobalScope; 13 + 14 + const CACHE = `cache-${version}`; 15 + 16 + // const ASSETS = [...files, ...build]; 17 + const ASSETS = [...build]; 18 + 19 + self.addEventListener("install", (event) => { 20 + self.skipWaiting(); 21 + const openCache = async () => { 22 + const cache = await caches.open(CACHE); 23 + await cache.addAll(ASSETS); 24 + }; 25 + event.waitUntil(openCache()); 26 + }); 27 + 28 + self.addEventListener("activate", (event) => { 29 + event.waitUntil(self.clients.claim()); 30 + const clearCaches = async () => { 31 + for (const key of await caches.keys()) { 32 + if (key !== CACHE) { 33 + await caches.delete(key); 34 + } 35 + } 36 + }; 37 + event.waitUntil(clearCaches()); 38 + }); 39 + 40 + self.addEventListener("fetch", (event) => { 41 + const { method } = event.request; 42 + if (method !== "GET") return; 43 + 44 + const respond = async () => { 45 + const url = new URL(event.request.url); 46 + const cache = await caches.open(CACHE); 47 + if (ASSETS.includes(url.pathname)) { 48 + const response = await cache.match(url.pathname); 49 + if (response) return response; 50 + } 51 + return fetch(event.request); 52 + }; 53 + 54 + event.respondWith(respond()); 55 + });