---
hide_title: false
hide_table_of_contents: false
pagination_next: null
pagination_prev: null
---
import {Fiddle} from '@site/src/components/fiddle';

# `CacheOverride()`

The **`CacheOverride` constructor** lets you configure the caching behavior of a `Response`.

Normally, the HTTP Headers on a [`Response`](../../globals/Response/Response.mdx) would control how the [`Response`](../../globals/Response/Response.mdx) is cached,
but `CacheOverride` can be set on a [`Request`](../../globals/Request/Request.mdx), to define custom caching behavior.

## Syntax

```js
new CacheOverride(mode)
new CacheOverride(mode, init)
```

> **Note:** `CacheOverride()` can only be constructed with `new`. Attempting to call it without `new` throws a [`TypeError`](../../globals/TypeError/TypeError.mdx).

### Parameters

- `mode` _: string_
  - 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 the `CacheOverride` object's settings.

- `init`

  - : An Object which contains all the configuration options to apply to the newly created `CacheOverride`.

    - `pci` _: boolean_ _**optional**_
      - 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.

    - `surrogateKey` _: string_ _**optional**_
      - 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.
    - `swr` _: number_ _**optional**_
      - Override the caching behavior of this request to use the given `stale-while-revalidate` time, in seconds

    - `ttl` _: number_ _**optional**_
      - Override the caching behavior of this request to use the given Time to Live (TTL), in seconds.

### Return value

A new `CacheOverride` object.

## Examples

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).

<Fiddle config={{
  "type": "javascript",
  "title": "CacheOverride Example",
  "origins": [
    "https://http-me.fastly.dev"
  ],
  "src": {
    "deps": "{\n  \"@fastly/js-compute\": \"^1.0.1\"\n}",
    "main": `
/// <reference types="@fastly/js-compute" />
import { CacheOverride } from "fastly:cache-override";
// 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).
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: 'origin_0'
  });
}
addEventListener("fetch", event => event.respondWith(app(event)));
`
  },
  "requests": [
    {
      "enableCluster": true,
      "enableShield": false,
      "enableWAF": false,
      "method": "GET",
      "path": "/status=200",
      "useFreshCache": false,
      "followRedirects": false,
      "tests": "",
      "delay": 0
    }
  ],
  "srcVersion": 1
}}>

```js
/// <reference types="@fastly/js-compute" />
import { CacheOverride } from "fastly:cache-override";
// 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).
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: 'origin_0'
  });
}
addEventListener("fetch", event => event.respondWith(app(event)));
```

</Fiddle>