diff --git a/src/lib/caching/cache-api.service.ts b/src/lib/caching/cache-api.service.ts index 2fdcfe9..750e539 100644 --- a/src/lib/caching/cache-api.service.ts +++ b/src/lib/caching/cache-api.service.ts @@ -12,7 +12,7 @@ export class CacheApiService { } get cachesAvailable(): boolean { - return !!window.caches; + return !!caches; } cacheRequest(request: HttpRequest, httpResponse: HttpResponse) { @@ -20,6 +20,10 @@ export class CacheApiService { return; } + if (!this.cachesAvailable) { + return; + } + const url = this._buildUrl(request); for (const dynCache of this._dynamicCaches) { for (const cacheUrl of dynCache.urls) { @@ -35,6 +39,10 @@ export class CacheApiService { async wipeCaches(logoutDependant = false) { // console.log('[CACHE-API] delete app level cache '); + if (!this.cachesAvailable) { + return; + } + await caches.delete(APP_LEVEL_CACHE); for (const cache of this._dynamicCaches) { @@ -234,6 +242,10 @@ export class CacheApiService { private async _handleCacheExpiration(dynCache: DynamicCache, now: number) { // console.log('[CACHE-API] checking cache expiration'); + if (!this.cachesAvailable) { + return; + } + const cache = await caches.open(dynCache.name); let keys = await cache.keys(); // removed expired; diff --git a/src/lib/caching/cache-utils.ts b/src/lib/caching/cache-utils.ts index 3afa506..dfc2d43 100644 --- a/src/lib/caching/cache-utils.ts +++ b/src/lib/caching/cache-utils.ts @@ -7,7 +7,7 @@ export const DYNAMIC_CACHES = new InjectionToken('dynamic-caches' export async function wipeAllCaches() { console.log('get caches keys'); - const keys = await caches.keys(); + const keys = (await caches?.keys()) ?? []; for (const cache of keys) { await wipeCache(cache); } @@ -15,10 +15,14 @@ export async function wipeAllCaches() { export function wipeCache(cacheName: string) { console.log('delete cache: ', cacheName); - return caches.delete(cacheName); + return caches?.delete(cacheName); } export async function wipeCacheEntry(cacheName: string, entry: string) { + if (!caches) { + return; + } + console.log('open cache: ', cacheName); const cache = await caches.open(cacheName); return cache.delete(entry, { ignoreSearch: false });