Constructor

BroadcastCacheUpdate

new BroadcastCacheUpdate(input)

Dispatches cache update messages when a cached response has been updated. Messages will be dispatched on a broadcast channel with the name provided as the channelName parameter.

Parameter

input

Object

Values in input have the following properties:

Parameter

channelName

string

The name that will be used when creating the BroadcastChannel.

headersToCheck

Array of string

A list of headers that will be used to determine whether the responses differ.

Defaults to ['content-length', 'etag', 'last-modified'].

source

string

An attribution value that indicates where the update originated.

Defaults to workbox-broadcast-cache-update.

Example

<caption>Using BroadcastCacheUpdate to compare a cached and fetched
request.</caption>

const url = '/path/to/file';
const cacheName = 'my-runtime-cache';

const bcu = new workbox.broadcastCacheUpdate.BroadcastCacheUpdate({
  channelName: 'cache-updates'
});

caches.open(cacheName)
.then((openCache) => {
  return Promise.all([
    cache.match(url),
    fetch(url)
  ]);
})
.then((responses) => {
  const cachedResponse = responses[0];
  const fetchedResponse = responses[1];
  if (cachedResponse) {
    bcu.notifyIfUpdated({
      cacheName,
      cachedResponse,
      fetchedResponse
    });
  }
});

Method

notifyIfUpdated

notifyIfUpdated(input)

An explicit method to call from your own code to trigger the comparison of two Responses and send a message via the Broadcast Channel API if they differ.

Parameter

input

Object

The input object to this function.

Values in input have the following properties:

Parameter

first

Response

One of the responses to compare. This should not be an opaque response.

second

Response

The response to compare with. This should not be an opaque response.

cacheName

string

Name of the cache the responses belong to. This is included in the message posted on the broadcast channel.

url

string

The URL of the updates request.