Skip to content

Commit

Permalink
docs: added staleIfError documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
arthurfiorette committed Jan 18, 2022
1 parent edb32bd commit 93dda05
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 1 deletion.
2 changes: 1 addition & 1 deletion docs/pages/_comparison-table.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,4 @@
| Update cache based on response || 🟡 | 🟡 |
| Predicate to test if request should be cached ||| 🛑 |
| Concurrent requests || 🔶[#231](https://github.com/RasCarlito/axios-cache-adapter/issues/231) | 🛑 |
| Cache fallback on network errors | 🛑 |||
| Cache fallback on network errors | |||
30 changes: 30 additions & 0 deletions docs/pages/per-request-configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -133,3 +133,33 @@ or true to use the last cached timestamp. If never cached before, the header is
If `interpretHeader` is set and a `Last-Modified` header is sent then value from that
header is used, otherwise cache creation timestamp will be sent in `If-Modified-Since`.
Default: `true`

## `cache.staleIfError`

Enables cache to be returned if the response comes with an error, either by invalid status
code, network errors and etc. You can filter the type of error that should be stale by
using a predicate function.

**Note**: If this value ends up `false`, either by default or by a predicate function and
there was an error, the request cache will be purged.

**Note**: If the response is treated as error because of invalid status code _(like from
AxiosRequestConfig#invalidateStatus)_, and this ends up `true`, the cache will be
preserved over the "invalid" request. So, if you want to preserve the response, you can
use this predicate:

```js
const customPredicate = (response, cache, error) => {
// Blocks staleIfError if has a response
return !response;

// Note that, this still respects axios default implementation
// and throws an error, (but it has the response)
};
```

Possible types:

- `number` -> the max time (in seconds) that the cache can be reused.
- `boolean` -> `false` disables and `true` enables with infinite time.
- `function` -> a predicate that can return `number` or `boolean` as described above.

0 comments on commit 93dda05

Please sign in to comment.