A service worker helper library that expires cached responses based on age or maximum number of entries.
—
Workbox plugin for seamless integration with caching strategies, providing automatic expiration based on age and entry limits during cache operations.
Workbox plugin that automatically handles expiration during cache operations.
/**
* Plugin for workbox-strategy to regularly enforce limits on age and/or number of cached requests.
* Can only be used with workbox-strategy instances that have a custom cacheName property set.
* Cannot be used to expire entries in strategies that use the default runtime cache name.
*/
class ExpirationPlugin {
/**
* Constructs a new ExpirationPlugin. At least one of maxEntries or maxAgeSeconds must be provided.
* @param config - Plugin configuration options
*/
constructor(config: ExpirationPluginOptions = {});
/**
* Deletes all underlying Cache instances associated with this plugin and
* removes metadata from IndexedDB. Preferable to calling caches.delete() directly
* when using cache expiration as it ensures IndexedDB cleanup.
*/
deleteCacheAndMetadata(): Promise<void>;
// Plugin lifecycle methods (automatically called by Workbox strategies)
cachedResponseWillBeUsed?: WorkboxPlugin['cachedResponseWillBeUsed'];
cacheDidUpdate?: WorkboxPlugin['cacheDidUpdate'];
}
interface ExpirationPluginOptions {
/** Maximum number of entries to cache. Entries used least recently will be removed first. */
maxEntries?: number;
/** Maximum age of an entry in seconds before it's treated as stale and removed. */
maxAgeSeconds?: number;
/** CacheQueryOptions that will be used when calling delete() on the cache. */
matchOptions?: CacheQueryOptions;
/** Whether to opt this cache into automatic deletion if available storage quota has been exceeded. */
purgeOnQuotaError?: boolean;
}Usage Examples:
import { ExpirationPlugin } from "workbox-expiration";
import { StaleWhileRevalidate, CacheFirst } from "workbox-strategies";
// Basic usage with a caching strategy
const strategy = new StaleWhileRevalidate({
cacheName: "api-cache",
plugins: [
new ExpirationPlugin({
maxEntries: 50,
maxAgeSeconds: 60 * 60 * 24, // 24 hours
}),
],
});
// With automatic quota error cleanup
const limitedStrategy = new CacheFirst({
cacheName: "images",
plugins: [
new ExpirationPlugin({
maxEntries: 100,
maxAgeSeconds: 60 * 60 * 24 * 30, // 30 days
purgeOnQuotaError: true,
}),
],
});
// Custom cache query options
const customStrategy = new StaleWhileRevalidate({
cacheName: "custom-cache",
plugins: [
new ExpirationPlugin({
maxEntries: 25,
matchOptions: {
ignoreSearch: true,
ignoreVary: true,
},
}),
],
});
// Manual cleanup of all caches managed by plugin
const plugin = new ExpirationPlugin({
maxEntries: 50,
maxAgeSeconds: 60 * 60,
});
// Later, when needed
await plugin.deleteCacheAndMetadata();The plugin automatically integrates with Workbox strategy lifecycle:
cachedResponseWillBeUsed:
maxAgeSeconds is configurednull for expired responses (causing strategy to fetch fresh)cacheDidUpdate:
Constructor Errors:
WorkboxError with code max-entries-or-age-required if neither maxEntries nor maxAgeSeconds is providedRuntime Errors:
WorkboxError with code expire-custom-caches-only if used with the default runtime cache nameCacheExpiration instances for each cacheregisterQuotaErrorCallback when purgeOnQuotaError is enableddontWaitFor pattern to avoid blocking cache operations during expirationevent.waitUntil() when availableWhen purgeOnQuotaError is enabled:
deleteCacheAndMetadata() when storage quota is exceededInstall with Tessl CLI
npx tessl i tessl/npm-workbox-expiration