---
url: /api/symbols/workbox-swkit/strategies/strategyhandler.md
---

# StrategyHandler

Class @vite-pwa/workbox-swkit/strategies

A class created every time a Strategy instance instance calls
workbox-strategies.Strategy~handle or
workbox-strategies.Strategy~handleAll that wraps all fetch and
cache actions around plugin callbacks and keeps track of when the strategy
is "done" (i.e. all added `event.waitUntil()` promises have resolved).

## Constructor

```ts
new StrategyHandler(strategy: Strategy, options: HandlerCallbackOptions)
```

Creates a new instance associated with the passed strategy and event
that's handling the request.

The constructor also initializes the state that will be passed to each of
the plugins handling this request.

## Properties

## Methods

### cacheMatch()

```ts
cacheMatch(key: RequestInfo): Promise<Response | undefined>
```

Matches a request from the cache (and invokes any applicable plugin
callback methods) using the `cacheName`, `matchOptions`, and `plugins`
defined on the strategy object.

The following plugin lifecycle methods are invoked when using this method:

* cacheKeyWillByUsed()
* cachedResponseWillByUsed()

**Parameters**

* `key` – The Request or URL to use as the cache key.

**Returns:** A matching response, if found.

### cachePut()

```ts
cachePut(key: RequestInfo, response: Response): Promise<boolean>
```

Puts a request/response pair in the cache (and invokes any applicable
plugin callback methods) using the `cacheName` and `plugins` defined on
the strategy object.

The following plugin lifecycle methods are invoked when using this method:

* cacheKeyWillByUsed()
* cacheWillUpdate()
* cacheDidUpdate()

**Parameters**

* `key` – The request or URL to use as the cache key.

* `response` – The response to cache.

**Returns:** `false` if a cacheWillUpdate caused the response
not be cached, and `true` otherwise.

### destroy()

```ts
destroy(): void
```

Stops running the strategy and immediately resolves any pending
`waitUntil()` promises.

### doneWaiting()

```ts
doneWaiting(): Promise<void>
```

Returns a promise that resolves once all promises passed to
workbox-strategies.StrategyHandler~waitUntil
have settled.

Note: any work done after `doneWaiting()` settles should be manually
passed to an event's `waitUntil()` method (not this handler's
`waitUntil()` method), otherwise the service worker thread my be killed
prior to your work completing.

### fetch()

```ts
fetch(input: RequestInfo): Promise<Response>
```

Fetches a given request (and invokes any applicable plugin callback
methods) using the `fetchOptions` (for non-navigation requests) and
`plugins` defined on the `Strategy` object.

The following plugin lifecycle methods are invoked when using this method:

* `requestWillFetch()`
* `fetchDidSucceed()`
* `fetchDidFail()`

**Parameters**

* `input` – The URL or request to fetch.

**Returns:**

### fetchAndCachePut()

```ts
fetchAndCachePut(input: RequestInfo): Promise<Response>
```

Calls `this.fetch()` and (in the background) runs `this.cachePut()` on
the response generated by `this.fetch()`.

The call to `this.cachePut()` automatically invokes `this.waitUntil()`,
so you do not have to manually call `waitUntil()` on the event.

**Parameters**

* `input` – The request or URL to fetch and cache.

**Returns:**

### getCacheKey()

```ts
getCacheKey(request: Request, mode: "read" | "write"): Promise<Request>
```

Checks the list of plugins for the `cacheKeyWillBeUsed` callback, and
executes any of those callbacks found in sequence. The final `Request`
object returned by the last plugin is treated as the cache key for cache
reads and/or writes. If no `cacheKeyWillBeUsed` plugin callbacks have
been registered, the passed request is returned unmodified

**Returns:**

### hasCallback()

```ts
hasCallback(name: C): boolean
```

Returns true if the strategy has at least one plugin with the given
callback.

**Parameters**

* `name` – The name of the callback to check for.

**Returns:**

### iterateCallbacks()

```ts
iterateCallbacks(name: C): Generator<NonNullable<WorkboxPlugin[C]>>
```

Accepts a callback and returns an iterable of matching plugin callbacks,
where each callback is wrapped with the current handler state (i.e. when
you call each callback, whatever object parameter you pass it will
be merged with the plugin's current state).

**Parameters**

* `name` – The name fo the callback to run

**Returns:**

### runCallbacks()

```ts
runCallbacks(name: C, param: Omit<WorkboxPluginCallbackParam[C], "state">): Promise<void>
```

Runs all plugin callbacks matching the given name, in order, passing the
given param object (merged ith the current plugin state) as the only
argument.

Note: since this method runs all plugins, it's not suitable for cases
where the return value of a callback needs to be applied prior to calling
the next callback. See
workbox-strategies.StrategyHandler#iterateCallbacks
below for how to handle that case.

**Parameters**

* `name` – The name of the callback to run within each plugin.

* `param` – The object to pass as the first (and only) param
  when executing each callback. This object will be merged with the
  current plugin state prior to callback execution.

### waitUntil()

```ts
waitUntil(promise: Promise<T>): Promise<T>
```

Adds a promise to the
\[extend lifetime promises]https://w3c.github.io/ServiceWorker/#extendableevent-extend-lifetime-promises
of the event event associated with the request being handled (usually a
`FetchEvent`).

Note: you can await
workbox-strategies.StrategyHandler~doneWaiting
to know when all added promises have settled.

**Parameters**

* `promise` – A promise to add to the extend lifetime promises
  of the event that triggered the request.
