Skip to content

Commit b5b7cfb

Browse files
committed
Probably fixed ignoring cache for blobs and arraybuffers
1 parent 2135873 commit b5b7cfb

File tree

3 files changed

+17
-7
lines changed

3 files changed

+17
-7
lines changed

src/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ function setupCache (config = {}) {
6969
if (!isFunction(next)) return next
7070

7171
// Nothing in cache so we execute the default adapter or any given adapter
72+
// Will throw if the request has a status different than 2xx
7273
res = await config.adapter(req)
7374

7475
// Process response to store in cache

src/response.js

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,10 @@ import limit from './limit'
22
import { write } from './cache'
33

44
async function response (config, req, res) {
5-
const type = res.status / 100 | 0
6-
7-
// only cache 2xx response
8-
if (type !== 2) {
9-
return res
10-
}
5+
const { request = {} } = res
116

127
// exclude binary response from cache
13-
if (['arraybuffer', 'blob'].indexOf(res.responseType) > -1) {
8+
if (['arraybuffer', 'blob'].indexOf(request.responseType) > -1) {
149
return res
1510
}
1611

test/spec/index.spec.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,20 @@ describe('Integration', () => {
4343
assert.ok(response.request.fromCache)
4444
})
4545

46+
it('Should not cache requests with a status not in the 2xx range', async () => {
47+
await api.cache.clear()
48+
49+
try {
50+
await api({ url: 'https://httpbin.org/status/404' })
51+
} catch (err) {
52+
assert.equal(err.response.status, 404)
53+
54+
const length = await api.cache.length()
55+
56+
assert.equal(length, 0)
57+
}
58+
})
59+
4660
it('Should bust cache when sending something else than a GET request', async () => {
4761
await api.cache.clear()
4862

0 commit comments

Comments
 (0)