mirror of
https://github.com/KuekHaoYang/KVideo.git
synced 2026-08-16 09:13:42 +08:00
feat: Implement HLS video segment preloading and caching using a service worker.
This commit is contained in:
@@ -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);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user