Skip to content

Commit c0e634d

Browse files
authored
cache: honour unqualified no-cache response directive (#4178)
* cache: honour unqualified no-cache response directive Closes #4173 * move test to interceptors/cache.js * lint * remove duplicate close() --------- Co-authored-by: alxndrsn <alxndrsn>
1 parent fe7aa83 commit c0e634d

2 files changed

Lines changed: 45 additions & 1 deletion

File tree

lib/interceptor/cache.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,12 @@ const { AbortError } = require('../core/errors.js')
2020
*/
2121
function needsRevalidation (result, cacheControlDirectives) {
2222
if (cacheControlDirectives?.['no-cache']) {
23-
// Always revalidate requests with the no-cache directive
23+
// Always revalidate requests with the no-cache request directive
24+
return true
25+
}
26+
27+
if (result.cacheControlDirectives?.['no-cache'] && !Array.isArray(result.cacheControlDirectives['no-cache'])) {
28+
// Always revalidate requests with unqualified no-cache response directive
2429
return true
2530
}
2631

test/interceptors/cache.js

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,45 @@ describe('Cache Interceptor', () => {
128128
}
129129
})
130130

131+
test('revalidates reponses with no-cache directive, regardless of cacheByDefault', async () => {
132+
let requestCount = 0
133+
const server = createServer({ joinDuplicateHeaders: true }, (req, res) => {
134+
++requestCount
135+
res.setHeader('Vary', 'Accept-Encoding')
136+
res.setHeader('cache-control', 'no-cache')
137+
res.end(`Request count: ${requestCount}`)
138+
}).listen(0)
139+
140+
after(async () => {
141+
server.close()
142+
143+
await once(server, 'close')
144+
})
145+
146+
await once(server, 'listening')
147+
148+
const client = new Client(`http://localhost:${server.address().port}`)
149+
.compose(interceptors.cache({
150+
cacheByDefault: 1000
151+
}))
152+
153+
const request = {
154+
origin: 'localhost',
155+
method: 'GET',
156+
path: '/'
157+
}
158+
159+
const res1 = await client.request(request)
160+
const body1 = await res1.body.text()
161+
strictEqual(body1, 'Request count: 1')
162+
strictEqual(requestCount, 1)
163+
164+
const res2 = await client.request(request)
165+
const body2 = await res2.body.text()
166+
strictEqual(body2, 'Request count: 2')
167+
strictEqual(requestCount, 2)
168+
})
169+
131170
test('stale responses are revalidated before deleteAt (if-modified-since)', async () => {
132171
const clock = FakeTimers.install({
133172
shouldClearNativeTimers: true

0 commit comments

Comments
 (0)