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:
1 file changed
+16
-6
@@ -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) => {
|
||||
|
||||
Reference in new issue
Block a user