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

Class: Strategy

Constructor

Strategy

new Strategy(options)

Creates a new instance of the strategy and sets all documented option properties as public instance properties.

Note: if a custom strategy class extends the base Strategy class and does not need more than these properties, it does not need to define its own constructor.

Parameter

options

Optional

Object

Values in options have the following properties:

Parameter

cacheName

Optional

string

Cache name to store and retrieve requests. Defaults to the cache names provided by workbox-core.

plugins

Optional

Array of Object

Plugins to use in conjunction with this caching strategy.

fetchOptions

Optional

Object

Values passed along to the init of non-navigation fetch() requests made by this strategy.

matchOptions

Optional

Object

The CacheQueryOptions for any cache.match() or cache.put() calls made by this strategy.

Properties

cacheName

string

Cache name to store and retrieve requests. Defaults to the cache names provided by workbox-core.

fetchOptions

Object

Values passed along to the init of all fetch() requests made by this strategy.

matchOptions

Object

The CacheQueryOptions for any cache.match() or cache.put() calls made by this strategy.

plugins

Array of Object

The list Plugins used by this strategy.

Methods

_handle

abstract

_handle(request, handler) returns Promise containing Response

Classes extending the Strategy based class should implement this method, and leverage the handler arg to perform all fetching and cache logic, which will ensure all relevant cache, cache options, fetch options and plugins are used (per the current strategy instance).

Parameter

request

Request

handler

module:workbox-strategies.StrategyHandler

Returns

Promise containing Response 

handle

handle(options)

Perform a request strategy and returns a Promise that will resolve with a Response, invoking all relevant plugin callbacks.

When a strategy instance is registered with a Workbox route, this method is automatically called when the route matches.

Alternatively, this method can be used in a standalone FetchEvent listener by passing it to event.respondWith().

Parameter

options

(FetchEvent or Object)

A FetchEvent or an object with the properties listed below.

Values in options have the following properties:

Parameter

request

(Request or string)

A request to run this strategy for.

event

ExtendableEvent

The event associated with the request.

url

Optional

URL

params

Optional

any type

handleAll

handleAll(options) returns Array of Promise

Similar to handle(), but instead of just returning a Promise that resolves to a Response it it will return an tuple of [response, done] promises, where the former (response) is equivalent to what handle() returns, and the latter is a Promise that will resolve once any promises that were added to event.waitUntil() as part of performing the strategy have completed.

You can await the done promise to ensure any extra work performed by the strategy (usually caching responses) completes successfully.

Parameter

options

(FetchEvent or Object)

A FetchEvent or an object with the properties listed below.

Values in options have the following properties:

Parameter

request

(Request or string)

A request to run this strategy for.

event

ExtendableEvent

The event associated with the request.

url

Optional

URL

params

Optional

any type

Returns

Array of Promise A tuple of [response, done] promises that can be used to determine when the response resolves as well as when the handler has completed all its work.