[DURACOM-288] Refactoring configuration to transfer state

This commit is contained in:
Giuseppe Digilio
2025-01-09 18:46:22 +01:00
parent f9d8e4e0b7
commit cb5deb644e
9 changed files with 44 additions and 27 deletions

View File

@@ -273,10 +273,8 @@ function serverSideRender(req, res, sendToUser: boolean = true) {
}, (err, data) => { }, (err, data) => {
if (hasNoValue(err) && hasValue(data)) { if (hasNoValue(err) && hasValue(data)) {
// Replace REST URL with UI URL // Replace REST URL with UI URL
if (environment.ui.replaceRestUrl && REST_BASE_URL !== environment.rest.baseUrl) { if (environment.universal.replaceRestUrl && REST_BASE_URL !== environment.rest.baseUrl) {
const t0 = Date.now(); data = data.replace(new RegExp(REST_BASE_URL, 'g'), environment.rest.baseUrl);
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
} }
// save server side rendered page to cache (if any are enabled) // save server side rendered page to cache (if any are enabled)

View File

@@ -48,9 +48,6 @@ export class DefaultAppConfig implements AppConfig {
// Trust X-FORWARDED-* headers from proxies // Trust X-FORWARDED-* headers from proxies
useProxies: true, useProxies: true,
transferState: true,
replaceRestUrl: false,
}; };
// The REST API server settings // The REST API server settings

View File

@@ -13,7 +13,4 @@ export class UIServerConfig extends ServerConfig {
// Trust X-FORWARDED-* headers from proxies // Trust X-FORWARDED-* headers from proxies
useProxies: boolean; useProxies: boolean;
transferState: boolean;
replaceRestUrl: boolean;
} }

View File

@@ -14,10 +14,29 @@ export interface UniversalConfig extends Config {
*/ */
inlineCriticalCss?: boolean; 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 to enable SSR for. Defaults to the home page and paths in the sitemap.
*/ */
paths: Array<string>; paths: Array<string>;
/** /**
* Whether to enable rendering of search component on SSR * Whether to enable rendering of search component on SSR
*/ */

View File

@@ -9,6 +9,8 @@ export const environment: Partial<BuildConfig> = {
async: true, async: true,
time: false, time: false,
inlineCriticalCss: false, inlineCriticalCss: false,
transferState: true,
replaceRestUrl: false,
paths: [ '/home', '/items/', '/entities/', '/collections/', '/communities/', '/bitstream/', '/bitstreams/', '/handle/', '/reload/' ], paths: [ '/home', '/items/', '/entities/', '/collections/', '/communities/', '/bitstream/', '/bitstreams/', '/handle/', '/reload/' ],
enableSearchComponent: false, enableSearchComponent: false,
enableBrowseComponent: false, enableBrowseComponent: false,

View File

@@ -12,6 +12,8 @@ export const environment: BuildConfig = {
async: true, async: true,
time: false, time: false,
inlineCriticalCss: false, inlineCriticalCss: false,
transferState: true,
replaceRestUrl: false,
paths: [ '/home', '/items/', '/entities/', '/collections/', '/communities/', '/bitstream/', '/bitstreams/', '/handle/', '/reload/' ], paths: [ '/home', '/items/', '/entities/', '/collections/', '/communities/', '/bitstream/', '/bitstreams/', '/handle/', '/reload/' ],
enableSearchComponent: false, enableSearchComponent: false,
enableBrowseComponent: false, enableBrowseComponent: false,

View File

@@ -14,6 +14,8 @@ export const environment: Partial<BuildConfig> = {
async: true, async: true,
time: false, time: false,
inlineCriticalCss: false, inlineCriticalCss: false,
transferState: true,
replaceRestUrl: false,
paths: [ '/home', '/items/', '/entities/', '/collections/', '/communities/', '/bitstream/', '/bitstreams/', '/handle/', '/reload/' ], paths: [ '/home', '/items/', '/entities/', '/collections/', '/communities/', '/bitstream/', '/bitstreams/', '/handle/', '/reload/' ],
enableSearchComponent: false, enableSearchComponent: false,
enableBrowseComponent: false, enableBrowseComponent: false,

View File

@@ -35,6 +35,7 @@ import { RootDataService } from '../../app/core/data/root-data.service';
import { firstValueFrom, lastValueFrom, Subscription } from 'rxjs'; import { firstValueFrom, lastValueFrom, Subscription } from 'rxjs';
import { ServerCheckGuard } from '../../app/core/server-check/server-check.guard'; import { ServerCheckGuard } from '../../app/core/server-check/server-check.guard';
import { HALEndpointService } from '../../app/core/shared/hal-endpoint.service'; import { HALEndpointService } from '../../app/core/shared/hal-endpoint.service';
import { BuildConfig } from '../../config/build-config.interface';
/** /**
* Performs client-side initialization. * Performs client-side initialization.
@@ -48,7 +49,7 @@ export class BrowserInitService extends InitService {
protected store: Store<AppState>, protected store: Store<AppState>,
protected correlationIdService: CorrelationIdService, protected correlationIdService: CorrelationIdService,
protected transferState: TransferState, protected transferState: TransferState,
@Inject(APP_CONFIG) protected appConfig: AppConfig, @Inject(APP_CONFIG) protected appConfig: BuildConfig,
protected translate: TranslateService, protected translate: TranslateService,
protected localeService: LocaleService, protected localeService: LocaleService,
protected angulartics2DSpace: Angulartics2DSpace, protected angulartics2DSpace: Angulartics2DSpace,
@@ -90,9 +91,7 @@ export class BrowserInitService extends InitService {
protected init(): () => Promise<boolean> { protected init(): () => Promise<boolean> {
return async () => { return async () => {
if (this.appConfig.ui.transferState) { await this.loadAppState();
await this.loadAppState();
}
this.checkAuthenticationToken(); this.checkAuthenticationToken();
this.externalAuthCheck(); this.externalAuthCheck();
this.initCorrelationId(); this.initCorrelationId();
@@ -124,7 +123,7 @@ export class BrowserInitService extends InitService {
*/ */
private async loadAppState(): Promise<boolean> { private async loadAppState(): Promise<boolean> {
// The app state can be transferred only when SSR and CSR are using the same base url for the REST API // 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<any>(InitService.NGRX_STATE, null); const state = this.transferState.get<any>(InitService.NGRX_STATE, null);
this.transferState.remove(InitService.NGRX_STATE); this.transferState.remove(InitService.NGRX_STATE);
this.store.dispatch(new StoreAction(StoreActionTypes.REHYDRATE, state)); this.store.dispatch(new StoreAction(StoreActionTypes.REHYDRATE, state));

View File

@@ -21,7 +21,8 @@ import { BreadcrumbsService } from '../../app/breadcrumbs/breadcrumbs.service';
import { ThemeService } from '../../app/shared/theme-support/theme.service'; import { ThemeService } from '../../app/shared/theme-support/theme.service';
import { take } from 'rxjs/operators'; import { take } from 'rxjs/operators';
import { MenuService } from '../../app/shared/menu/menu.service'; 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. * Performs server-side initialization.
@@ -32,7 +33,7 @@ export class ServerInitService extends InitService {
protected store: Store<AppState>, protected store: Store<AppState>,
protected correlationIdService: CorrelationIdService, protected correlationIdService: CorrelationIdService,
protected transferState: TransferState, protected transferState: TransferState,
@Inject(APP_CONFIG) protected appConfig: AppConfig, @Inject(APP_CONFIG) protected appConfig: BuildConfig,
protected translate: TranslateService, protected translate: TranslateService,
protected localeService: LocaleService, protected localeService: LocaleService,
protected angulartics2DSpace: Angulartics2DSpace, protected angulartics2DSpace: Angulartics2DSpace,
@@ -59,9 +60,7 @@ export class ServerInitService extends InitService {
return async () => { return async () => {
this.checkAuthenticationToken(); this.checkAuthenticationToken();
this.saveAppConfigForCSR(); this.saveAppConfigForCSR();
if (this.appConfig.ui.transferState) { this.saveAppState();
this.saveAppState();
}
this.initCorrelationId(); this.initCorrelationId();
this.checkEnvironment(); this.checkEnvironment();
@@ -83,14 +82,16 @@ export class ServerInitService extends InitService {
* @private * @private
*/ */
private saveAppState() { private saveAppState() {
this.transferState.onSerialize(InitService.NGRX_STATE, () => { if (this.appConfig.universal.transferState && (isEmpty(this.appConfig.rest.ssrBaseUrl) || this.appConfig.universal.replaceRestUrl)) {
let state; this.transferState.onSerialize(InitService.NGRX_STATE, () => {
this.store.pipe(take(1)).subscribe((saveState: any) => { let state;
state = saveState; this.store.pipe(take(1)).subscribe((saveState: any) => {
}); state = saveState;
});
return state; return state;
}); });
}
} }
private saveAppConfigForCSR(): void { private saveAppConfigForCSR(): void {