diff --git a/src/utils/pwaUtils.ts b/src/utils/pwaUtils.ts index 8fcd0e95..3672748d 100644 --- a/src/utils/pwaUtils.ts +++ b/src/utils/pwaUtils.ts @@ -49,6 +49,15 @@ export async function promptPWAInstall(installEvent: any): Promise { * Clears outdated caches for storage optimization on mobile */ export async function clearOutdatedCaches(cachePrefix = 'teachlink-cache-'): Promise { - if (!('caches' in window)) return; - // Typically executed inside the SW, but can be manually triggered if needed from client side + if (typeof globalThis === 'undefined' || !('caches' in globalThis)) return; + + try { + const cacheNames = await globalThis.caches.keys(); + const matchingCaches = cacheNames.filter((cacheName) => cacheName.startsWith(cachePrefix)); + + await Promise.all(matchingCaches.map((cacheName) => globalThis.caches.delete(cacheName))); + logger.info('Cleared outdated caches', { cachePrefix, deletedCount: matchingCaches.length }); + } catch (error) { + logger.error('Failed to clear outdated caches', { error, cachePrefix }); + } }