A middleware to configure cache behavior on a per route or request basis
- From the NPM registry
npm install @p-j/eapi-middleware-cache
# or
yarn add @p-j/eapi-middleware-cache
withCache
is a Middleware Factory; it takes the following options:
export interface WithCacheOptions {
cacheControl?: string
cdnTtl?: number
cacheError?: boolean
varyHeaders?: string[]
serverTimings?: boolean
}
As noted above, none of the options are required.
cacheControl
the value to be assigned to the Cache-Control header (control the Browser Cache TTL)cdnTtl
this control the Edge Cache TTL, by default it also sets a Cache-Control of the same value, unlesscacheControl
is also setcacheCacheError
wheter or not to cache errors. Defaults to false.varyHeaders
an array of Header names to be add to the Vary header (eg: 'Accept', 'Origin' ... ).serverTimings=true
add Server-Timing header with cache interaction information. Defaults to true.
For a better understanding of how cache works in the context of Cloudflare Workers, these links may help:
And for the underlying API