web.dev LIVE has kicked off! Join us now for our digital event from June 30th to July 2nd at web.dev/live

Class: BroadcastCacheUpdate

Constructor

BroadcastCacheUpdate

new BroadcastCacheUpdate(options)

Construct a BroadcastCacheUpdate instance with a specific channelName to broadcast messages on

Parameter

options

Object

Values in options have the following properties:

Parameter

headersToCheck

Optional

Array of string

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

generatePayload

Optional

string

A function whose return value will be used as the payload field in any cache update messages sent to the window clients.

Method

notifyIfUpdated

async

notifyIfUpdated(options) returns Promise

Compares two Responses and sends a message (via postMessage()) to all window clients if the responses differ (note: neither of the Responses can be opaque).

The message that's posted has the following format (where payload can be customized via the generatePayload option the instance is created with):

{
  type: 'CACHE_UPDATED',
  meta: 'workbox-broadcast-update',
  payload: {
    cacheName: 'the-cache-name',
    updatedURL: 'https://example.com/'
  }
}

Parameter

options

Object

Values in options have the following properties:

Parameter

oldResponse

Optional

Response

Cached response to compare.

newResponse

Response

Possibly updated response to compare.

request

Request

The request.

cacheName

string

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

event

Optional

Event

event An optional event that triggered this possible cache update.

Returns

Promise Resolves once the update is sent.