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

Commit

Permalink
fix(cache): invalidation on non-GET
Browse files Browse the repository at this point in the history
  • Loading branch information
zkat committed Mar 31, 2017
1 parent a4a7769 commit fe78fac
Showing 1 changed file with 28 additions and 10 deletions.
38 changes: 28 additions & 10 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ function cachingFetch (uri, _opts) {
}
let res
if (
(!opts.method || opts.method.toLowerCase() === 'get') &&
opts.cache &&
opts.cacheMode !== 'no-store' &&
opts.cacheMode !== 'reload'
Expand Down Expand Up @@ -87,37 +88,48 @@ function heuristicFreshness (res) {
}
}

function condFetch (uri, res, opts) {
function condFetch (uri, cachedRes, opts) {
const newHeaders = {}
Object.keys(opts.headers || {}).forEach(k => {
newHeaders[k] = opts.headers[k]
})
if (res.headers.get('etag')) {
if (cachedRes.headers.get('etag')) {
const condHeader = opts.method && opts.method.toLowerCase() !== 'get'
? 'if-match'
: 'if-none-match'
newHeaders[condHeader] = res.headers.get('etag')
newHeaders[condHeader] = cachedRes.headers.get('etag')
}
if (res.headers.get('last-modified')) {
if (cachedRes.headers.get('last-modified')) {
const condHeader = opts.method && opts.method.toLowerCase() !== 'get'
? 'if-unmodified-since'
: 'if-modified-since'
newHeaders[condHeader] = res.headers.get('last-modified')
newHeaders[condHeader] = cachedRes.headers.get('last-modified')
}
opts.headers = newHeaders
return remoteFetch(uri, opts).then(condRes => {
if (condRes.status === 304) {
condRes.body = res.body
condRes.body = cachedRes.body
} else if (condRes.status >= 500) {
if (condRes.method.toLowerCase() === 'get') {
return cachedRes
} else {
return opts.cache.delete(uri).then(() => cachedRes)
}
}
if (condRes.method.toLowerCase() !== 'get') {
return opts.cache.delete(uri).then(() => condRes)
} else {
return condRes
}
return condRes
}).catch(() => {
return cachedRes
})
}

function remoteFetch (uri, opts) {
const agent = getAgent(uri, opts)
const headers = {
'connection': agent ? 'keep-alive' : 'close',
'connection': agent != null ? 'keep-alive' : 'close',
'user-agent': `${pkg.name}/${pkg.version} (+https://npm.im/${pkg.name})`
}
if (opts.headers) {
Expand All @@ -138,9 +150,15 @@ function remoteFetch (uri, opts) {
timeout: opts.timeout || 0
})
return fetch(req).then(res => {
if (opts.cache && opts.cacheMode !== 'no-store' && res.status < 300 && res.status >= 200) {
if (
req.method.toLowerCase() === 'get' &&
opts.cache &&
opts.cacheMode !== 'no-store' &&
res.status < 300 &&
res.status >= 200
) {
return opts.cache.put(req, res, opts.cacheOpts)
} else if (req.method !== 'POST' && res.status >= 500) {
} else if (req.method.toLowerCase() !== 'post' && res.status >= 500) {
return retryHandler(res)
} else {
return res
Expand Down

0 comments on commit fe78fac

Please sign in to comment.