diff --git a/app/util/resources.js b/app/util/resources.js index 7e53a76..f27cc83 100644 --- a/app/util/resources.js +++ b/app/util/resources.js @@ -1,3 +1,9 @@ +import {LRUCache} from 'lru-cache'; + +const cache = new LRUCache({ + max: 128, +}); + export async function computeMissing(current, manifest) { const missing = []; for (const path in manifest) { @@ -58,12 +64,15 @@ export async function get() { export async function set(resources) { const {set} = await import('idb-keyval'); + cache.clear(); await set('$$silphius_resources', resources); } export async function readAsset(path) { - const {pathname} = new URL(path, 'http://example.org'); - const resourcePath = pathname.slice('/resources/'.length); - const resources = await get(); - return resources[resourcePath]?.asset; + if (!cache.has(path)) { + const {pathname} = new URL(path, 'http://example.org'); + const resourcePath = pathname.slice('/resources/'.length); + cache.set(path, get().then((resources) => resources[resourcePath]?.asset)); + } + return cache.get(path); }