From 486aefebc7b3b3ac729ec4de35ebad017d179dc0 Mon Sep 17 00:00:00 2001 From: Giuseppe Digilio Date: Tue, 13 Jun 2023 19:00:33 +0200 Subject: [PATCH 1/3] [CST-5729] Add possibility to store response's headers into the SSR cache --- server.ts | 18 +++++++++++++----- src/config/cache-config.interface.ts | 2 ++ src/config/default-app-config.ts | 2 ++ 3 files changed, 17 insertions(+), 5 deletions(-) diff --git a/server.ts b/server.ts index d64b80b4ab6..791baf7236c 100644 --- a/server.ts +++ b/server.ts @@ -53,7 +53,7 @@ import { buildAppConfig } from './src/config/config.server'; import { APP_CONFIG, AppConfig } from './src/config/app-config.interface'; import { extendEnvironmentWithAppConfig } from './src/config/config.util'; import { logStartupMessage } from './startup-message'; -import { TOKENITEM } from 'src/app/core/auth/models/auth-token-info.model'; +import { TOKENITEM } from './src/app/core/auth/models/auth-token-info.model'; /* @@ -374,9 +374,15 @@ function cacheCheck(req, res, next) { } // If cached copy exists, return it to the user. - if (cachedCopy) { + if (cachedCopy && cachedCopy.page) { + if (cachedCopy.headers && Array.isArray(environment.cache.serverSide.headers) && environment.cache.serverSide.headers.length > 0) { + environment.cache.serverSide.headers.forEach((header) => { + if (environment.cache.serverSide.debug) { console.log(`Restore cached ${header} header`); } + res.setHeader(header, cachedCopy.headers[header.toLowerCase()]); + }); + } res.locals.ssr = true; // mark response as SSR-generated (enables text compression) - res.send(cachedCopy); + res.send(cachedCopy.page); // Tell Express to skip all other handlers for this path // This ensures we don't try to re-render the page since we've already returned the cached copy @@ -452,16 +458,18 @@ function saveToCache(req, page: any) { // Avoid caching "/reload/[random]" paths (these are hard refreshes after logout) if (key.startsWith('/reload')) { return; } + // Retrieve response headers + const headers = req.res.getHeaders(); // 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) if (botCacheEnabled() && !botCache.has(key)) { - botCache.set(key, page); + botCache.set(key, { page, headers }); if (environment.cache.serverSide.debug) { console.log(`CACHE SAVE FOR ${key} in bot cache.`); } } // If anonymous cache is enabled, save it to that cache if it doesn't exist or is expired if (anonymousCacheEnabled() && !anonymousCache.has(key)) { - anonymousCache.set(key, page); + anonymousCache.set(key, { page, headers }); if (environment.cache.serverSide.debug) { console.log(`CACHE SAVE FOR ${key} in anonymous cache.`); } } } diff --git a/src/config/cache-config.interface.ts b/src/config/cache-config.interface.ts index 9560fe46a5a..14af509bbf2 100644 --- a/src/config/cache-config.interface.ts +++ b/src/config/cache-config.interface.ts @@ -13,6 +13,8 @@ export interface CacheConfig extends Config { serverSide: { // Debug server-side caching. Set to true to see cache hits/misses/refreshes in console logs. debug: boolean, + // List of headers to restore from the cache hit + headers: string[], // Cache specific to known bots. Allows you to serve cached contents to bots only. botCache: { // Maximum number of pages (rendered via SSR) to cache. Setting max=0 disables the cache. diff --git a/src/config/default-app-config.ts b/src/config/default-app-config.ts index 80420407c79..a6e9e092e46 100644 --- a/src/config/default-app-config.ts +++ b/src/config/default-app-config.ts @@ -78,6 +78,8 @@ export class DefaultAppConfig implements AppConfig { // In-memory cache of server-side rendered content serverSide: { debug: false, + // Link header is used for signposting functionality + headers: ['Link'], // Cache specific to known bots. Allows you to serve cached contents to bots only. // Defaults to caching 1,000 pages. Each page expires after 1 day botCache: { From ac9be25faf9135424f4c201a24472c1d24b5f7f7 Mon Sep 17 00:00:00 2001 From: Giuseppe Digilio Date: Tue, 13 Jun 2023 19:30:40 +0200 Subject: [PATCH 2/3] [CST-5729] check if header exists --- server.ts | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/server.ts b/server.ts index 791baf7236c..f73bd1b7746 100644 --- a/server.ts +++ b/server.ts @@ -377,8 +377,12 @@ function cacheCheck(req, res, next) { if (cachedCopy && cachedCopy.page) { if (cachedCopy.headers && Array.isArray(environment.cache.serverSide.headers) && environment.cache.serverSide.headers.length > 0) { environment.cache.serverSide.headers.forEach((header) => { - if (environment.cache.serverSide.debug) { console.log(`Restore cached ${header} header`); } - res.setHeader(header, cachedCopy.headers[header.toLowerCase()]); + if (cachedCopy.headers[header.toLowerCase()]) { + if (environment.cache.serverSide.debug) { + console.log(`Restore cached ${header} header`); + } + res.setHeader(header, cachedCopy.headers[header.toLowerCase()]); + } }); } res.locals.ssr = true; // mark response as SSR-generated (enables text compression) From 2f06a7cb17a3ede463789cba27f47858bb8961c2 Mon Sep 17 00:00:00 2001 From: Giuseppe Digilio Date: Wed, 14 Jun 2023 18:26:55 +0200 Subject: [PATCH 3/3] [CST-5729] Change in order to save header only if configured and existing --- server.ts | 27 +++++++++++++++++++++------ src/config/cache-config.interface.ts | 2 +- src/environments/environment.test.ts | 1 + 3 files changed, 23 insertions(+), 7 deletions(-) diff --git a/server.ts b/server.ts index f73bd1b7746..3bbb28820aa 100644 --- a/server.ts +++ b/server.ts @@ -375,13 +375,13 @@ function cacheCheck(req, res, next) { // If cached copy exists, return it to the user. if (cachedCopy && cachedCopy.page) { - if (cachedCopy.headers && Array.isArray(environment.cache.serverSide.headers) && environment.cache.serverSide.headers.length > 0) { - environment.cache.serverSide.headers.forEach((header) => { - if (cachedCopy.headers[header.toLowerCase()]) { + if (cachedCopy.headers) { + Object.keys(cachedCopy.headers).forEach((header) => { + if (cachedCopy.headers[header]) { if (environment.cache.serverSide.debug) { 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) if (key.startsWith('/reload')) { return; } - // Retrieve response headers - const headers = req.res.getHeaders(); + // Retrieve response headers to save, if any + const headers = retrieveHeaders(req.res); // 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) 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 */ diff --git a/src/config/cache-config.interface.ts b/src/config/cache-config.interface.ts index 14af509bbf2..73520c95ea1 100644 --- a/src/config/cache-config.interface.ts +++ b/src/config/cache-config.interface.ts @@ -13,7 +13,7 @@ export interface CacheConfig extends Config { serverSide: { // Debug server-side caching. Set to true to see cache hits/misses/refreshes in console logs. debug: boolean, - // List of headers to restore from the cache hit + // List of response headers to save into the cache headers: string[], // Cache specific to known bots. Allows you to serve cached contents to bots only. botCache: { diff --git a/src/environments/environment.test.ts b/src/environments/environment.test.ts index 9fe58f868cf..cb9d2c71303 100644 --- a/src/environments/environment.test.ts +++ b/src/environments/environment.test.ts @@ -59,6 +59,7 @@ export const environment: BuildConfig = { // In-memory cache of server-side rendered pages. Disabled in test environment (max=0) serverSide: { debug: false, + headers: ['Link'], botCache: { max: 0, timeToLive: 24 * 60 * 60 * 1000, // 1 day