Skip to content
This repository has been archived by the owner on Jun 12, 2022. It is now read-only.

Commit

Permalink
fix(cache): make force-cache and only-if-cached work as expected
Browse files Browse the repository at this point in the history
  • Loading branch information
zkat committed Apr 1, 2017
1 parent 8ebda1d commit f50e9df
Showing 1 changed file with 11 additions and 3 deletions.
14 changes: 11 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@ const pkg = require('./package.json')
const retry = require('promise-retry')
const url = require('url')

// The "cache mode" options are really confusing, and this module does
// its best to recreate them:
// https://fetch.spec.whatwg.org/#http-network-or-cache-fetch
module.exports = cachingFetch
function cachingFetch (uri, _opts) {
Expand Down Expand Up @@ -52,8 +50,18 @@ function cachingFetch (uri, _opts) {
return res
} else if (res && (opts.cache === 'default' || opts.cache === 'no-cache')) {
return condFetch(uri, res, opts)
} else if (res && (
opts.cache === 'force-cache' || opts.cache === 'only-if-cached'
)) {
return res
} else if (!res && opts.cache === 'only-if-cached') {
throw new Error(`request to ${uri} failed: cache mode is 'only-if-cached' but no cached response available.`)
const err = new Error(
`request to ${
uri
} failed: cache mode is 'only-if-cached' but no cached response available.`
)
err.code = 'ENOTCACHED'
throw err
} else {
// Missing cache entry, or mode is default (if stale), reload, no-store
return remoteFetch(uri, opts)
Expand Down

0 comments on commit f50e9df

Please sign in to comment.