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

Values in input have the following properties:

Parameter

maxEntries

Optional

Number

The maximum number of entries to cache. Entries will be expired using a least recently used (LRU) policy once the cache reaches this threshold.

maxAgeSeconds

Optional

Number

The maximum age of an entry before it's treated as staled and removed.

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

Values in input have the following properties:

Parameter

cacheName

string

Name of the cache to review and expire entries for.

now

Optional

Number

A timestamp to treat as now. This is largely only useful for testing purposes.

Defaults to the current time.

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

Values in input have the following properties:

Parameter

cacheName

string

Name of the cache the responses belong to.

cachedResponse

Response

The Response object that's been read from a cache and whose freshness should be checked.

now

Optional

Number

A timestamp.

Defaults to the current time.

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

Values in input have the following properties:

Parameter

cacheName

string

Name of the cache the Responses belong to.

url

string

The URL for the entry to update. The hash portion of the URL will be ignored.

now

Optional

Number

A timestamp. Defaults to the current time.

Example

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