Oh yeah, we have Image Cache uses IndexedDB!
Pros:
- No localStorage 5Mb size limit
- Keep the cache as long as you want
- Better for some expensive action cache (download/alogrithm calculated)
Arguments:
- Abuse indexedDB
- Shoude use CacheStorage for caching
Thoughts:
- Developers grab the wheel
- Just for image/simple blob caching, CacheStorage only accessed by worker, it's complicated to use
npm i indexeddb-image-cache
import ImageCache from 'indexeddb-image-cache'
const BASE64 = 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAgAAAAIAQMAAAD+wSzIAAAABlBMVEX///+/v7+jQ3Y5AAAADklEQVQI12P4AIX8EAgALgAD/aNpbtEAAAAASUVORK5CYII';
const cache = new ImageCache({ version: 1 });
await cache.init();
await cache.putImage('trophy', BASE64);
// await cache.putBlob('blob', blobInstance)
// somewhere else
// it will return string by URL.createObjectURL()
// get blob easily by: let blob = await fetch(url).then(r => r.blob());
const image = await cache.getImage('trophy')
- More usage see image-cache.test.ts file
- Vite
- Vitest
- Typescript
It includes test examples using vite test
npm run build
npm run test
npm run coverage
npm pack
npm pack --dry-run
npm run lint
npm run lint-and-fix
npm run pretty
npm run clean-up