diff --git a/src/interceptors/request.ts b/src/interceptors/request.ts index 92a1a33..8046252 100644 --- a/src/interceptors/request.ts +++ b/src/interceptors/request.ts @@ -182,7 +182,7 @@ export function defaultRequestInterceptor(axios: AxiosCacheInstance): RequestInt config ); - if (cache.state === 'stale' || cache.state === 'must-revalidate') { + if ((cache.state === 'stale' || cache.state === 'must-revalidate') && !overrideCache) { updateStaleRequest(cache, config as ConfigWithCache); if (__ACI_DEV__) { diff --git a/test/interceptors/etag.test.ts b/test/interceptors/etag.test.ts index d9f0eb7..ca5551f 100644 --- a/test/interceptors/etag.test.ts +++ b/test/interceptors/etag.test.ts @@ -87,4 +87,26 @@ describe('ETag handling', () => { assert.equal(response2.config.headers?.[Header.IfNoneMatch], 'fake-etag'); assert.equal(response2.headers?.[Header.ETag], 'fake-etag-2'); }); + + it('No If-None-Match header when override is true', async () => { + const axios = mockAxios({}, { etag: 'fakeEtag', 'cache-control': 'max-age=1' }); + const config = { cache: { interpretHeader: true, etag: true } }; + + // Initial request to populate cache + const response1 = await axios.get('http://test.com', config); + assert.equal(response1.cached, false); + assert.equal(response1.status, 200); + + // Sleep to make cache stale + await mockDateNow(1000); + + // Request with override should not send If-None-Match and should get 200, not 304 + const response2 = await axios.get('http://test.com', { + cache: { ...config.cache, override: true } + }); + assert.equal(response2.cached, false); + assert.equal(response2.stale, undefined); + assert.equal(response2.status, 200); // Should be 200, not 304 + assert.equal(response2.config.headers?.[Header.IfNoneMatch], undefined); + }); }); diff --git a/test/interceptors/last-modified.test.ts b/test/interceptors/last-modified.test.ts index 80e2c4b..2ea0bb2 100644 --- a/test/interceptors/last-modified.test.ts +++ b/test/interceptors/last-modified.test.ts @@ -135,4 +135,37 @@ describe('LastModified handling', () => { assert.notEqual(Number(secondMyHeader), Number.NaN); assert.notEqual(secondMyHeader, firstMyHeader); }); + + it('No If-Modified-Since header when override is true', async () => { + const axios = mockAxios( + {}, + { + 'last-modified': 'Wed, 21 Oct 2015 07:28:00 GMT', + 'cache-control': 'max-age=1' + } + ); + + const config: CacheRequestConfig = { + id: 'same request', + cache: { interpretHeader: true, modifiedSince: true } + }; + + // Initial request to populate cache + const response1 = await axios.get('url', config); + assert.equal(response1.cached, false); + assert.equal(response1.status, 200); + + // Sleep to make cache stale + await mockDateNow(1000); + + // Request with override should not send If-Modified-Since and should get 200, not 304 + const response2 = await axios.get('url', { + ...config, + cache: { ...config.cache, override: true } + }); + assert.equal(response2.cached, false); + assert.equal(response2.stale, undefined); + assert.equal(response2.status, 200); // Should be 200, not 304 + assert.equal(response2.config.headers?.[Header.IfModifiedSince], undefined); + }); });