@@ -53,7 +53,7 @@ import { buildAppConfig } from './src/config/config.server';
5353import { APP_CONFIG , AppConfig } from './src/config/app-config.interface' ;
5454import { extendEnvironmentWithAppConfig } from './src/config/config.util' ;
5555import { 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 */
0 commit comments