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:
co-authored by
Alexandr
parent
ade7d53653
commit
09627ce715
+8
-12
@@ -5,7 +5,11 @@ import { gistWhitelist } from "../src/common/envs.js";
|
||||
import { isLocaleAvailable } from "../src/translations.js";
|
||||
import { renderGistCard } from "../src/cards/gist.js";
|
||||
import { fetchGist } from "../src/fetchers/gist.js";
|
||||
import { resolveCacheSeconds } from "../src/common/cache.js";
|
||||
import {
|
||||
resolveCacheSeconds,
|
||||
setCacheHeaders,
|
||||
setErrorCacheHeaders,
|
||||
} from "../src/common/cache.js";
|
||||
|
||||
export default async (req, res) => {
|
||||
const {
|
||||
@@ -57,16 +61,13 @@ export default async (req, res) => {
|
||||
try {
|
||||
const gistData = await fetchGist(id);
|
||||
const cacheSeconds = resolveCacheSeconds({
|
||||
requested: cache_seconds,
|
||||
requested: parseInt(cache_seconds, 10),
|
||||
def: CONSTANTS.TWO_DAY,
|
||||
min: CONSTANTS.TWO_DAY,
|
||||
max: CONSTANTS.SIX_DAY,
|
||||
});
|
||||
|
||||
res.setHeader(
|
||||
"Cache-Control",
|
||||
`max-age=${cacheSeconds}, s-maxage=${cacheSeconds}`,
|
||||
);
|
||||
setCacheHeaders(res, cacheSeconds);
|
||||
|
||||
return res.send(
|
||||
renderGistCard(gistData, {
|
||||
@@ -83,12 +84,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,
|
||||
|
||||
+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,
|
||||
|
||||
+8
-12
@@ -2,7 +2,11 @@
|
||||
|
||||
import { renderRepoCard } from "../src/cards/repo.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, parseBoolean, renderError } from "../src/common/utils.js";
|
||||
import { fetchRepo } from "../src/fetchers/repo.js";
|
||||
@@ -77,16 +81,13 @@ export default async (req, res) => {
|
||||
try {
|
||||
const repoData = await fetchRepo(username, repo);
|
||||
const cacheSeconds = resolveCacheSeconds({
|
||||
requested: cache_seconds,
|
||||
requested: parseInt(cache_seconds, 10),
|
||||
def: CONSTANTS.PIN_CARD_CACHE_SECONDS,
|
||||
min: CONSTANTS.ONE_DAY,
|
||||
max: CONSTANTS.TEN_DAY,
|
||||
});
|
||||
|
||||
res.setHeader(
|
||||
"Cache-Control",
|
||||
`max-age=${cacheSeconds}, s-maxage=${cacheSeconds}`,
|
||||
);
|
||||
setCacheHeaders(res, cacheSeconds);
|
||||
|
||||
return res.send(
|
||||
renderRepoCard(repoData, {
|
||||
@@ -104,12 +105,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,
|
||||
|
||||
+8
-12
@@ -2,7 +2,11 @@
|
||||
|
||||
import { renderTopLanguages } from "../src/cards/top-languages.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,
|
||||
@@ -106,16 +110,13 @@ export default async (req, res) => {
|
||||
count_weight,
|
||||
);
|
||||
const cacheSeconds = resolveCacheSeconds({
|
||||
requested: cache_seconds,
|
||||
requested: parseInt(cache_seconds, 10),
|
||||
def: CONSTANTS.TOP_LANGS_CACHE_SECONDS,
|
||||
min: CONSTANTS.TWO_DAY,
|
||||
max: CONSTANTS.TEN_DAY,
|
||||
});
|
||||
|
||||
res.setHeader(
|
||||
"Cache-Control",
|
||||
`max-age=${cacheSeconds / 2}, s-maxage=${cacheSeconds}`,
|
||||
);
|
||||
setCacheHeaders(res, cacheSeconds);
|
||||
|
||||
return res.send(
|
||||
renderTopLanguages(topLangs, {
|
||||
@@ -139,12 +140,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,
|
||||
|
||||
+8
-14
@@ -10,7 +10,11 @@ import {
|
||||
import { whitelist } from "../src/common/envs.js";
|
||||
import { fetchWakatimeStats } from "../src/fetchers/wakatime.js";
|
||||
import { isLocaleAvailable } from "../src/translations.js";
|
||||
import { resolveCacheSeconds } from "../src/common/cache.js";
|
||||
import {
|
||||
resolveCacheSeconds,
|
||||
setCacheHeaders,
|
||||
setErrorCacheHeaders,
|
||||
} from "../src/common/cache.js";
|
||||
|
||||
export default async (req, res) => {
|
||||
const {
|
||||
@@ -72,18 +76,13 @@ export default async (req, res) => {
|
||||
try {
|
||||
const stats = await fetchWakatimeStats({ username, api_domain });
|
||||
const cacheSeconds = resolveCacheSeconds({
|
||||
requested: cache_seconds,
|
||||
requested: parseInt(cache_seconds, 10),
|
||||
def: CONSTANTS.CARD_CACHE_SECONDS,
|
||||
min: CONSTANTS.SIX_HOURS,
|
||||
max: CONSTANTS.TWO_DAY,
|
||||
});
|
||||
|
||||
res.setHeader(
|
||||
"Cache-Control",
|
||||
`max-age=${
|
||||
cacheSeconds / 2
|
||||
}, s-maxage=${cacheSeconds}, stale-while-revalidate=${CONSTANTS.ONE_DAY}`,
|
||||
);
|
||||
setCacheHeaders(res, cacheSeconds);
|
||||
|
||||
return res.send(
|
||||
renderWakatimeCard(stats, {
|
||||
@@ -109,12 +108,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