feat: disable cache with proper headers when CACHE_SECONDS env is zero (#4539)

* feat: disable cache with proper headers when CACHE_SECONDS env is zero

* Update src/common/cache.js

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>

---------

Co-authored-by: Alexandr <qwerty541zxc@gmail.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
This commit is contained in:
Ophelia Goldstein
2025-10-08 01:19:54 +03:00
committed by GitHub
co-authored by Copilot Alexandr
parent 66764616fa
commit e8e5cf86fb
2 changed files with 71 additions and 4 deletions
+34 -3
View File
@@ -15,13 +15,31 @@ import { clampValue, CONSTANTS } from "./utils.js";
const resolveCacheSeconds = ({ requested, def, min, max }) => {
let cacheSeconds = clampValue(isNaN(requested) ? def : requested, min, max);
cacheSeconds = process.env.CACHE_SECONDS
? parseInt(process.env.CACHE_SECONDS, 10) || cacheSeconds
: cacheSeconds;
if (process.env.CACHE_SECONDS) {
const envCacheSeconds = parseInt(process.env.CACHE_SECONDS, 10);
if (!isNaN(envCacheSeconds)) {
cacheSeconds = envCacheSeconds;
}
}
return cacheSeconds;
};
/**
* Disables caching by setting appropriate headers on the response object.
*
* @param {Object} res The response object.
*/
const disableCaching = (res) => {
// Disable caching for browsers, shared caches/CDNs, and GitHub Camo.
res.setHeader(
"Cache-Control",
"no-cache, no-store, must-revalidate, max-age=0, s-maxage=0",
);
res.setHeader("Pragma", "no-cache");
res.setHeader("Expires", "0");
};
/**
* Sets the Cache-Control headers on the response object.
*
@@ -29,6 +47,11 @@ const resolveCacheSeconds = ({ requested, def, min, max }) => {
* @param {number} cacheSeconds The cache seconds to set in the headers.
*/
const setCacheHeaders = (res, cacheSeconds) => {
if (cacheSeconds < 1) {
disableCaching(res);
return;
}
res.setHeader(
"Cache-Control",
`max-age=${cacheSeconds}, ` +
@@ -43,6 +66,14 @@ const setCacheHeaders = (res, cacheSeconds) => {
* @param {Object} res The response object.
*/
const setErrorCacheHeaders = (res) => {
const envCacheSeconds = process.env.CACHE_SECONDS
? parseInt(process.env.CACHE_SECONDS, 10)
: NaN;
if (!isNaN(envCacheSeconds) && envCacheSeconds < 1) {
disableCaching(res);
return;
}
// Use lower cache period for errors.
res.setHeader(
"Cache-Control",