StrategyHandler
First added
Workbox
About
A class created every time a Strategy instance calls Strategy.handle
or Strategy.handleAll
that wraps all fetch and cache actions around plugin callbacks and keeps track of when the strategy is “done” (i.e. when all added event.waitUntil()
promises have resolved).
Methods and fields
event
— The event associated with this request.request
— The request the strategy is processing (passed to the strategy’shandle()
orhandleAll()
method).url
— AURL
instance ofrequest.url
(if passed to the strategy’shandle()
orhandleAll()
method). Note: theurl
param will be present if the strategy is invoked from aRoute
object.params
— Some additional params (if passed to the strategy’shandle()
orhandleAll()
method). Note: theparams
param will be present if the strategy is invoked from aRoute
object and that route’s matcher returned a truthy value (it will be that value).async fetch(requestInfo)
— Fetches a given request (and invokes any applicable plugin callback methods), taking thefetchOptions
(for non-navigation requests) andplugins
provided to theStrategy
object into account.async fetchAndCachePut(requestInfo)
— Callsthis.fetch()
and (in the background) caches the generated response. The call tothis.cachePut()
automatically invokesthis.waitUntil()
, so you do not have to callwaitUntil()
yourself.async cacheMatch(key)
— Matches a request from the cache (and invokes any applicable plugin callback method) using thecacheName
,matchOptions
, andplugins
provided to theStrategy
object. The following lifecycle methods are invoked when using this method:cacheKeyWillBeUsed
cachedResponseWillBeUsed
async cachePut(key, response)
— Puts a request/response pair into the cache (and invokes any applicable plugin callback method) using thecacheName
andplugins
provided to theStrategy
object. The following plugin lifecycle methods are invoked when using this method:cacheKeyWillBeUsed
cacheWillUpdate
cacheDidUpdate
async getCacheKey(request, mode)
— Checks theplugins
provided to theStrategy
object forcacheKeyWillBeUsed
callbacks and executes found callbacks in sequence. The finalRequest
object returned by the last plugin is treated as the cache key for cache reads and/or writes. If nocacheKeyWillBeUsed
plugin callbacks have been registered, the passed request is returned unmodified.hasCallback(name)
— Returnstrue
if the strategy has at least one plugin with the given callback.async runCallbacks(name, param)
— Runs all plugin callbacks matching the given name, in order, passing the givenparam
object (merged with the plugin’s current state) as the only argument.*iterateCallbacks(name)
— Accepts a callback name and returns an iterable of matching plugin callbacks.waitUntil(promise)
— Adds a promise to the extend lifetime promises of the event event associated with the request being handled (usually aFetchEvent
).async doneWaiting()
— Returns a promise that resolves once all promises passed tothis.waitUntil()
have settled. Note: any work done afterdoneWaiting()
settles should be manually passed to an event’swaitUntil()
method (notthis.waitUntil()
), otherwise the service worker thread may be killed prior to your work completing.destroy()
— Stops running the strategy and immediately resolves any pendingwaitUntil()
promise.
Usage
import { Strategy, type StrategyHandler } from "serwist";
class CacheNetworkRace extends Strategy {
_handle(request: Request, handler: StrategyHandler) {
const fetchAndCachePutDone = handler.fetchAndCachePut(request);
const cacheMatchDone = handler.cacheMatch(request);
return new Promise<Response>((resolve, reject) => {
fetchAndCachePutDone.then(resolve);
cacheMatchDone.then((response) => {
if (response) {
resolve(response);
}
});
// Reject if both network and cache error or find no response.
Promise.allSettled([fetchAndCachePutDone, cacheMatchDone]).then((results) => {
const [fetchAndCachePutResult, cacheMatchResult] = results;
if (fetchAndCachePutResult.status === "rejected" && (cacheMatchResult.status === "rejected" || !cacheMatchResult.value)) {
reject(fetchAndCachePutResult.reason);
}
});
});
}
}
More resources
Here is a list of resources you can read to learn more about StrategyHandler
: