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
# Can I cache this?[](https://travis-ci.org/kornelski/http-cache-semantics)
1
+
# Can I cache this?
2
2
3
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
4
It also implements `stale-if-error` and `stale-while-revalidate` from [RFC 5861](https://tools.ietf.org/html/rfc5861).
@@ -90,7 +90,7 @@ Returns `true` if the response can be stored in a cache. If it's `false` then yo
90
90
91
91
### `satisfiesWithoutRevalidation(newRequest)`
92
92
93
-
This is the most important method. Use this method to check whether the cached response is still fresh in the context of the new request.
93
+
Use this method to check whether the cached response is still fresh in the context of the new request.
94
94
95
95
If it returns `true`, then the given `request` matches the original response this cache policy has been created with, and the response can be reused without contacting the server. Note that the old response can't be returned without being updated, see `responseHeaders()`.
96
96
@@ -112,7 +112,98 @@ After that time (when `timeToLive() <= 0`) the response may still be usable in c
112
112
113
113
### `toObject()`/`fromObject(json)`
114
114
115
-
Chances are you'll want to store the `CachePolicy` object along with the cached response. `obj = policy.toObject()` gives a plain JSON-serializable object. `policy = CachePolicy.fromObject(obj)` creates an instance from it.
115
+
You'll want to store the `CachePolicy` object along with the cached response. `obj = policy.toObject()` gives a plain JSON-serializable object. `policy = CachePolicy.fromObject(obj)` creates an instance from it.
116
+
117
+
## Complete Usage
118
+
119
+
### `evaluateRequest(newRequest)`
120
+
121
+
Returns an object telling what to do next — optional `revalidation`, and optional `response` from cache. Either one of these properties will be present. Both may be present at the same time.
122
+
123
+
```js
124
+
{
125
+
// If defined, you must send a request to the server.
126
+
revalidation: {
127
+
headers: {}, // HTTP headers to use when sending the revalidation response
128
+
// If true, you MUST wait for a response from the server before using the cache
129
+
// If false, this is stale-while-revalidate. The cache is stale, but you can use it while you update it asynchronously.
130
+
synchronous: bool,
131
+
},
132
+
// If defined, you can use this cached response.
133
+
response: {
134
+
headers: {}, // Updated cached HTTP headers you must use when responding to the client
135
+
},
136
+
}
137
+
```
138
+
139
+
### Example
140
+
141
+
```js
142
+
let cached =cacheStorage.get(incomingRequest.url);
143
+
144
+
// Cache miss - make a request to the origin and cache it
headers:policy.responseHeaders(), // these are from the new revalidated policy
189
+
body,
190
+
}
191
+
});
192
+
193
+
if (revalidation.synchronous) {
194
+
// If synchronous, then you MUST get a reply from the server first
195
+
returnawait updatedResponsePromise;
196
+
}
197
+
198
+
// If not synchronous, it can fall thru to returning the cached response,
199
+
// while the request to the server is happening in the background.
200
+
}
201
+
202
+
return {
203
+
headers:response.headers, // Same as cached.policy.responseHeaders()
204
+
body:cached.body,
205
+
}
206
+
```
116
207
117
208
### Refreshing stale cache (revalidation)
118
209
@@ -124,7 +215,7 @@ The following methods help perform the update efficiently and correctly.
124
215
125
216
Returns updated, filtered set of request headers to send to the origin server to check if the cached response can be reused. These headers allow the origin server to return status 304 indicating the response is still fresh. All headers unrelated to caching are passed through as-is.
126
217
127
-
Use this method when updating cache from the origin server.
218
+
Use this method when updating cache from the origin server. Also available in `evaluateRequest(newRequest).revalidation.headers`.
Use this method to update the cache after receiving a new response from the origin server. It returns an object with two keys:
136
227
137
228
-`policy` — A new `CachePolicy` with HTTP headers updated from `revalidationResponse`. You can always replace the old cached `CachePolicy` with the new one.
138
-
-`modified` — Boolean indicating whether the response body has changed.
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`.
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.
// Update the cache with the newer/fresher response
164
-
letsPretendThisIsSomeCache.set(
165
-
newRequest.url,
166
-
{ policy, body },
167
-
policy.timeToLive()
168
-
);
169
-
170
-
// Make a new response from the cached or revalidated data
171
-
return {
172
-
headers:policy.responseHeaders()
173
-
body,
174
-
}
175
-
}
176
-
```
229
+
-`modified` — Boolean indicating whether the response body has changed, and you should use the new response body sent by the server.
230
+
- If `true`, you should use the new response body, and you can replace the old cached response with the updated one.
231
+
- If `false`, then you should reuse the old cached response body. Either a valid 304 Not Modified response has been received, or an error happened and `stale-if-error` allows falling back to the cache.
177
232
178
233
# Yo, FRESH
179
234
@@ -182,6 +237,7 @@ if (!cachedPolicy.satisfiesWithoutRevalidation(newRequest)) {
0 commit comments