Files
MeBox/web/public/artwork-cache-sw.js
T

74 lines
2.2 KiB
JavaScript
Vendored

const ARTWORK_CACHE = 'mediastationgo-artwork-v1'
const STRIP_QUERY_KEYS = ['token', 'profile_id', 'profile_pin_token']
function isArtworkRequest(url) {
if (url.origin !== self.location.origin) return false
if (url.pathname === '/api/img') return true
return url.pathname.startsWith('/api/cloud/play/')
}
function normalizedArtworkRequest(request) {
const url = new URL(request.url)
for (const key of STRIP_QUERY_KEYS) {
url.searchParams.delete(key)
}
return new Request(url.toString(), {
method: 'GET',
headers: {
Accept: request.headers.get('Accept') || 'image/avif,image/webp,image/apng,image/*,*/*;q=0.8',
},
credentials: 'same-origin',
mode: 'same-origin',
redirect: 'follow',
})
}
self.addEventListener('fetch', (event) => {
const request = event.request
if (request.method !== 'GET') return
const url = new URL(request.url)
if (!isArtworkRequest(url)) return
event.respondWith(cacheArtwork(request))
})
async function cacheArtwork(request) {
const cache = await caches.open(ARTWORK_CACHE)
const cacheKey = normalizedArtworkRequest(request)
const cached = await cache.match(cacheKey)
if (cached) return cached
const response = await fetch(request)
const contentType = response.headers.get('Content-Type') || ''
if (response.ok && contentType.toLowerCase().startsWith('image/')) {
await cache.put(cacheKey, response.clone())
await deleteOldArtworkVariants(cache, cacheKey)
}
return response
}
async function deleteOldArtworkVariants(cache, currentRequest) {
const currentURL = new URL(currentRequest.url)
const currentIdentity = artworkIdentity(currentURL)
if (!currentIdentity) return
const keys = await cache.keys()
await Promise.all(keys.map(async (key) => {
if (key.url === currentRequest.url) return
const keyURL = new URL(key.url)
if (artworkIdentity(keyURL) !== currentIdentity) return
await cache.delete(key)
}))
}
function artworkIdentity(url) {
if (url.pathname === '/api/img') {
return `${url.origin}${url.pathname}?url=${url.searchParams.get('url') || ''}`
}
if (url.pathname.startsWith('/api/cloud/play/')) {
return `${url.origin}${url.pathname}`
}
return ''
}