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— AURLinstance ofrequest.url(if passed to the strategy’shandle()orhandleAll()method). Note: theurlparam will be present if the strategy is invoked from aRouteobject.params— Some additional params (if passed to the strategy’shandle()orhandleAll()method). Note: theparamsparam will be present if the strategy is invoked from aRouteobject 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) andpluginsprovided to theStrategyobject 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, andpluginsprovided to theStrategyobject. The following lifecycle methods are invoked when using this method:cacheKeyWillBeUsedcachedResponseWillBeUsed
async cachePut(key, response)— Puts a request/response pair into the cache (and invokes any applicable plugin callback method) using thecacheNameandpluginsprovided to theStrategyobject. The following plugin lifecycle methods are invoked when using this method:cacheKeyWillBeUsedcacheWillUpdatecacheDidUpdate
async getCacheKey(request, mode)— Checks thepluginsprovided to theStrategyobject forcacheKeyWillBeUsedcallbacks and executes found callbacks in sequence. The finalRequestobject returned by the last plugin is treated as the cache key for cache reads and/or writes. If nocacheKeyWillBeUsedplugin callbacks have been registered, the passed request is returned unmodified.hasCallback(name)— Returnstrueif 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 givenparamobject (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: