@@ -15,6 +15,15 @@ const HEURISTICALLY_CACHEABLE_STATUS_CODES = [
|
15 | 15 | 200, 203, 204, 206, 300, 301, 308, 404, 405, 410, 414, 501 |
16 | 16 | ] |
17 | 17 | |
| 18 | +// Status codes which semantic is not handled by the cache |
| 19 | +// https://datatracker.ietf.org/doc/html/rfc9111#section-3 |
| 20 | +// This list should not grow beyond 206 and 304 unless the RFC is updated |
| 21 | +// by a newer one including more. Please introduce another list if |
| 22 | +// implementing caching of responses with the 'must-understand' directive. |
| 23 | +const NOT_UNDERSTOOD_STATUS_CODES = [ |
| 24 | +206, 304 |
| 25 | +] |
| 26 | + |
18 | 27 | const MAX_RESPONSE_AGE = 2147483647000 |
19 | 28 | |
20 | 29 | /** |
@@ -241,10 +250,19 @@ class CacheHandler {
|
241 | 250 | * @param {import('../../types/cache-interceptor.d.ts').default.CacheControlDirectives} cacheControlDirectives |
242 | 251 | */ |
243 | 252 | function canCacheResponse (cacheType, statusCode, resHeaders, cacheControlDirectives) { |
244 | | -// Allow caching for status codes 200 and 307 (original behavior) |
245 | | -// Also allow caching for other status codes that are heuristically cacheable |
246 | | -// when they have explicit cache directives |
247 | | -if (statusCode !== 200 && statusCode !== 307 && !HEURISTICALLY_CACHEABLE_STATUS_CODES.includes(statusCode)) { |
| 253 | +// Status code must be final and understood. |
| 254 | +if (statusCode < 200 || NOT_UNDERSTOOD_STATUS_CODES.includes(statusCode)) { |
| 255 | +return false |
| 256 | +} |
| 257 | +// Responses with neither status codes that are heuristically cacheable, nor "explicit enough" caching |
| 258 | +// directives, are not cacheable. "Explicit enough": see https://www.rfc-editor.org/rfc/rfc9111.html#section-3 |
| 259 | +if (!HEURISTICALLY_CACHEABLE_STATUS_CODES.includes(statusCode) && !resHeaders['expires'] && |
| 260 | +!cacheControlDirectives.public && |
| 261 | +cacheControlDirectives['max-age'] === undefined && |
| 262 | +// RFC 9111: a private response directive, if the cache is not shared |
| 263 | +!(cacheControlDirectives.private && cacheType === 'private') && |
| 264 | +!(cacheControlDirectives['s-maxage'] !== undefined && cacheType === 'shared') |
| 265 | +) { |
248 | 266 | return false |
249 | 267 | } |
250 | 268 | |
|