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:
1 parent
ade7d53653
commit
09627ce715
10 files changed
+100
-94
No files matched your search
+8
-12
@@ -2,7 +2,11 @@
|
||||
|
||||
import { renderStatsCard } from "../src/cards/stats.js";
|
||||
import { blacklist } from "../src/common/blacklist.js";
|
||||
import { resolveCacheSeconds } from "../src/common/cache.js";
|
||||
import {
|
||||
resolveCacheSeconds,
|
||||
setCacheHeaders,
|
||||
setErrorCacheHeaders,
|
||||
} from "../src/common/cache.js";
|
||||
import { whitelist } from "../src/common/envs.js";
|
||||
import {
|
||||
CONSTANTS,
|
||||
@@ -104,16 +108,13 @@ export default async (req, res) => {
|
||||
parseInt(commits_year, 10),
|
||||
);
|
||||
const cacheSeconds = resolveCacheSeconds({
|
||||
requested: cache_seconds,
|
||||
requested: parseInt(cache_seconds, 10),
|
||||
def: CONSTANTS.CARD_CACHE_SECONDS,
|
||||
min: CONSTANTS.TWELVE_HOURS,
|
||||
max: CONSTANTS.TWO_DAY,
|
||||
});
|
||||
|
||||
res.setHeader(
|
||||
"Cache-Control",
|
||||
`max-age=${cacheSeconds}, s-maxage=${cacheSeconds}, stale-while-revalidate=${CONSTANTS.ONE_DAY}`,
|
||||
);
|
||||
setCacheHeaders(res, cacheSeconds);
|
||||
|
||||
return res.send(
|
||||
renderStatsCard(stats, {
|
||||
@@ -144,12 +145,7 @@ export default async (req, res) => {
|
||||
}),
|
||||
);
|
||||
} catch (err) {
|
||||
res.setHeader(
|
||||
"Cache-Control",
|
||||
`max-age=${CONSTANTS.ERROR_CACHE_SECONDS / 2}, s-maxage=${
|
||||
CONSTANTS.ERROR_CACHE_SECONDS
|
||||
}, stale-while-revalidate=${CONSTANTS.ONE_DAY}`,
|
||||
); // Use lower cache period for errors.
|
||||
setErrorCacheHeaders(res);
|
||||
return res.send(
|
||||
renderError(err.message, err.secondaryMessage, {
|
||||
title_color,
|
||||
|
||||
Reference in new issue
Block a user