CacheExpiration
First added
Workbox
About
Allows you to expires cached responses based on age or maximum number of entries.
Options
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 callingdelete()on the cache.
Methods and fields
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.
Usage
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();