---
title: CacheOverride
summary: Configures the caching behavior of a Response.
url: https://www.fastly.com/documentation/reference/compute/sdks/javascript/js-compute/cache-override/class.CacheOverride/
---

**Kind:** Class  
**Module:** [cache-override](https://www.fastly.com/documentation/reference/compute/sdks/javascript/js-compute/cache-override/)  
**Package:** @fastly/js-compute 4.0.1  
**Source:** https://github.com/fastly/js-compute-runtime/blob/70cf30af4e657879b2d1e7eafab880e39e3a3f7d/types/cache-override.d.ts#L150

Configures the caching behavior of a [`Response`](https://www.fastly.com/documentation/reference/compute/sdks/javascript/js-compute/globals/interface.Response/).

Normally, the HTTP Headers on a [`Response`](https://www.fastly.com/documentation/reference/compute/sdks/javascript/js-compute/globals/interface.Response/) would control how the [`Response`](https://www.fastly.com/documentation/reference/compute/sdks/javascript/js-compute/globals/interface.Response/) is cached,
but `CacheOverride` can be set on a [`Request`](https://www.fastly.com/documentation/reference/compute/sdks/javascript/js-compute/globals/interface.Request/), to define custom caching behavior.

````javascript
In this example we override the cache for all the requests prefixed /static/ to have a long TTL (Time To Live),
and the home page to have a short TTL and a long SWR (Stale While Revalidate).

```js
import { CacheOverride } from "fastly:cache-override";

async function app(event) {
  const path = (new URL(event.request.url)).pathname;
  let cacheOverride;
  if (path == '/') {
    cacheOverride = new CacheOverride('override', {ttl: 10, swr: 86_400});
  } else if (path.startsWith('/static/')) {
    cacheOverride = new CacheOverride('override', {ttl: 86_400});
  } else {
    cacheOverride = new CacheOverride('none')
  }
  return fetch(event.request.url, {
    cacheOverride,
    backend: 'my-backend'
  });
}
addEventListener("fetch", event => event.respondWith(app(event)));
```
````

## Constructor

_Overload 1 of 2_

```javascript
new CacheOverride(mode: CacheOverrideMode, init?: ICacheOverride)
```

**Source:** https://github.com/fastly/js-compute-runtime/blob/70cf30af4e657879b2d1e7eafab880e39e3a3f7d/types/cache-override.d.ts#L161

**Parameters**

- `mode`: `CacheOverrideMode`

  Sets the cache override mode for a request

  If set to:
  - "none": Do not override the behavior specified in the origin response’s cache control headers.
  - "pass": Do not cache the response to this request, regardless of the origin response’s headers.
  - "override": Override particular cache control settings using a [`CacheOverride`](https://www.fastly.com/documentation/reference/compute/sdks/javascript/js-compute/cache-override/class.CacheOverride/) object.
- `init`: `ICacheOverride` (optional) — Sets the cache override init options

_Overload 2 of 2_

```javascript
new CacheOverride(overrideInit?: ICacheOverride)
```

**Source:** https://github.com/fastly/js-compute-runtime/blob/70cf30af4e657879b2d1e7eafab880e39e3a3f7d/types/cache-override.d.ts#L168

When an init object is provided as the first argument, the mode defaults to `"override"`.

**Parameters**

- `overrideInit`: `ICacheOverride` (optional) — Sets the cache override init options

## Properties

### mode

**Type:** `CacheOverrideMode`  
**Source:** https://github.com/fastly/js-compute-runtime/blob/70cf30af4e657879b2d1e7eafab880e39e3a3f7d/types/cache-override.d.ts#L178

Sets the cache override mode for a request

If set to:
- "none": Do not override the behavior specified in the origin response’s cache control headers.
- "pass": Do not cache the response to this request, regardless of the origin response’s headers.
- "override": Override particular cache control settings using a [`CacheOverride`](https://www.fastly.com/documentation/reference/compute/sdks/javascript/js-compute/cache-override/class.CacheOverride/) object.

### ttl

**Type:** `number`  
**Flags:** optional  
**Source:** https://github.com/fastly/js-compute-runtime/blob/70cf30af4e657879b2d1e7eafab880e39e3a3f7d/types/cache-override.d.ts#L182

Override the caching behavior of this request to use the given Time to Live (TTL), in seconds.

### swr

**Type:** `number`  
**Flags:** optional  
**Source:** https://github.com/fastly/js-compute-runtime/blob/70cf30af4e657879b2d1e7eafab880e39e3a3f7d/types/cache-override.d.ts#L187

Override the caching behavior of this request to use the given `stale-while-revalidate` time,
in seconds

### surrogateKey

**Type:** `string`  
**Flags:** optional  
**Source:** https://github.com/fastly/js-compute-runtime/blob/70cf30af4e657879b2d1e7eafab880e39e3a3f7d/types/cache-override.d.ts#L195

Override the caching behavior of this request to include the given surrogate key, provided as
a header value.

See the [Fastly surrogate keys guide](https://docs.fastly.com/en/guides/purging-api-cache-with-surrogate-keys)
for details.

### pci

**Type:** `boolean`  
**Flags:** optional  
**Source:** https://github.com/fastly/js-compute-runtime/blob/70cf30af4e657879b2d1e7eafab880e39e3a3f7d/types/cache-override.d.ts#L206

Override the caching behavior of this request to enable or disable PCI/HIPAA-compliant
non-volatile caching.

By default, this is false, which means the request may not be PCI/HIPAA-compliant. Set it to
true to enable compliant caching.

See the [Fastly PCI-Compliant Caching and Delivery documentation](https://docs.fastly.com/products/pci-compliant-caching-and-delivery)
for details.

### beforeSend

**Type:** `(request: Request) => void | PromiseLike<void>`  
**Flags:** optional, since 3.30.0  
**Source:** https://github.com/fastly/js-compute-runtime/blob/70cf30af4e657879b2d1e7eafab880e39e3a3f7d/types/cache-override.d.ts#L216

Callback to be invoked if a request is going all the way to a backend, allowing the request to be modified beforehand.

See [Modifying a request as it is forwarded to a backend](https://www.fastly.com/documentation/guides/concepts/edge-state/cache/#modifying-a-request-as-it-is-forwarded-to-a-backend)
in the Fastly cache interfaces documentation for details.

### afterSend

**Type:** `(response: Response) => void | CacheOptions | PromiseLike<void | CacheOptions>`  
**Flags:** optional, since 3.30.0  
**Source:** https://github.com/fastly/js-compute-runtime/blob/70cf30af4e657879b2d1e7eafab880e39e3a3f7d/types/cache-override.d.ts#L226

Callback to be invoked after a response has been sent, but before it is stored into the cache.

See [Controlling cache behavior based on backend response](https://www.fastly.com/documentation/guides/concepts/edge-state/cache/#controlling-cache-behavior-based-on-backend-response)
in the Fastly cache interfaces documentation for details.
