CacheOverride

Classin cache-override@fastly/js-compute 4.0.0View source

Configures the caching behavior of a Response.

Normally, the HTTP Headers on a Response would control how the Response is cached, but CacheOverride can be set on a Request, to define custom caching behavior.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
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

new CacheOverride(mode: CacheOverrideMode, init?: ICacheOverride)
View source

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 object.
init: ICacheOverrideoptional

Sets the cache override init options

Overload 2 of 2

new CacheOverride(overrideInit?: ICacheOverride)
View source

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

Parameters

overrideInit: ICacheOverrideoptional

Sets the cache override init options

Properties

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

ttl

numberoptionalView source

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

swr

numberoptionalView source

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

surrogateKey

stringoptionalView source

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

See the Fastly surrogate keys guide for details.

pci

booleanoptionalView source

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 for details.

beforeSend

(request: Request) => void | PromiseLike<void>optionalsince 3.30.0View source

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 in the Fastly cache interfaces documentation for details.

afterSend

(response: Response) => void | CacheOptions | PromiseLike<void | CacheOptions>optionalsince 3.30.0View source

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 in the Fastly cache interfaces documentation for details.