mirror of
https://github.com/DSpace/dspace-angular.git
synced 2025-10-07 10:04:11 +00:00
[CST-5729] Add possibility to store response's headers into the SSR cache
This commit is contained in:
18
server.ts
18
server.ts
@@ -53,7 +53,7 @@ import { buildAppConfig } from './src/config/config.server';
|
|||||||
import { APP_CONFIG, AppConfig } from './src/config/app-config.interface';
|
import { APP_CONFIG, AppConfig } from './src/config/app-config.interface';
|
||||||
import { extendEnvironmentWithAppConfig } from './src/config/config.util';
|
import { extendEnvironmentWithAppConfig } from './src/config/config.util';
|
||||||
import { logStartupMessage } from './startup-message';
|
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 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.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
|
// 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
|
// 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)
|
// Avoid caching "/reload/[random]" paths (these are hard refreshes after logout)
|
||||||
if (key.startsWith('/reload')) { return; }
|
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
|
// 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)) {
|
||||||
botCache.set(key, page);
|
botCache.set(key, { page, headers });
|
||||||
if (environment.cache.serverSide.debug) { console.log(`CACHE SAVE FOR ${key} in bot cache.`); }
|
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 anonymous cache is enabled, save it to that cache if it doesn't exist or is expired
|
||||||
if (anonymousCacheEnabled() && !anonymousCache.has(key)) {
|
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.`); }
|
if (environment.cache.serverSide.debug) { console.log(`CACHE SAVE FOR ${key} in anonymous cache.`); }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -13,6 +13,8 @@ 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
|
||||||
|
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: {
|
||||||
// Maximum number of pages (rendered via SSR) to cache. Setting max=0 disables the cache.
|
// Maximum number of pages (rendered via SSR) to cache. Setting max=0 disables the cache.
|
||||||
|
@@ -78,6 +78,8 @@ export class DefaultAppConfig implements AppConfig {
|
|||||||
// In-memory cache of server-side rendered content
|
// In-memory cache of server-side rendered content
|
||||||
serverSide: {
|
serverSide: {
|
||||||
debug: false,
|
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.
|
// 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
|
// Defaults to caching 1,000 pages. Each page expires after 1 day
|
||||||
botCache: {
|
botCache: {
|
||||||
|
Reference in New Issue
Block a user