You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+28-20Lines changed: 28 additions & 20 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,10 +1,12 @@
1
1
# Can I cache this? [](https://travis-ci.org/kornelski/http-cache-semantics)
2
2
3
-
`CachePolicy`tells when responses can be reused from a cache, taking into account [HTTP RFC 7234](http://httpwg.org/specs/rfc7234.html) rules for user agents and shared caches.
4
-
It also implements [RFC 5861](https://tools.ietf.org/html/rfc5861), implementing `stale-if-error` and `stale-while-revalidate`.
3
+
This library tells when responses can be reused from a cache, taking into account [HTTP RFC 7234/9111](http://httpwg.org/specs/rfc9111.html) rules for user agents and shared caches.
4
+
It also implements `stale-if-error` and `stale-while-revalidate` from [RFC 5861](https://tools.ietf.org/html/rfc5861).
5
5
It's aware of many tricky details such as the `Vary` header, proxy revalidation, and authenticated responses.
6
6
7
-
## Usage
7
+
## Basic Usage
8
+
9
+
`CachePolicy` is a metadata object that is meant to be stored in the cache, and it will keep track of cacheability of the response.
8
10
9
11
Cacheability of an HTTP response depends on how it was requested, so both `request` and `response` are required to create the policy.
10
12
@@ -20,22 +22,26 @@ if (!policy.storable()) {
20
22
// (this is pseudocode, roll your own cache (lru-cache package works))
21
23
letsPretendThisIsSomeCache.set(
22
24
request.url,
23
-
{ policy, response },
25
+
{ policy, body:response.body },// you only need to store the response body. CachePolicy holds the headers.
const { policy, body } =letsPretendThisIsSomeCache.get(newRequest.url);
31
33
32
34
// It's not enough that it exists in the cache, it has to match the new request, too:
33
35
if (policy &&policy.satisfiesWithoutRevalidation(newRequest)) {
34
36
// OK, the previous response can be used to respond to the `newRequest`.
35
37
// Response headers have to be updated, e.g. to add Age and remove uncacheable headers.
36
-
response.headers=policy.responseHeaders();
37
-
return response;
38
+
return {
39
+
headers:policy.responseHeaders(),
40
+
body,
41
+
}
38
42
}
43
+
44
+
// Cache miss. See revalidationHeaders() and revalidatedPolicy() for advanced usage.
39
45
```
40
46
41
47
It may be surprising, but it's not enough for an HTTP response to be [fresh](#yo-fresh) to satisfy a request. It may need to match request headers specified in `Vary`. Even a matching fresh response may still not be usable if the new request restricted cacheability, etc.
@@ -95,15 +101,14 @@ If it returns `false`, then the response may not be matching at all (e.g. it's f
95
101
Returns updated, filtered set of response headers to return to clients receiving the cached response. This function is necessary, because proxies MUST always remove hop-by-hop headers (such as `TE` and `Connection`) and update response's `Age` to avoid doubling cache time.
Returns approximate time in _milliseconds_until the response becomes stale (i.e. not fresh).
109
+
Suggests a time in _milliseconds_for how long this cache entry may be useful. This is not freshness, so always check with `satisfiesWithoutRevalidation()`. This time may be longer than response's `max-age` to allow for `stale-if-error` and `stale-while-revalidate`.
104
110
105
-
After that time (when `timeToLive() <= 0`) the response might not be usable without revalidation. However, there are exceptions, e.g. a client can explicitly allow stale responses, so always check with `satisfiesWithoutRevalidation()`.
106
-
`stale-if-error` and `stale-while-revalidate` extend the time to live of the cache, that can still be used if stale.
111
+
After that time (when `timeToLive() <= 0`) the response may still be usable in certain cases, e.g. if client can explicitly allows stale responses.
107
112
108
113
### `toObject()`/`fromObject(json)`
109
114
@@ -133,37 +138,40 @@ Use this method to update the cache after receiving a new response from the orig
133
138
-`modified` — Boolean indicating whether the response body has changed.
134
139
- If `false`, then a valid 304 Not Modified response has been received, and you can reuse the old cached response body. This is also affected by `stale-if-error`.
135
140
- If `true`, you should use new response's body (if present), or make another request to the origin server without any conditional headers (i.e. don't use `revalidationHeaders()` this time) to get the new resource.
141
+
-`headers` — updated response headers to use when returning the response to the client.
0 commit comments