Strategy
First added
Workbox
About
Abstract class for implementing runtime caching strategies.
Custom strategies should extend this class and leverage StrategyHandler
, which will ensure all relevant cache options, fetch options, and plugins are used (per the current strategy instance), to perform all fetching and caching logic.
Abstract methods
_handle(request, handler)
— The main handling logic of the strategy. It is provided with two parameters:request
— TheRequest
the strategy should return a response for.handler
— The StrategyHandler instance automatically created for the current strategy.
Methods and fields
handle(options)
— Performs a request strategy and returns aPromise
that will resolve with aResponse
, invoking all relevant plugin callbacks. When a strategy instance is registered with aRoute
, this method is automatically called when the route matches. Alternatively, this method can be used in a standaloneFetchEvent
listener by passing it toevent.respondWith()
.handleAll(options)
— Similar tohandle()
, but instead of just returning aPromise
that resolves to aResponse
, it will return an tuple of[response, done]
promises, whereresponse
is equivalent to whathandle()
returns, anddone
is aPromise
that will resolve once all promises added toevent.waitUntil()
as a part of performing the strategy have completed. You can await thedone
promise to ensure any extra work performed by the strategy (usually caching responses) completes successfully.
Usage
import { Strategy, type StrategyHandler } from "serwist";
class NetworkOnly extends Strategy {
_handle(request: Request, handler: StrategyHandler) {
return handler.fetch(request);
}
}
More resources
Here is a list of resources you can read to learn more about Strategy
: