Skip to content

Commit 7ebdcd3

Browse files
authored
Merge pull request DSpace#2304 from 4Science/cache-response-headers
Add possibility to store response's headers into the SSR cache
2 parents 0c9baf2 + 2f06a7c commit 7ebdcd3

4 files changed

Lines changed: 37 additions & 5 deletions

File tree

server.ts

Lines changed: 32 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,19 @@ 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) {
379+
Object.keys(cachedCopy.headers).forEach((header) => {
380+
if (cachedCopy.headers[header]) {
381+
if (environment.cache.serverSide.debug) {
382+
console.log(`Restore cached ${header} header`);
383+
}
384+
res.setHeader(header, cachedCopy.headers[header]);
385+
}
386+
});
387+
}
378388
res.locals.ssr = true; // mark response as SSR-generated (enables text compression)
379-
res.send(cachedCopy);
389+
res.send(cachedCopy.page);
380390

381391
// Tell Express to skip all other handlers for this path
382392
// This ensures we don't try to re-render the page since we've already returned the cached copy
@@ -452,21 +462,38 @@ function saveToCache(req, page: any) {
452462
// Avoid caching "/reload/[random]" paths (these are hard refreshes after logout)
453463
if (key.startsWith('/reload')) { return; }
454464

465+
// Retrieve response headers to save, if any
466+
const headers = retrieveHeaders(req.res);
455467
// If bot cache is enabled, save it to that cache if it doesn't exist or is expired
456468
// (NOTE: has() will return false if page is expired in cache)
457469
if (botCacheEnabled() && !botCache.has(key)) {
458-
botCache.set(key, page);
470+
botCache.set(key, { page, headers });
459471
if (environment.cache.serverSide.debug) { console.log(`CACHE SAVE FOR ${key} in bot cache.`); }
460472
}
461473

462474
// If anonymous cache is enabled, save it to that cache if it doesn't exist or is expired
463475
if (anonymousCacheEnabled() && !anonymousCache.has(key)) {
464-
anonymousCache.set(key, page);
476+
anonymousCache.set(key, { page, headers });
465477
if (environment.cache.serverSide.debug) { console.log(`CACHE SAVE FOR ${key} in anonymous cache.`); }
466478
}
467479
}
468480
}
469481

482+
function retrieveHeaders(response) {
483+
const headers = Object.create({});
484+
if (Array.isArray(environment.cache.serverSide.headers) && environment.cache.serverSide.headers.length > 0) {
485+
environment.cache.serverSide.headers.forEach((header) => {
486+
if (response.hasHeader(header)) {
487+
if (environment.cache.serverSide.debug) {
488+
console.log(`Save ${header} header to cache`);
489+
}
490+
headers[header] = response.getHeader(header);
491+
}
492+
});
493+
}
494+
495+
return headers;
496+
}
470497
/**
471498
* Whether a user is authenticated or not
472499
*/

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 response headers to save into the cache
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: {

src/environments/environment.test.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ export const environment: BuildConfig = {
5959
// In-memory cache of server-side rendered pages. Disabled in test environment (max=0)
6060
serverSide: {
6161
debug: false,
62+
headers: ['Link'],
6263
botCache: {
6364
max: 0,
6465
timeToLive: 24 * 60 * 60 * 1000, // 1 day

0 commit comments

Comments
 (0)