Skip to content

Commit

Permalink
feat: cache predicate defaults follows RFC 7231
Browse files Browse the repository at this point in the history
  • Loading branch information
arthurfiorette committed Mar 19, 2023
1 parent 2ae4d28 commit 174ea35
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 4 deletions.
2 changes: 1 addition & 1 deletion docs/src/config/request-specifics.md
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ exceptions to the method rule.
<Badge text="optional" type="warning"/>

- Type: `CachePredicate<R, D>`
- Default: `{}`
- Default: `{ statusCheck: (status) => [200, 203, 300, 301, 302, 404, 405, 410, 414, 501].includes(status) }`

An object or function that will be tested against the response to indicate if it can be
cached.
Expand Down
2 changes: 1 addition & 1 deletion src/cache/cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ export type CacheProperties<R = unknown, D = unknown> = {
* An object or function that will be tested against the response to indicate if it can
* be cached.
*
* @default { statusCheck: (status) => status >= 200 && status < 400 }
* @default { statusCheck: (status) => [200, 203, 300, 301, 302, 404, 405, 410, 414, 501].includes(status) }
* @see https://axios-cache-interceptor.js.org/config/request-specifics#cache-cachepredicate
*/
cachePredicate: CachePredicate<R, D>;
Expand Down
7 changes: 5 additions & 2 deletions src/cache/create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,13 @@ export function setupCache(

ttl: options.ttl ?? 1000 * 60 * 5,

methods: options.methods || ['get'],
// Although RFC 7231 also marks POST as cacheable, most users don't know that
// and may have problems about why their "create X" route not working.
methods: options.methods || ['get', 'head'],

cachePredicate: options.cachePredicate || {
statusCheck: (status) => status >= 200 && status < 400
// All cacheable status codes defined in RFC 7231
statusCheck: (status) => [200, 203, 300, 301, 302, 404, 405, 410, 414, 501].includes(status)
},

etag: options.etag ?? true,
Expand Down

0 comments on commit 174ea35

Please sign in to comment.