Chrome Dev Summit is back! Visit goo.gle/cds2021 to secure your spot in workshops, office hours and learning lounges!

Class: PrecacheController

Constructor

PrecacheController

new PrecacheController(options)

Create a new PrecacheController.

Parameter

options

Optional

Object

Values in options have the following properties:

Parameter

cacheName

Optional

string

The cache to use for precaching.

plugins

Optional

string

Plugins to use when precaching as well as responding to fetch events for precached assets.

fallbackToNetwork

Optional

boolean

Whether to attempt to get the response from the network if there's a precache miss.

Property

strategy

module:workbox-precaching.PrecacheStrategy

The strategy created by this controller andused to cache assets and respond to fetch events.

Methods

activate

activate(event) returns Promise containing module:workbox-precaching.CleanupResult

Deletes assets that are no longer present in the current precache manifest. Call this method from the service worker activate event.

Note: this method calls event.waitUntil() for you, so you do not need to call it yourself in your event handlers.

Parameter

event

ExtendableEvent

Returns

Promise containing module:workbox-precaching.CleanupResult 

addToCacheList

addToCacheList(entries)

This method will add items to the precache list, removing duplicates and ensuring the information is valid.

Parameter

entries

Array of (module:workbox-precaching.PrecacheController.PrecacheEntry or string)

Array of entries to precache.

createHandlerBoundToURL

createHandlerBoundToURL(url) returns module:workbox-routing~handlerCallback

Returns a function that looks up url in the precache (taking into account revision information), and returns the corresponding Response.

Parameter

url

string

The precached URL which will be used to lookup the Response.

Returns

module:workbox-routing~handlerCallback 

getCachedURLs

getCachedURLs() returns Array of string

Returns a list of all the URLs that have been precached by the current service worker.

Returns

Array of string The precached URLs.

getCacheKeyForURL

getCacheKeyForURL(url) returns string

Returns the cache key used for storing a given URL. If that URL is unversioned, like `/index.html', then the cache key will be the original URL with a search parameter appended to it.

Parameter

url

string

A URL whose cache key you want to look up.

Returns

string The versioned URL that corresponds to a cache key for the original URL, or undefined if that URL isn't precached.

getURLsToCacheKeys

getURLsToCacheKeys() returns Object with string properties

Returns a mapping of a precached URL to the corresponding cache key, taking into account the revision information for the URL.

Returns

Object with string properties A URL to cache key mapping.

install

install(event) returns Promise containing module:workbox-precaching.InstallResult

Precaches new and updated assets. Call this method from the service worker install event.

Note: this method calls event.waitUntil() for you, so you do not need to call it yourself in your event handlers.

Parameter

event

ExtendableEvent

Returns

Promise containing module:workbox-precaching.InstallResult 

matchPrecache

async

matchPrecache(request) returns Promise containing (Response or undefined)

This acts as a drop-in replacement for cache.match() with the following differences:

  • It knows what the name of the precache is, and only checks in that cache.
  • It allows you to pass in an "original" URL without versioning parameters, and it will automatically look up the correct cache key for the currently active revision of that URL.

E.g., matchPrecache('index.html') will find the correct precached response for the currently active service worker, even if the actual cache key is '/index.html?__WB_REVISION__=1234abcd'.

Parameter

request

(string or Request)

The key (without revisioning parameters) to look up in the precache.

Returns

Promise containing (Response or undefined) 

precache

precache(entries)

Adds items to the precache list, removing any duplicates and stores the files in the "precache cache" when the service worker installs.

This method can be called multiple times.

Parameter

entries

Optional

Array of (Object or string)

Array of entries to precache.