Files
immich/web/src/service-worker/cache.ts

43 lines
749 B
TypeScript
Raw Normal View History

2025-08-25 12:52:57 -04:00
import { version } from '$service-worker';
const CACHE = `cache-${version}`;
2025-08-25 12:52:57 -04:00
let _cache: Cache | undefined;
const getCache = async () => {
if (_cache) {
return _cache;
}
2025-08-25 12:52:57 -04:00
_cache = await caches.open(CACHE);
return _cache;
};
2025-08-25 12:52:57 -04:00
export const get = async (key: string) => {
const cache = await getCache();
if (!cache) {
return;
}
2025-08-25 12:52:57 -04:00
return cache.match(key);
};
2025-08-25 12:52:57 -04:00
export const put = async (key: string, response: Response) => {
if (response.status !== 200) {
return;
}
2025-08-25 12:52:57 -04:00
const cache = await getCache();
if (!cache) {
return;
}
2025-08-25 12:52:57 -04:00
cache.put(key, response.clone());
};
2025-08-25 12:52:57 -04:00
export const prune = async () => {
for (const key of await caches.keys()) {
if (key !== CACHE) {
await caches.delete(key);
}
}
2025-08-25 12:52:57 -04:00
};