diff --git a/api/index.js b/api/index.js index 62286a3..68c34e0 100644 --- a/api/index.js +++ b/api/index.js @@ -37,32 +37,32 @@ module.exports = async (req, res) => { parseBoolean(count_private), parseBoolean(include_all_commits) ); + + const cacheSeconds = clampValue( + parseInt(cache_seconds || CONSTANTS.TWO_HOURS, 10), + CONSTANTS.TWO_HOURS, + CONSTANTS.ONE_DAY + ); + + res.setHeader("Cache-Control", `public, max-age=${cacheSeconds}`); + + return res.send( + renderStatsCard(stats, { + hide: parseArray(hide), + show_icons: parseBoolean(show_icons), + hide_title: parseBoolean(hide_title), + hide_border: parseBoolean(hide_border), + hide_rank: parseBoolean(hide_rank), + include_all_commits: parseBoolean(include_all_commits), + line_height, + title_color, + icon_color, + text_color, + bg_color, + theme, + }) + ); } catch (err) { return res.send(renderError(err.message, err.secondaryMessage)); } - - const cacheSeconds = clampValue( - parseInt(cache_seconds || CONSTANTS.TWO_HOURS, 10), - CONSTANTS.TWO_HOURS, - CONSTANTS.ONE_DAY - ); - - res.setHeader("Cache-Control", `public, max-age=${cacheSeconds}`); - - res.send( - renderStatsCard(stats, { - hide: parseArray(hide), - show_icons: parseBoolean(show_icons), - hide_title: parseBoolean(hide_title), - hide_border: parseBoolean(hide_border), - hide_rank: parseBoolean(hide_rank), - include_all_commits: parseBoolean(include_all_commits), - line_height, - title_color, - icon_color, - text_color, - bg_color, - theme, - }) - ); }; diff --git a/api/pin.js b/api/pin.js index a920061..3fc5058 100644 --- a/api/pin.js +++ b/api/pin.js @@ -27,39 +27,39 @@ module.exports = async (req, res) => { try { repoData = await fetchRepo(username, repo); - } catch (err) { - return res.send(renderError(err.message, err.secondaryMessage)); - } - let cacheSeconds = clampValue( - parseInt(cache_seconds || CONSTANTS.TWO_HOURS, 10), - CONSTANTS.TWO_HOURS, - CONSTANTS.ONE_DAY - ); + let cacheSeconds = clampValue( + parseInt(cache_seconds || CONSTANTS.TWO_HOURS, 10), + CONSTANTS.TWO_HOURS, + CONSTANTS.ONE_DAY + ); - /* + /* if star count & fork count is over 1k then we are kFormating the text and if both are zero we are not showing the stats so we can just make the cache longer, since there is no need to frequent updates */ - const stars = repoData.stargazers.totalCount; - const forks = repoData.forkCount; - const isBothOver1K = stars > 1000 && forks > 1000; - const isBothUnder1 = stars < 1 && forks < 1; - if (!cache_seconds && (isBothOver1K || isBothUnder1)) { - cacheSeconds = CONSTANTS.FOUR_HOURS; + const stars = repoData.stargazers.totalCount; + const forks = repoData.forkCount; + const isBothOver1K = stars > 1000 && forks > 1000; + const isBothUnder1 = stars < 1 && forks < 1; + if (!cache_seconds && (isBothOver1K || isBothUnder1)) { + cacheSeconds = CONSTANTS.FOUR_HOURS; + } + + res.setHeader("Cache-Control", `public, max-age=${cacheSeconds}`); + + return res.send( + renderRepoCard(repoData, { + title_color, + icon_color, + text_color, + bg_color, + theme, + show_owner: parseBoolean(show_owner), + }) + ); + } catch (err) { + return res.send(renderError(err.message, err.secondaryMessage)); } - - res.setHeader("Cache-Control", `public, max-age=${cacheSeconds}`); - - res.send( - renderRepoCard(repoData, { - title_color, - icon_color, - text_color, - bg_color, - theme, - show_owner: parseBoolean(show_owner), - }) - ); }; diff --git a/api/top-langs.js b/api/top-langs.js index c473bd5..8950158 100644 --- a/api/top-langs.js +++ b/api/top-langs.js @@ -29,29 +29,29 @@ module.exports = async (req, res) => { try { topLangs = await fetchTopLanguages(username); + + const cacheSeconds = clampValue( + parseInt(cache_seconds || CONSTANTS.TWO_HOURS, 10), + CONSTANTS.TWO_HOURS, + CONSTANTS.ONE_DAY + ); + + res.setHeader("Cache-Control", `public, max-age=${cacheSeconds}`); + + return res.send( + renderTopLanguages(topLangs, { + hide_title: parseBoolean(hide_title), + hide_border: parseBoolean(hide_border), + card_width: parseInt(card_width, 10), + hide: parseArray(hide), + title_color, + text_color, + bg_color, + theme, + layout, + }) + ); } catch (err) { return res.send(renderError(err.message, err.secondaryMessage)); } - - const cacheSeconds = clampValue( - parseInt(cache_seconds || CONSTANTS.TWO_HOURS, 10), - CONSTANTS.TWO_HOURS, - CONSTANTS.ONE_DAY - ); - - res.setHeader("Cache-Control", `public, max-age=${cacheSeconds}`); - - res.send( - renderTopLanguages(topLangs, { - hide_title: parseBoolean(hide_title), - hide_border: parseBoolean(hide_border), - card_width: parseInt(card_width, 10), - hide: parseArray(hide), - title_color, - text_color, - bg_color, - theme, - layout, - }) - ); };