From 64e4c0f43c9196d07063266f4be2f5b9dd4c1e32 Mon Sep 17 00:00:00 2001 From: Julien Maille Date: Fri, 26 Dec 2025 00:47:11 +0100 Subject: [PATCH] Fix CORS issue when fetching cover art for downloads --- js/downloads.js | 29 +++++++++++++++++++++++++++-- sw.js | 3 +++ 2 files changed, 30 insertions(+), 2 deletions(-) diff --git a/js/downloads.js b/js/downloads.js index 10ac3d4..25c5eb1 100644 --- a/js/downloads.js +++ b/js/downloads.js @@ -13,16 +13,41 @@ async function getCoverBlob(api, coverId) { if (!coverId) return null; if (coverCache.has(coverId)) return coverCache.get(coverId); + const fetchWithProxy = async (url) => { + try { + const proxyUrl = `https://corsproxy.io/?${encodeURIComponent(url)}`; + const response = await fetch(proxyUrl); + if (response.ok) return await response.blob(); + } catch (e) { + console.warn('Proxy fetch failed:', e); + } + return null; + }; + try { const url = api.getCoverUrl(coverId, '1280'); + // Try direct fetch first const response = await fetch(url); if (response.ok) { const blob = await response.blob(); coverCache.set(coverId, blob); return blob; + } else { + // If direct fetch fails (e.g. 404 from SW due to CORS), try proxy + const blob = await fetchWithProxy(url); + if (blob) { + coverCache.set(coverId, blob); + return blob; + } + } + } catch (e) { + // Network error (CORS rejection not handled by SW), try proxy + const url = api.getCoverUrl(coverId, '1280'); + const blob = await fetchWithProxy(url); + if (blob) { + coverCache.set(coverId, blob); + return blob; } - } catch (error) { - console.warn('Cover fetch failed:', error); } return null; } diff --git a/sw.js b/sw.js index f4882e1..a6fc714 100644 --- a/sw.js +++ b/sw.js @@ -45,6 +45,9 @@ self.addEventListener('fetch', event => { cache.put(event.request, networkResponse.clone()); } return networkResponse; + }).catch(() => { + // If fetch fails (e.g. CORS), return null/error so client handles it + return new Response(null, { status: 404, statusText: 'Image fetch failed' }); }); }); })