feat: Implement HLS video segment preloading and caching using a service worker.

This commit is contained in:
kuekhaoyang
2025-11-23 13:01:08 +08:00
parent 5e9af1b89e
commit 1e31a842e9
11 changed files with 435 additions and 2 deletions
+47
View File
@@ -0,0 +1,47 @@
/**
* Cache Management Utility
* Handles clearing segments from cache
*/
const CACHE_NAME = 'video-cache-v1';
export async function clearSegmentsForUrl(url: string): Promise<void> {
if (!('caches' in window)) return;
try {
const cache = await caches.open(CACHE_NAME);
const requests = await cache.keys();
// Parse the base URL to match against
const baseUrl = url.substring(0, url.lastIndexOf('/') + 1);
let deletedCount = 0;
for (const request of requests) {
if (request.url.startsWith(baseUrl)) {
await cache.delete(request);
deletedCount++;
}
}
if (deletedCount > 0) {
console.log(`[CacheManager] Deleted ${deletedCount} segments for ${url}`);
}
} catch (error) {
console.error('[CacheManager] Error clearing cache:', error);
}
}
export async function clearAllCache(): Promise<void> {
if (!('caches' in window)) return;
try {
const deleted = await caches.delete(CACHE_NAME);
if (deleted) {
console.log('[CacheManager] Cleared all cached segments');
// Recreate the cache for future use
await caches.open(CACHE_NAME);
}
} catch (error) {
console.error('[CacheManager] Error clearing all cache:', error);
}
}