mirror of
https://github.com/immich-app/immich.git
synced 2025-12-29 17:25:00 +03:00
43 lines
749 B
TypeScript
43 lines
749 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;
|
|
}
|
|
|
|
cache.put(key, response.clone());
|
|
};
|
|
|
|
export const prune = async () => {
|
|
for (const key of await caches.keys()) {
|
|
if (key !== CACHE) {
|
|
await caches.delete(key);
|
|
}
|
|
}
|
|
};
|