Skip to content

Commit 5bdc0ec

Browse files
Embraser01kornelski
authored andcommitted
Better support stale-while-revalidate option
1 parent ac15ac7 commit 5bdc0ec

File tree

2 files changed

+86
-3
lines changed

2 files changed

+86
-3
lines changed

index.js

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ const understoodStatuses = new Set([
3636
const errorStatusCodes = new Set([
3737
500,
3838
502,
39-
503,
39+
503,
4040
504,
4141
]);
4242

@@ -255,8 +255,16 @@ module.exports = class CachePolicy {
255255
// the stored response is either:
256256
// fresh, or allowed to be served stale
257257
if (this.stale()) {
258-
const allowsStale = requestCC['max-stale'] &&
259-
(true === requestCC['max-stale'] || requestCC['max-stale'] > this.age() - this.maxAge());
258+
let allowsStale = false;
259+
if (requestCC['max-stale'] && !this._rescc['must-revalidate']) {
260+
if (requestCC['max-stale'] === true || requestCC['max-stale'] > this.age() - this.maxAge()) {
261+
allowsStale = true;
262+
}
263+
// Allow stale-while-revalidate queries to be served stale
264+
// even if must-revalidate is set as the revalidation should be happening in the background
265+
} else if (this.useStaleWhileRevalidate()) {
266+
allowsStale = true;
267+
}
260268

261269
if (!allowsStale) {
262270
return false;

test/okhttptest.js

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,81 @@ describe('okhttp tests', function() {
210210
assert.equal(cache.timeToLive(), 260000);
211211
});
212212

213+
it('stale-while-revalidate satisfies when stale', function() {
214+
const cache = new CachePolicy(
215+
{ headers: {} },
216+
{
217+
headers: {
218+
age: 120,
219+
'cache-control': 'max-age=60, stale-while-revalidate=200',
220+
},
221+
},
222+
{ shared: false }
223+
);
224+
225+
assert(cache.stale());
226+
assert(cache.useStaleWhileRevalidate());
227+
assert(cache.satisfiesWithoutRevalidation({ headers: {} }));
228+
});
229+
230+
it('stale-while-revalidate satisfies when stale and must-revalidate', function() {
231+
const cache = new CachePolicy(
232+
{ headers: {} },
233+
{
234+
headers: {
235+
age: 120,
236+
'cache-control': 'max-age=60, stale-while-revalidate=200, must-revalidate',
237+
},
238+
},
239+
{ shared: false }
240+
);
241+
242+
assert(cache.stale());
243+
assert(cache.satisfiesWithoutRevalidation({ headers: {} }));
244+
});
245+
246+
it('stale-while-revalidate work with max-stale', function() {
247+
const cache = new CachePolicy(
248+
{ headers: {} },
249+
{
250+
headers: {
251+
age: 100,
252+
'cache-control': 'max-age=60, stale-while-revalidate=200',
253+
},
254+
},
255+
{ shared: false }
256+
);
257+
258+
assert(cache.stale());
259+
assert(cache.satisfiesWithoutRevalidation({
260+
headers: {
261+
'cache-control': 'max-stale',
262+
}
263+
}));
264+
assert(!cache.satisfiesWithoutRevalidation({
265+
headers: {
266+
'cache-control': 'max-stale=40',
267+
}
268+
}));
269+
});
270+
271+
it('stale-while-revalidate not satisfies when stale and expired', function() {
272+
const cache = new CachePolicy(
273+
{ headers: {} },
274+
{
275+
headers: {
276+
age: 260,
277+
'cache-control': 'max-age=60, stale-while-revalidate=200',
278+
},
279+
},
280+
{ shared: false }
281+
);
282+
283+
assert(cache.stale());
284+
assert(!cache.useStaleWhileRevalidate());
285+
assert(!cache.satisfiesWithoutRevalidation({ headers: {} }));
286+
});
287+
213288
it('max age preferred over lower shared max age', function() {
214289
const cache = new CachePolicy(
215290
{ headers: {} },

0 commit comments

Comments
 (0)