fix: improve image cache resilience for offline viewing

- Clean up stale metadata when Cache API doesn't have cached data
- Handle online/offline state properly in image loading
- Show original URL when online, blob URL from cache when offline
- Prevent cache misses when browser clears Cache API on hard reload
This commit is contained in:
Gigi
2025-10-09 18:15:30 +01:00
parent 60975b449d
commit b20a67d4d0
2 changed files with 53 additions and 42 deletions

View File

@@ -162,6 +162,13 @@ async function getCachedImageUrl(url: string): Promise<string | null> {
const response = await cache.match(url)
if (!response) {
// Cache miss - clean up stale metadata if it exists
const metadata = getMetadata()
if (metadata[url]) {
delete metadata[url]
saveMetadata(metadata)
console.log('🧹 Cleaned up stale cache metadata for:', url.substring(0, 50))
}
return null
}
@@ -175,7 +182,8 @@ async function getCachedImageUrl(url: string): Promise<string | null> {
// Convert response to blob URL
const blob = await response.blob()
return URL.createObjectURL(blob)
} catch {
} catch (err) {
console.warn('Failed to load from cache:', err)
return null
}
}