Skip to content

Commit 486aefe

Browse files
committed
[CST-5729] Add possibility to store response's headers into the SSR cache
1 parent 48ea7df commit 486aefe

3 files changed

Lines changed: 17 additions & 5 deletions

File tree

server.ts

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ import { buildAppConfig } from './src/config/config.server';
5353
import { APP_CONFIG, AppConfig } from './src/config/app-config.interface';
5454
import { extendEnvironmentWithAppConfig } from './src/config/config.util';
5555
import { logStartupMessage } from './startup-message';
56-
import { TOKENITEM } from 'src/app/core/auth/models/auth-token-info.model';
56+
import { TOKENITEM } from './src/app/core/auth/models/auth-token-info.model';
5757

5858

5959
/*
@@ -374,9 +374,15 @@ function cacheCheck(req, res, next) {
374374
}
375375

376376
// If cached copy exists, return it to the user.
377-
if (cachedCopy) {
377+
if (cachedCopy && cachedCopy.page) {
378+
if (cachedCopy.headers && Array.isArray(environment.cache.serverSide.headers) && environment.cache.serverSide.headers.length > 0) {
379+
environment.cache.serverSide.headers.forEach((header) => {
380+
if (environment.cache.serverSide.debug) { console.log(`Restore cached ${header} header`); }
381+
res.setHeader(header, cachedCopy.headers[header.toLowerCase()]);
382+
});
383+
}
378384
res.locals.ssr = true; // mark response as SSR-generated (enables text compression)
379-
res.send(cachedCopy);
385+
res.send(cachedCopy.page);
380386

381387
// Tell Express to skip all other handlers for this path
382388
// 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) {
452458
// Avoid caching "/reload/[random]" paths (these are hard refreshes after logout)
453459
if (key.startsWith('/reload')) { return; }
454460

461+
// Retrieve response headers
462+
const headers = req.res.getHeaders();
455463
// If bot cache is enabled, save it to that cache if it doesn't exist or is expired
456464
// (NOTE: has() will return false if page is expired in cache)
457465
if (botCacheEnabled() && !botCache.has(key)) {
458-
botCache.set(key, page);
466+
botCache.set(key, { page, headers });
459467
if (environment.cache.serverSide.debug) { console.log(`CACHE SAVE FOR ${key} in bot cache.`); }
460468
}
461469

462470
// If anonymous cache is enabled, save it to that cache if it doesn't exist or is expired
463471
if (anonymousCacheEnabled() && !anonymousCache.has(key)) {
464-
anonymousCache.set(key, page);
472+
anonymousCache.set(key, { page, headers });
465473
if (environment.cache.serverSide.debug) { console.log(`CACHE SAVE FOR ${key} in anonymous cache.`); }
466474
}
467475
}

src/config/cache-config.interface.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ export interface CacheConfig extends Config {
1313
serverSide: {
1414
// Debug server-side caching. Set to true to see cache hits/misses/refreshes in console logs.
1515
debug: boolean,
16+
// List of headers to restore from the cache hit
17+
headers: string[],
1618
// Cache specific to known bots. Allows you to serve cached contents to bots only.
1719
botCache: {
1820
// Maximum number of pages (rendered via SSR) to cache. Setting max=0 disables the cache.

src/config/default-app-config.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,8 @@ export class DefaultAppConfig implements AppConfig {
7878
// In-memory cache of server-side rendered content
7979
serverSide: {
8080
debug: false,
81+
// Link header is used for signposting functionality
82+
headers: ['Link'],
8183
// Cache specific to known bots. Allows you to serve cached contents to bots only.
8284
// Defaults to caching 1,000 pages. Each page expires after 1 day
8385
botCache: {

0 commit comments

Comments
 (0)