docs-serwist-core-cache-expiration.md
Workbox
Allows you to expires cached responses based on age or maximum number of entries.
maxEntries — The maximum number of entries to cache. Entries used least recently will be removed as the maximum is reached.maxAgeSeconds — The maximum age of an entry before it’s treated as stale and removed.matchOptions — The CacheQueryOptions that will be used when calling delete() on the cache.async expireEntries() — Expires entries for the given cache and given criteria.async updateTimestamp(url) — Updates the timestamp for the given URL, allowing it to be correctly tracked by the class.async isURLExpired(url) — Checks if a URL has expired or not before it’s used.async delete() — Removes the IndexedDB used to keep track of cache expiration metadata.sw.ts
import { CacheExpiration } from "serwist";
const cacheName = "my-cache";
const expirationManager = new CacheExpiration(cacheName, {
maxAgeSeconds: 24 * 60 * 60,
maxEntries: 20,
});
const openCache = await caches.open(cacheName);
// Put the response into the cache.
await openCache.put(request, response);
// Update the timestamp of the request.
await expirationManager.updateTimestamp(request.url);
// Expire entries that have reached max age.
await expirationManager.expireEntries();