Constructor

CacheExpiration

new CacheExpiration(input)

Creates a new CacheExpiration instance, which is used to remove entries from a Cache once certain criteria—max number of entries, age of entry, or both—is met.

Parameter

input

Object

Example

const cacheExpiration = new workbox.cacheExpiration.CacheExpiration({
  maxEntries: 2,
  maxAgeSeconds: 10,
});

Methods

expireEntries

async

expireEntries(input) returns Promise

Expires entries based on the the maximum age and the maximum number of entries defined in the constructor.

To avoid concurrency issues, calls to this method when it's already running will result in the call begin re-run after the current execution has finished.

Parameter

input

Object

Returns

Promise Resolves when the cache expiration has been performed. If the function is currently executing the Promise will resolve immediately.

Example

// Assume that entries have been added to 'example-cache-name', and that
// updateTimestamp() was called after each entry was added.
cacheExpiration.expireEntries({
  cacheName: 'example-cache-name'
});

isResponseFresh

isResponseFresh(input) returns boolean

Checks whether a Response is "fresh", based on the Response's Date header and the maxAgeSeconds parameter passed into the constructor.

The general approach is to default to fresh unless proven otherwise.

If maxAgeSeconds or the Date header is not set then it will default to returning true, i.e. the response is still fresh and should be used.

Parameter

input

Object

Returns

boolean Either true if the response is fresh, or false if the Response is older than maxAgeSeconds and should no longer be used.

Example

expirationPlugin.isResponseFresh({
  cachedResponse: responseFromCache
});

updateTimestamp

async

updateTimestamp(input)

Updates the timestamp stored in IndexedDB for url to be equal to now.

When using this class directly (i.e. not via CacheExpirationPlugin), it's your responsibility to call updateTimestap() each time an entry is put into the cache. Otherwise, the expireEntries() method will not know which entries to remove.

Parameter

input

Object

Example

expirationPlugin.updateTimestamp({
  cacheName: 'example-cache-name',
  url: '/example-url'
});