Skip to content

Commit 9f01d46

Browse files
committed
Fix min-fresh
1 parent 184f54b commit 9f01d46

File tree

2 files changed

+14
-6
lines changed

2 files changed

+14
-6
lines changed

index.js

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -247,10 +247,7 @@ module.exports = class CachePolicy {
247247
return false;
248248
}
249249

250-
if (
251-
requestCC['min-fresh'] &&
252-
this.timeToLive() < 1000 * requestCC['min-fresh']
253-
) {
250+
if (requestCC['min-fresh'] && this.maxAge() - this.age() < toNumberOrZero(requestCC['min-fresh'])) {
254251
return false;
255252
}
256253

@@ -455,9 +452,9 @@ module.exports = class CachePolicy {
455452
}
456453

457454
/**
458-
* Up-to-date `max-age` value, in *milliseconds*.
455+
* Remaining time this cache entry may be useful for, in *milliseconds*.
459456
*
460-
* Prefer this method over `maxAge()`.
457+
* Prefer this method over `maxAge()`, because it includes other factors.
461458
*/
462459
timeToLive() {
463460
const age = this.maxAge() - this.age();

test/okhttptest.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -369,6 +369,17 @@ describe('okhttp tests', function() {
369369
);
370370
});
371371

372+
it('request min fresh with stale-while-revalidate', function() {
373+
const cache = new CachePolicy(
374+
{ headers: {} },
375+
{ headers: {'cache-control': 'max-age=60, stale-while-revalidate=100000'} },
376+
{ shared: false }
377+
);
378+
379+
assert(!cache.satisfiesWithoutRevalidation({headers: {'cache-control': 'min-fresh=120'}}));
380+
assert(cache.satisfiesWithoutRevalidation({headers: {'cache-control': 'min-fresh=10'}}));
381+
});
382+
372383
it('request max stale', function() {
373384
const cache = new CachePolicy(
374385
{ headers: {} },

0 commit comments

Comments
 (0)