workbox-strategies. Strategy
An abstract base class that all other strategy classes must extend from:
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
|
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
_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 |
- 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 Values in
|
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 Values in
|
- 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.