CacheOverride
1234567891011121314151617181920212223In 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).
```jsimport { 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
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
CacheOverrideobject.
- init: ICacheOverrideoptional
Sets the cache override init options
Overload 2 of 2
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
CacheOverrideobject.
Override the caching behavior of this request to use the given Time to Live (TTL), in seconds.
Override the caching behavior of this request to use the given stale-while-revalidate time,
in seconds
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.
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.
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 sourceCallback 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.