⚙️ refactor: brotli asset serving behind a feature toggle (#13641)

This commit is contained in:
Ravi Kumar L 2026-06-10 08:49:50 -04:00 committed by GitHub
parent db863e75e3
commit 346ebea2d9
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 66 additions and 4 deletions

View file

@ -6,9 +6,10 @@ const oneDayInSeconds = 24 * 60 * 60;
const sMaxAge = process.env.STATIC_CACHE_S_MAX_AGE || oneDayInSeconds;
const maxAge = process.env.STATIC_CACHE_MAX_AGE || oneDayInSeconds * 2;
const isEnabled = (value) => value === true || String(value).toLowerCase() === 'true';
/**
* Creates an Express static middleware with optional gzip compression and configurable caching
* Creates an Express static middleware with optional precompressed asset serving and configurable caching
*
* @param {string} staticPath - The file system path to serve static files from
* @param {Object} [options={}] - Configuration options
@ -18,6 +19,7 @@ const maxAge = process.env.STATIC_CACHE_MAX_AGE || oneDayInSeconds * 2;
*/
function staticCache(staticPath, options = {}) {
const { noCache = false, skipGzipScan = false } = options;
const enableBrotli = isEnabled(process.env.ENABLE_STATIC_ASSET_BROTLI);
const setHeaders = (res, filePath) => {
if (process.env.NODE_ENV?.toLowerCase() !== 'production') {
@ -51,8 +53,8 @@ function staticCache(staticPath, options = {}) {
});
} else {
return expressStaticGzip(staticPath, {
enableBrotli: false,
orderPreference: ['gz'],
enableBrotli,
orderPreference: enableBrotli ? ['br', 'gz'] : ['gz'],
setHeaders,
index: false,
});