screentinker/server/player/sw.js
ScreenTinker d18045a386 Fix web player single-video loop and service worker cache errors
- Loop single-video playlists natively instead of destroying/recreating the element
- Skip caching HTTP 206 partial responses in service worker (video range requests)
- Bump service worker cache version to invalidate old cache

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-04-08 12:58:43 -05:00

54 lines
1.6 KiB
JavaScript

const CACHE_NAME = 'rd-player-v2';
const STATIC_ASSETS = ['/player/', '/player/index.html', '/socket.io/socket.io.js'];
// Install: cache static assets
self.addEventListener('install', (event) => {
event.waitUntil(
caches.open(CACHE_NAME).then(cache => cache.addAll(STATIC_ASSETS))
);
self.skipWaiting();
});
// Activate: clean old caches
self.addEventListener('activate', (event) => {
event.waitUntil(
caches.keys().then(keys => Promise.all(
keys.filter(k => k !== CACHE_NAME).map(k => caches.delete(k))
))
);
self.clients.claim();
});
// Fetch: cache content files for offline playback
self.addEventListener('fetch', (event) => {
const url = new URL(event.request.url);
// Cache content files (videos, images) on first fetch
if (url.pathname.startsWith('/uploads/content/')) {
event.respondWith(
caches.match(event.request).then(cached => {
if (cached) return cached;
return fetch(event.request).then(response => {
if (response.ok && response.status === 200) {
const clone = response.clone();
caches.open(CACHE_NAME).then(cache => cache.put(event.request, clone));
}
return response;
}).catch(() => new Response('Offline', { status: 503 }));
})
);
return;
}
// For static assets, try cache first
if (STATIC_ASSETS.some(a => url.pathname === a || url.pathname.endsWith(a))) {
event.respondWith(
caches.match(event.request).then(cached => cached || fetch(event.request))
);
return;
}
// Everything else: network only
event.respondWith(fetch(event.request));
});