diff options
| author | Fuwn <[email protected]> | 2025-01-11 16:28:27 -0800 |
|---|---|---|
| committer | Fuwn <[email protected]> | 2025-01-11 16:28:27 -0800 |
| commit | cf04f824c34efa32a453a7324929d66451fbe649 (patch) | |
| tree | af0eccdcb7f2a2879624966b7b1c70ae40ebb459 | |
| parent | fix(Wrapped): spelling of exclude (diff) | |
| download | due.moe-cf04f824c34efa32a453a7324929d66451fbe649.tar.xz due.moe-cf04f824c34efa32a453a7324929d66451fbe649.zip | |
docs(service-worker): clean up comments
| -rw-r--r-- | src/service-worker.ts | 24 |
1 files changed, 4 insertions, 20 deletions
diff --git a/src/service-worker.ts b/src/service-worker.ts index 8f96f620..2aa76a3f 100644 --- a/src/service-worker.ts +++ b/src/service-worker.ts @@ -8,17 +8,10 @@ import { database } from './lib/Database/IDB/user'; import { notifications } from './lib/Data/AniList/notifications'; const sw = self as unknown as ServiceWorkerGlobalScope; - -// Create a unique cache name for this deployment const CACHE = `cache-${version}`; - -const ASSETS = [ - ...build, // the app itself - ...files // everything in `static` -]; +const ASSETS = [...build, ...files]; sw.addEventListener('install', (event: ExtendableEvent) => { - // Create a new cache and add all files to it async function addFilesToCache() { const cache = await caches.open(CACHE); await cache.addAll(ASSETS); @@ -28,7 +21,6 @@ sw.addEventListener('install', (event: ExtendableEvent) => { }); sw.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); @@ -39,7 +31,6 @@ sw.addEventListener('activate', (event) => { }); sw.addEventListener('fetch', (event) => { - // ignore POST requests etc if (event.request.method !== 'GET') return; const url = new URL(event.request.url).origin; @@ -51,7 +42,6 @@ sw.addEventListener('fetch', (event) => { const url = new URL(event.request.url); const cache = await caches.open(CACHE); - // `build`/`files` can always be served from the cache if (ASSETS.includes(url.pathname)) { const response = await cache.match(url.pathname); @@ -60,13 +50,9 @@ sw.addEventListener('fetch', (event) => { } } - // for everything else, try the network first, but - // fall back to the cache if we're offline try { const response = await fetch(event.request); - // if we're offline, fetch can return a value that is not a Response - // instead of throwing - and we can't pass this non-Response to respondWith if (!(response instanceof Response)) { throw new Error('invalid response from fetch'); } @@ -83,8 +69,6 @@ sw.addEventListener('fetch', (event) => { return response; } - // if there's no cache, then just error out - // as there is nothing we can do to respond to this request throw err; } } @@ -111,13 +95,13 @@ sw.addEventListener('push', async (event: PushEvent) => { recentNotifications.length > 0 && (recentNotifications[0].id > (user.lastNotificationID as number) || new Date(recentNotifications[0].createdAt * 1000).getTime() + 30000 > - new Date().getTime()) + new Date().getTime()) ) { await database.users.update(user.id, { lastNotificationID: recentNotifications[0].id }); await sw.registration.showNotification('due.moe', { body: `${recentNotifications[0].user.name}${recentNotifications[0].context}`, icon: recentNotifications[0].user.avatar.large || '/favicon-196x196.png', - // Ref. https://stackoverflow.com/a/50805868/14452787 + // https://stackoverflow.com/a/50805868/14452787 tag: 'notification-1' }); @@ -139,7 +123,7 @@ sw.addEventListener('push', async (event: PushEvent) => { tag: 'notification-1' }); - // Ref. https://github.com/firebase/quickstart-js/issues/126#issuecomment-504081087 + // https://github.com/firebase/quickstart-js/issues/126#issuecomment-504081087 await new Promise((resolve) => { (resolve as unknown as () => void)(); setTimeout( |