Cache images via service worker
Improves the image request logic by making it possible to check if an image is cached. In case an image is cached, it does not need to be put into the image queue because it won't be sent to the server.
This commit is contained in:
40
src/lib/service-worker/ImageCache.ts
Normal file
40
src/lib/service-worker/ImageCache.ts
Normal file
@@ -0,0 +1,40 @@
|
||||
/*
|
||||
* Copyright (C) Contributors to the Suwayomi project
|
||||
*
|
||||
* This Source Code Form is subject to the terms of the Mozilla Public
|
||||
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
|
||||
*/
|
||||
|
||||
const CACHE_NAME = 'image-cache';
|
||||
|
||||
export class ImageCache {
|
||||
static async has(url: string): Promise<boolean> {
|
||||
try {
|
||||
const cache = await caches.open(CACHE_NAME);
|
||||
const response = await cache.match(url, { ignoreVary: true });
|
||||
|
||||
return response !== undefined;
|
||||
} catch (error) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
static async get(url: string): Promise<Response | null> {
|
||||
try {
|
||||
const cache = await caches.open(CACHE_NAME);
|
||||
const response = await cache.match(url);
|
||||
|
||||
return response || null;
|
||||
} catch (error) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
static async clear(): Promise<void> {
|
||||
const cache = await caches.open(CACHE_NAME);
|
||||
const keys = await cache.keys();
|
||||
|
||||
await Promise.all(keys.map((key) => cache.delete(key)));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user