feat: add support for "s-maxage" directive in "Cache-Control" header
This commit is contained in:
parent
e59d95bce0
commit
5b5f7bb0e7
2 changed files with 18 additions and 2 deletions
|
@ -20,12 +20,18 @@ function shouldCacheResponse(cacheControl, isAuthenticated) {
|
|||
if (cacheControl["public"]) {
|
||||
return true;
|
||||
}
|
||||
return !isAuthenticated && cacheControl["max-age"] !== undefined;
|
||||
return (
|
||||
!isAuthenticated &&
|
||||
(cacheControl["max-age"] !== undefined ||
|
||||
cacheControl["s-maxage"] !== undefined)
|
||||
);
|
||||
}
|
||||
|
||||
function isCacheValid(entry, requestHeaders) {
|
||||
const { timestamp, cacheControl } = entry;
|
||||
const maxAge = parseInt(cacheControl["max-age"], 10);
|
||||
const maxAge = cacheControl["s-maxage"]
|
||||
? parseInt(cacheControl["s-maxage"], 10)
|
||||
: parseInt(cacheControl["max-age"], 10);
|
||||
if (Date.now() - timestamp > maxAge * 1000) {
|
||||
return false;
|
||||
}
|
||||
|
|
|
@ -81,6 +81,16 @@ describe("shouldCacheResponse", () => {
|
|||
expect(shouldCacheResponse(cacheControl, true)).toBe(false);
|
||||
});
|
||||
|
||||
test("should return true if s-maxage is present and not authenticated", () => {
|
||||
const cacheControl = { "s-maxage": "3600" };
|
||||
expect(shouldCacheResponse(cacheControl, false)).toBe(true);
|
||||
});
|
||||
|
||||
test("should return false if s-maxage is present and authenticated", () => {
|
||||
const cacheControl = { "s-maxage": "3600" };
|
||||
expect(shouldCacheResponse(cacheControl, true)).toBe(false);
|
||||
});
|
||||
|
||||
test("should return false if no relevant directives are present", () => {
|
||||
const cacheControl = {};
|
||||
expect(shouldCacheResponse(cacheControl, false)).toBe(false);
|
||||
|
|
Loading…
Reference in a new issue