mirror of
https://github.com/immich-app/immich.git
synced 2025-12-26 17:25:00 +03:00
47 lines
850 B
TypeScript
47 lines
850 B
TypeScript
import { version } from '$service-worker';
|
|
|
|
const CACHE = `cache-${version}`;
|
|
|
|
let _cache: Cache | undefined;
|
|
const getCache = async () => {
|
|
if (_cache) {
|
|
return _cache;
|
|
}
|
|
_cache = await caches.open(CACHE);
|
|
return _cache;
|
|
};
|
|
|
|
export const get = async (key: string) => {
|
|
const cache = await getCache();
|
|
if (!cache) {
|
|
return;
|
|
}
|
|
|
|
return cache.match(key);
|
|
};
|
|
|
|
export const put = async (key: string, response: Response) => {
|
|
if (response.status !== 200) {
|
|
return;
|
|
}
|
|
|
|
const cache = await getCache();
|
|
if (!cache) {
|
|
return;
|
|
}
|
|
|
|
try {
|
|
await cache.put(key, response.clone());
|
|
} catch (error) {
|
|
console.error('Ignoring error during cache put', error);
|
|
}
|
|
};
|
|
|
|
export const prune = async () => {
|
|
for (const key of await caches.keys()) {
|
|
if (key !== CACHE) {
|
|
await caches.delete(key);
|
|
}
|
|
}
|
|
};
|