Skip to content

Commit

Permalink
Add clearOnError option
Browse files Browse the repository at this point in the history
  • Loading branch information
lisathesecond committed Oct 29, 2017
1 parent b7c2046 commit 75d4067
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 7 deletions.
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,14 +77,15 @@ where they will be stored, etc.
#### Options

* `maxAge {Number}`: Maximum time for storing each request in milliseconds, defaults to 15 minutes (`15 * 60 * 1000`)
* `store {Object}`: An instance of [`localForage`](https://github.com/localForage/localForage), defaults to a custom in memory store
* `clearOnStale {Boolean}`: Clear cache when it is stale, defaults to `true`
* `limit {Number}`: Maximum number of cached request (last in, first out queue system), no limit by default
* `store {Object}`: An instance of [`localForage`](https://github.com/localForage/localForage), defaults to a custom in memory store
* `key {Mixed}`: Can either be a `String` or a `Function` which receives the `request` object as first parameter to return a unique cache key for each request. Defaults to `req => req.url`
* `exclude {Object}`: Object defining which kind of requests should be excluded from cache
* `filter {Function}`: A method which receives the request and returns `true` to exclude request from cache, defaults to `null`
* `query {Boolean}`: If `true` all requests containing a query will be excluded, defaults to `true`
* `paths {Array}`: An `Array` of regular expressions to match against request URLs, if a match is found it will be excluded, defaults to `[]`
* `clearOnStale {Boolean}`: Clear cached item when it is stale, defaults to `true`
* `clearOnError {Boolean}`: Clear all cache when a write error occurs (prevents size quota problems with `localStorage`)
* `debug {Boolean}`: Print some logs to console, defaults to `false`

#### Returns
Expand Down
10 changes: 6 additions & 4 deletions src/cache.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,12 @@ async function write (config, req, res) {
} catch (err) {
config.debug('Could not store response', err)

try {
await config.store.clear()
} catch (err) {
config.debug('Could not clear store', err)
if (config.clearOnError) {
try {
await config.store.clear()
} catch (err) {
config.debug('Could not clear store', err)
}
}

return false
Expand Down
4 changes: 3 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,16 @@ import request from './request'
const defaultConfig = {
maxAge: 0,
limit: false,
clearOnStale: true,
store: null,
key: null,
exclude: {
paths: [],
query: true,
filter: null
},
adapter: axios.defaults.adapter,
clearOnStale: true,
clearOnError: true,
debug: false
}

Expand Down

0 comments on commit 75d4067

Please sign in to comment.