From d13b0b3dafb60f32481256f598615444befd714a Mon Sep 17 00:00:00 2001 From: Joshua Higgins Date: Thu, 19 Nov 2026 18:47:06 -0500 Subject: [PATCH] fix(web): show downloaded cover fallbacks offline - Serve saved artist covers and cache the missing-cover placeholder for offline use --- apps/web/public/downloads-sw.js | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/apps/web/public/downloads-sw.js b/apps/web/public/downloads-sw.js index 888e0c5..cea9d79 100644 --- a/apps/web/public/downloads-sw.js +++ b/apps/web/public/downloads-sw.js @@ -1,10 +1,18 @@ // Serves covers of downloaded music when the network fails, so they show -// offline. Everything else, and every request that succeeds online, passes -// through untouched. Covers are saved by lib/downloads.ts. +// offline; any other cover falls back to the placeholder the server would send +// for a missing cover. Everything else, and every request that succeeds +// online, passes through untouched. Covers are saved by lib/downloads.ts. const COVER_CACHE = "otunes-download-covers" -const COVER_PATH = /^\/api\/(album|playlist)\/[^/]+\/cover$/ +const PLACEHOLDER_CACHE = "otunes-placeholders" +const PLACEHOLDER_PATH = "/placeholder-album.svg" +const COVER_PATH = /^\/api\/(album|playlist|artist)\/[^/]+\/cover$/ -self.addEventListener("install", () => self.skipWaiting()) +self.addEventListener("install", (event) => { + self.skipWaiting() + event.waitUntil( + caches.open(PLACEHOLDER_CACHE).then((cache) => cache.add(PLACEHOLDER_PATH)) + ) +}) self.addEventListener("activate", (event) => event.waitUntil(self.clients.claim()) ) @@ -19,9 +27,11 @@ function coverKey(url) { return COVER_PATH.test(url.pathname) ? url.pathname : null } +/** The saved cover, else the placeholder. */ async function savedCover(key) { - const cache = await caches.open(COVER_CACHE) - return cache.match(key) + const saved = await (await caches.open(COVER_CACHE)).match(key) + if (saved) return saved + return (await caches.open(PLACEHOLDER_CACHE)).match(PLACEHOLDER_PATH) } self.addEventListener("fetch", (event) => {