mirror of
https://github.com/screentinker/screentinker.git
synced 2026-05-15 07:32:23 -06:00
- 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>
54 lines
1.6 KiB
JavaScript
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));
|
|
});
|