refactor: move cache headers logic into reusable functions (#4533)

* refactor: move cache headers logic into reusable functions

* dev

---------

Co-authored-by: Alexandr <qwerty541zxc@gmail.com>
This commit is contained in:
Alexandr Garbuzov
2025-10-07 00:13:21 +03:00
committed by GitHub
co-authored by Alexandr
parent ade7d53653
commit 09627ce715
10 changed files with 100 additions and 94 deletions
+20 -22
View File
@@ -179,9 +179,9 @@ describe("Test /api/", () => {
["Content-Type", "image/svg+xml"],
[
"Cache-Control",
`max-age=${CONSTANTS.CARD_CACHE_SECONDS}, s-maxage=${
CONSTANTS.CARD_CACHE_SECONDS
}, stale-while-revalidate=${CONSTANTS.ONE_DAY}`,
`max-age=${CONSTANTS.CARD_CACHE_SECONDS}, ` +
`s-maxage=${CONSTANTS.CARD_CACHE_SECONDS}, ` +
`stale-while-revalidate=${CONSTANTS.ONE_DAY}`,
],
]);
});
@@ -195,11 +195,9 @@ describe("Test /api/", () => {
["Content-Type", "image/svg+xml"],
[
"Cache-Control",
`max-age=${
cache_seconds
}, s-maxage=${cache_seconds}, stale-while-revalidate=${
CONSTANTS.ONE_DAY
}`,
`max-age=${cache_seconds}, ` +
`s-maxage=${cache_seconds}, ` +
`stale-while-revalidate=${CONSTANTS.ONE_DAY}`,
],
]);
});
@@ -212,25 +210,25 @@ describe("Test /api/", () => {
["Content-Type", "image/svg+xml"],
[
"Cache-Control",
`max-age=${CONSTANTS.ERROR_CACHE_SECONDS / 2}, s-maxage=${
CONSTANTS.ERROR_CACHE_SECONDS
}, stale-while-revalidate=${CONSTANTS.ONE_DAY}`,
`max-age=${CONSTANTS.ERROR_CACHE_SECONDS}, ` +
`s-maxage=${CONSTANTS.ERROR_CACHE_SECONDS}, ` +
`stale-while-revalidate=${CONSTANTS.ONE_DAY}`,
],
]);
});
it("should set proper cache with clamped values", async () => {
{
let { req, res } = faker({ cache_seconds: 200000 }, data_stats);
let { req, res } = faker({ cache_seconds: 200_000 }, data_stats);
await api(req, res);
expect(res.setHeader.mock.calls).toEqual([
["Content-Type", "image/svg+xml"],
[
"Cache-Control",
`max-age=${CONSTANTS.TWO_DAY}, s-maxage=${
CONSTANTS.TWO_DAY
}, stale-while-revalidate=${CONSTANTS.ONE_DAY}`,
`max-age=${CONSTANTS.TWO_DAY}, ` +
`s-maxage=${CONSTANTS.TWO_DAY}, ` +
`stale-while-revalidate=${CONSTANTS.ONE_DAY}`,
],
]);
}
@@ -244,24 +242,24 @@ describe("Test /api/", () => {
["Content-Type", "image/svg+xml"],
[
"Cache-Control",
`max-age=${CONSTANTS.ONE_DAY}, s-maxage=${
CONSTANTS.ONE_DAY
}, stale-while-revalidate=${CONSTANTS.ONE_DAY}`,
`max-age=${CONSTANTS.TWELVE_HOURS}, ` +
`s-maxage=${CONSTANTS.TWELVE_HOURS}, ` +
`stale-while-revalidate=${CONSTANTS.ONE_DAY}`,
],
]);
}
{
let { req, res } = faker({ cache_seconds: -10000 }, data_stats);
let { req, res } = faker({ cache_seconds: -10_000 }, data_stats);
await api(req, res);
expect(res.setHeader.mock.calls).toEqual([
["Content-Type", "image/svg+xml"],
[
"Cache-Control",
`max-age=${CONSTANTS.TWELVE_HOURS}, s-maxage=${
CONSTANTS.TWELVE_HOURS
}, stale-while-revalidate=${CONSTANTS.ONE_DAY}`,
`max-age=${CONSTANTS.TWELVE_HOURS}, ` +
`s-maxage=${CONSTANTS.TWELVE_HOURS}, ` +
`stale-while-revalidate=${CONSTANTS.ONE_DAY}`,
],
]);
}
+1 -1
View File
@@ -188,7 +188,7 @@ describe("Test /api/gist", () => {
expect(res.setHeader).toBeCalledWith("Content-Type", "image/svg+xml");
expect(res.setHeader).toBeCalledWith(
"Cache-Control",
`max-age=${CONSTANTS.TWO_DAY}, s-maxage=${CONSTANTS.TWO_DAY}`,
`max-age=${CONSTANTS.TWO_DAY}, s-maxage=${CONSTANTS.TWO_DAY}, stale-while-revalidate=${CONSTANTS.ONE_DAY}`,
);
});
});
+3 -3
View File
@@ -224,9 +224,9 @@ describe("Test /api/pin", () => {
expect(res.setHeader).toBeCalledWith("Content-Type", "image/svg+xml");
expect(res.setHeader).toBeCalledWith(
"Cache-Control",
`max-age=${CONSTANTS.PIN_CARD_CACHE_SECONDS}, s-maxage=${
CONSTANTS.PIN_CARD_CACHE_SECONDS
}`,
`max-age=${CONSTANTS.PIN_CARD_CACHE_SECONDS}, ` +
`s-maxage=${CONSTANTS.PIN_CARD_CACHE_SECONDS}, ` +
`stale-while-revalidate=${CONSTANTS.ONE_DAY}`,
);
});
});
+3 -3
View File
@@ -230,9 +230,9 @@ describe("Test /api/top-langs", () => {
expect(res.setHeader).toBeCalledWith("Content-Type", "image/svg+xml");
expect(res.setHeader).toBeCalledWith(
"Cache-Control",
`max-age=${CONSTANTS.TOP_LANGS_CACHE_SECONDS / 2}, s-maxage=${
CONSTANTS.TOP_LANGS_CACHE_SECONDS
}`,
`max-age=${CONSTANTS.TOP_LANGS_CACHE_SECONDS}, ` +
`s-maxage=${CONSTANTS.TOP_LANGS_CACHE_SECONDS}, ` +
`stale-while-revalidate=${CONSTANTS.ONE_DAY}`,
);
});
});