The caching options, all optional: with none given, exchanges are cached in memory under the freshness the origin server reports
Optional Readonlystore?: number | Store | BucketThe Store or Bucket to hold entries in, or the number of entries the default memory
store is to retain, giving up the least recently used beyond that
Optional Readonlyttl?: numberThe freshness to assume where a response reports neither an expiration nor a change time and the cap on any freshness derived from what it does report, in milliseconds, measured from the instant the origin server generated the content rather than from the instant it was received
Optional Readonlyavoid?: Some<string>The glob patterns selecting the targets to be kept out of the cache, one or several, matched
whole against the target URI: * and ? stand for a run of characters and for a single character within a
path segment, ** for a run of characters across separators; with none given, every eligible target is cached
Optional Readonlyforce?: Some<string>The glob patterns selecting the targets to be cached whatever caching directives the origin
server states, one or several, matched as avoid patterns are; with none given, every target is cached under
the directives the origin server states; a target the avoid option also selects is kept out of the cache
A Middleware replaying eligible exchanges from the store
Creates a caching middleware.
The generated middleware replays the response it holds for a
GETorHEADexchange while that response is fresh. Freshness is the one the origin server states or, where it states none, the one itsLast-Modifiedimplies or thettloption assumes. A response no longer fresh is revalidated with its stored validators, and its held content is replayed again when the origin server answers304. Where nothing usable is held, the exchange is relayed to the wrapped Fetch implementation and the response is kept for later exchanges. A successful mutating request invalidates the entries for its target, so a client that reads and writes never serves what it overwrote.A response answered from the store restates the exchange as it was retrieved, its URL, status and reason phrase included, so that a client reading it draws the same conclusions it draws from a freshly retrieved one. The age is the one departure: an
Ageheader field states how long the content has been held since the origin server delivered it, so that whoever receives it judges for itself how much of its freshness is left. A successful revalidation restarts the age from what the304states, as the delivery it confirms supersedes the one the entry was reporting.Invalidation gives up every variant held for a target and extends to the URIs named by the
LocationandContent-Locationheader fields of the response, when they share the origin of the target, so that a server reports what else a mutating request has changed. No other entry is given up: a target and its query string variants are distinct resources, and nothing states that one goes stale when another is written.Exchanges targeting a scheme other than
httpandhttpsand exchanges stating aRangeare relayed untouched and never stored, as are partial responses and, outside the targets theforceoption selects, responses setting a cookie, so that a complete response answering a complete request is the only thing ever replayed.A target the
avoidoption selects is relayed untouched and never stored, so that a resource the consumer holds back stays out of the cache whatever the origin server states about it. Invalidation is unaffected: a successful unsafe exchange for an avoided target still gives up the entries the response reports as changed.A target the
forceoption selects is cached as though the origin server stated no caching directives, so that a source forbidding caching on every response is reused under the freshness the consumer assumes. A forced response is stored even where it sets a cookie or statesno-store. ItsCache-Controldirectives andExpiresfield play no part in its freshness, which follows itsLast-Modifiedor, failing that,ttl. The cookie it sets reaches the client on the original exchange alone and is never stored, so that no session outlives the exchange that opened it.Forcing settles whether and for how long a response is reused, not which requests it answers or what its status means. Unsuccessful, partial and unpredictably varying responses are still never stored, variants are still told apart by
Vary, and a freshness is still assumed for the heuristically cacheable status codes alone. A target both options select is avoided, as keeping a resource out of the cache outweighs reusing it.With no positive
ttl, a forced response reporting no usableLast-Modifiedis revalidated on every exchange. A forced source reporting neither a change time nor a validator is then downloaded again each time, soforcepays off for it only alongside a positivettl, which bounds how long its content is reused.Every numeric option is removed by a value less than or equal to
0, as the defaults do: entries are then retained for the life of the middleware and reuse rests on what the origin server reports alone.The
ttloption states a single freshness budget, that is how long a response is worth reusing: the origin server overrides it by stating less, and it governs where the response reports nothing to derive a freshness from, so that a response carrying no expiration is reused rather than revalidated on every exchange. Freshness is assumed only for the status codes RFC 9110 § 15.1 defines as heuristically cacheable, and only where noCache-Controlno-cache,max-age,must-revalidateorproxy-revalidatedirective and noExpiresheader field is stated: a stated expiration keeps winning, however short it is, except for the targets theforceoption selects.A response stating no expiration but reporting when its content last changed is reused for a tenth of the interval it had gone unchanged, so that a resource edited a minute ago is revalidated sooner than one untouched for a year;
ttlcaps that share as it caps a stated expiration, and supplies the freshness where no usableLast-Modifiedis reported.