diff --git a/server.ts b/server.ts index 2fa7b9b947..cfab230ef5 100644 --- a/server.ts +++ b/server.ts @@ -273,10 +273,8 @@ function serverSideRender(req, res, sendToUser: boolean = true) { }, (err, data) => { if (hasNoValue(err) && hasValue(data)) { // Replace REST URL with UI URL - if (environment.ui.replaceRestUrl && REST_BASE_URL !== environment.rest.baseUrl) { - const t0 = Date.now(); - data = data.replace(new RegExp(REST_BASE_URL, 'g'), environment.rest.baseUrl); - console.log(`Replaced all SSR URLs in HTML in ${Date.now() - t0}ms`); // todo: remove this + if (environment.universal.replaceRestUrl && REST_BASE_URL !== environment.rest.baseUrl) { + data = data.replace(new RegExp(REST_BASE_URL, 'g'), environment.rest.baseUrl); } // save server side rendered page to cache (if any are enabled) diff --git a/src/config/default-app-config.ts b/src/config/default-app-config.ts index 1e19815e24..de4f3bd56e 100644 --- a/src/config/default-app-config.ts +++ b/src/config/default-app-config.ts @@ -48,9 +48,6 @@ export class DefaultAppConfig implements AppConfig { // Trust X-FORWARDED-* headers from proxies useProxies: true, - - transferState: true, - replaceRestUrl: false, }; // The REST API server settings diff --git a/src/config/ui-server-config.interface.ts b/src/config/ui-server-config.interface.ts index 2e58f9d6de..70e2fa3e26 100644 --- a/src/config/ui-server-config.interface.ts +++ b/src/config/ui-server-config.interface.ts @@ -13,7 +13,4 @@ export class UIServerConfig extends ServerConfig { // Trust X-FORWARDED-* headers from proxies useProxies: boolean; - - transferState: boolean; - replaceRestUrl: boolean; } diff --git a/src/config/universal-config.interface.ts b/src/config/universal-config.interface.ts index e3f7f399a9..c789d3f169 100644 --- a/src/config/universal-config.interface.ts +++ b/src/config/universal-config.interface.ts @@ -14,10 +14,29 @@ export interface UniversalConfig extends Config { */ inlineCriticalCss?: boolean; + /** + * Enable state transfer from the server-side application to the client-side application. + * Defaults to true. + * + * Note: When using an external application cache layer, it's recommended not to transfer the state to avoid caching it. + * Disabling it ensures that dynamic state information is not inadvertently cached, which can improve security and + * ensure that users always use the most up-to-date state. + */ + transferState: boolean; + + /** + * When a different REST base URL is used for the server-side application, the generated state contains references to + * REST resources with the internal URL configured, so it is not transferred to the client application, by default. + * Enabling this setting transfers the state to the client application and replaces internal URLs with the public + * URLs used by the client application. + */ + replaceRestUrl: boolean; + /** * Paths to enable SSR for. Defaults to the home page and paths in the sitemap. */ paths: Array; + /** * Whether to enable rendering of search component on SSR */ diff --git a/src/environments/environment.production.ts b/src/environments/environment.production.ts index d69389cfe3..48e8125e24 100644 --- a/src/environments/environment.production.ts +++ b/src/environments/environment.production.ts @@ -9,6 +9,8 @@ export const environment: Partial = { async: true, time: false, inlineCriticalCss: false, + transferState: true, + replaceRestUrl: false, paths: [ '/home', '/items/', '/entities/', '/collections/', '/communities/', '/bitstream/', '/bitstreams/', '/handle/', '/reload/' ], enableSearchComponent: false, enableBrowseComponent: false, diff --git a/src/environments/environment.test.ts b/src/environments/environment.test.ts index e01c67480c..30ca741b36 100644 --- a/src/environments/environment.test.ts +++ b/src/environments/environment.test.ts @@ -12,6 +12,8 @@ export const environment: BuildConfig = { async: true, time: false, inlineCriticalCss: false, + transferState: true, + replaceRestUrl: false, paths: [ '/home', '/items/', '/entities/', '/collections/', '/communities/', '/bitstream/', '/bitstreams/', '/handle/', '/reload/' ], enableSearchComponent: false, enableBrowseComponent: false, diff --git a/src/environments/environment.ts b/src/environments/environment.ts index e3258cb4e2..502bae140a 100644 --- a/src/environments/environment.ts +++ b/src/environments/environment.ts @@ -14,6 +14,8 @@ export const environment: Partial = { async: true, time: false, inlineCriticalCss: false, + transferState: true, + replaceRestUrl: false, paths: [ '/home', '/items/', '/entities/', '/collections/', '/communities/', '/bitstream/', '/bitstreams/', '/handle/', '/reload/' ], enableSearchComponent: false, enableBrowseComponent: false, diff --git a/src/modules/app/browser-init.service.ts b/src/modules/app/browser-init.service.ts index 30e03cc19d..4b1c6b40d2 100644 --- a/src/modules/app/browser-init.service.ts +++ b/src/modules/app/browser-init.service.ts @@ -35,6 +35,7 @@ import { RootDataService } from '../../app/core/data/root-data.service'; import { firstValueFrom, lastValueFrom, Subscription } from 'rxjs'; import { ServerCheckGuard } from '../../app/core/server-check/server-check.guard'; import { HALEndpointService } from '../../app/core/shared/hal-endpoint.service'; +import { BuildConfig } from '../../config/build-config.interface'; /** * Performs client-side initialization. @@ -48,7 +49,7 @@ export class BrowserInitService extends InitService { protected store: Store, protected correlationIdService: CorrelationIdService, protected transferState: TransferState, - @Inject(APP_CONFIG) protected appConfig: AppConfig, + @Inject(APP_CONFIG) protected appConfig: BuildConfig, protected translate: TranslateService, protected localeService: LocaleService, protected angulartics2DSpace: Angulartics2DSpace, @@ -90,9 +91,7 @@ export class BrowserInitService extends InitService { protected init(): () => Promise { return async () => { - if (this.appConfig.ui.transferState) { - await this.loadAppState(); - } + await this.loadAppState(); this.checkAuthenticationToken(); this.externalAuthCheck(); this.initCorrelationId(); @@ -124,7 +123,7 @@ export class BrowserInitService extends InitService { */ private async loadAppState(): Promise { // The app state can be transferred only when SSR and CSR are using the same base url for the REST API - if (this.appConfig.ui.transferState && (!this.appConfig.rest.hasSsrBaseUrl || this.appConfig.ui.replaceRestUrl)) { + if (this.appConfig.universal.transferState) { const state = this.transferState.get(InitService.NGRX_STATE, null); this.transferState.remove(InitService.NGRX_STATE); this.store.dispatch(new StoreAction(StoreActionTypes.REHYDRATE, state)); diff --git a/src/modules/app/server-init.service.ts b/src/modules/app/server-init.service.ts index bb32e917ee..e0c55211e3 100644 --- a/src/modules/app/server-init.service.ts +++ b/src/modules/app/server-init.service.ts @@ -21,7 +21,8 @@ import { BreadcrumbsService } from '../../app/breadcrumbs/breadcrumbs.service'; import { ThemeService } from '../../app/shared/theme-support/theme.service'; import { take } from 'rxjs/operators'; import { MenuService } from '../../app/shared/menu/menu.service'; -import { isNotEmpty } from '../../app/shared/empty.util'; +import { isEmpty, isNotEmpty } from '../../app/shared/empty.util'; +import { BuildConfig } from '../../config/build-config.interface'; /** * Performs server-side initialization. @@ -32,7 +33,7 @@ export class ServerInitService extends InitService { protected store: Store, protected correlationIdService: CorrelationIdService, protected transferState: TransferState, - @Inject(APP_CONFIG) protected appConfig: AppConfig, + @Inject(APP_CONFIG) protected appConfig: BuildConfig, protected translate: TranslateService, protected localeService: LocaleService, protected angulartics2DSpace: Angulartics2DSpace, @@ -59,9 +60,7 @@ export class ServerInitService extends InitService { return async () => { this.checkAuthenticationToken(); this.saveAppConfigForCSR(); - if (this.appConfig.ui.transferState) { - this.saveAppState(); - } + this.saveAppState(); this.initCorrelationId(); this.checkEnvironment(); @@ -83,14 +82,16 @@ export class ServerInitService extends InitService { * @private */ private saveAppState() { - this.transferState.onSerialize(InitService.NGRX_STATE, () => { - let state; - this.store.pipe(take(1)).subscribe((saveState: any) => { - state = saveState; - }); + if (this.appConfig.universal.transferState && (isEmpty(this.appConfig.rest.ssrBaseUrl) || this.appConfig.universal.replaceRestUrl)) { + this.transferState.onSerialize(InitService.NGRX_STATE, () => { + let state; + this.store.pipe(take(1)).subscribe((saveState: any) => { + state = saveState; + }); - return state; - }); + return state; + }); + } } private saveAppConfigForCSR(): void {