fix(web): show downloaded cover fallbacks offline

- Serve saved artist covers and cache the missing-cover placeholder for offline use
This commit is contained in:
joshuafhiggins committed 2026-11-19 18:47:06 -05:00 Verified
1 parent 9c96142378
commit d13b0b3daf
1 file changed
+16 -6
+16 -6
View File
@@ -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) => {