[CST-5729] Change in order to save header only if configured and existing

This commit is contained in:
Giuseppe Digilio
2023-06-14 18:26:55 +02:00
parent ac9be25faf
commit 2f06a7cb17
3 changed files with 23 additions and 7 deletions

View File

@@ -375,13 +375,13 @@ function cacheCheck(req, res, next) {
// If cached copy exists, return it to the user. // If cached copy exists, return it to the user.
if (cachedCopy && cachedCopy.page) { if (cachedCopy && cachedCopy.page) {
if (cachedCopy.headers && Array.isArray(environment.cache.serverSide.headers) && environment.cache.serverSide.headers.length > 0) { if (cachedCopy.headers) {
environment.cache.serverSide.headers.forEach((header) => { Object.keys(cachedCopy.headers).forEach((header) => {
if (cachedCopy.headers[header.toLowerCase()]) { if (cachedCopy.headers[header]) {
if (environment.cache.serverSide.debug) { if (environment.cache.serverSide.debug) {
console.log(`Restore cached ${header} header`); console.log(`Restore cached ${header} header`);
} }
res.setHeader(header, cachedCopy.headers[header.toLowerCase()]); res.setHeader(header, cachedCopy.headers[header]);
} }
}); });
} }
@@ -462,8 +462,8 @@ function saveToCache(req, page: any) {
// Avoid caching "/reload/[random]" paths (these are hard refreshes after logout) // Avoid caching "/reload/[random]" paths (these are hard refreshes after logout)
if (key.startsWith('/reload')) { return; } if (key.startsWith('/reload')) { return; }
// Retrieve response headers // Retrieve response headers to save, if any
const headers = req.res.getHeaders(); const headers = retrieveHeaders(req.res);
// If bot cache is enabled, save it to that cache if it doesn't exist or is expired // If bot cache is enabled, save it to that cache if it doesn't exist or is expired
// (NOTE: has() will return false if page is expired in cache) // (NOTE: has() will return false if page is expired in cache)
if (botCacheEnabled() && !botCache.has(key)) { if (botCacheEnabled() && !botCache.has(key)) {
@@ -479,6 +479,21 @@ function saveToCache(req, page: any) {
} }
} }
function retrieveHeaders(response) {
const headers = Object.create({});
if (Array.isArray(environment.cache.serverSide.headers) && environment.cache.serverSide.headers.length > 0) {
environment.cache.serverSide.headers.forEach((header) => {
if (response.hasHeader(header)) {
if (environment.cache.serverSide.debug) {
console.log(`Save ${header} header to cache`);
}
headers[header] = response.getHeader(header);
}
});
}
return headers;
}
/** /**
* Whether a user is authenticated or not * Whether a user is authenticated or not
*/ */

View File

@@ -13,7 +13,7 @@ export interface CacheConfig extends Config {
serverSide: { serverSide: {
// Debug server-side caching. Set to true to see cache hits/misses/refreshes in console logs. // Debug server-side caching. Set to true to see cache hits/misses/refreshes in console logs.
debug: boolean, debug: boolean,
// List of headers to restore from the cache hit // List of response headers to save into the cache
headers: string[], headers: string[],
// Cache specific to known bots. Allows you to serve cached contents to bots only. // Cache specific to known bots. Allows you to serve cached contents to bots only.
botCache: { botCache: {

View File

@@ -59,6 +59,7 @@ export const environment: BuildConfig = {
// In-memory cache of server-side rendered pages. Disabled in test environment (max=0) // In-memory cache of server-side rendered pages. Disabled in test environment (max=0)
serverSide: { serverSide: {
debug: false, debug: false,
headers: ['Link'],
botCache: { botCache: {
max: 0, max: 0,
timeToLive: 24 * 60 * 60 * 1000, // 1 day timeToLive: 24 * 60 * 60 * 1000, // 1 day