check caches for undefined

This commit is contained in:
Dan Percic 2023-02-27 15:52:03 +02:00
parent 2761140489
commit 7ca5db9cee
2 changed files with 19 additions and 3 deletions

View File

@ -12,7 +12,7 @@ export class CacheApiService {
}
get cachesAvailable(): boolean {
return !!window.caches;
return !!caches;
}
cacheRequest(request: HttpRequest<any>, httpResponse: HttpResponse<any>) {
@ -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;

View File

@ -7,7 +7,7 @@ export const DYNAMIC_CACHES = new InjectionToken<DynamicCaches>('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 });