Skip to content

Commit cb5deb6

Browse files
committed
[DURACOM-288] Refactoring configuration to transfer state
1 parent f9d8e4e commit cb5deb6

9 files changed

Lines changed: 44 additions & 27 deletions

server.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -273,10 +273,8 @@ function serverSideRender(req, res, sendToUser: boolean = true) {
273273
}, (err, data) => {
274274
if (hasNoValue(err) && hasValue(data)) {
275275
// Replace REST URL with UI URL
276-
if (environment.ui.replaceRestUrl && REST_BASE_URL !== environment.rest.baseUrl) {
277-
const t0 = Date.now();
278-
data = data.replace(new RegExp(REST_BASE_URL, 'g'), environment.rest.baseUrl);
279-
console.log(`Replaced all SSR URLs in HTML in ${Date.now() - t0}ms`); // todo: remove this
276+
if (environment.universal.replaceRestUrl && REST_BASE_URL !== environment.rest.baseUrl) {
277+
data = data.replace(new RegExp(REST_BASE_URL, 'g'), environment.rest.baseUrl);
280278
}
281279

282280
// save server side rendered page to cache (if any are enabled)

src/config/default-app-config.ts

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,6 @@ export class DefaultAppConfig implements AppConfig {
4848

4949
// Trust X-FORWARDED-* headers from proxies
5050
useProxies: true,
51-
52-
transferState: true,
53-
replaceRestUrl: false,
5451
};
5552

5653
// The REST API server settings

src/config/ui-server-config.interface.ts

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,4 @@ export class UIServerConfig extends ServerConfig {
1313

1414
// Trust X-FORWARDED-* headers from proxies
1515
useProxies: boolean;
16-
17-
transferState: boolean;
18-
replaceRestUrl: boolean;
1916
}

src/config/universal-config.interface.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,29 @@ export interface UniversalConfig extends Config {
1414
*/
1515
inlineCriticalCss?: boolean;
1616

17+
/**
18+
* Enable state transfer from the server-side application to the client-side application.
19+
* Defaults to true.
20+
*
21+
* Note: When using an external application cache layer, it's recommended not to transfer the state to avoid caching it.
22+
* Disabling it ensures that dynamic state information is not inadvertently cached, which can improve security and
23+
* ensure that users always use the most up-to-date state.
24+
*/
25+
transferState: boolean;
26+
27+
/**
28+
* When a different REST base URL is used for the server-side application, the generated state contains references to
29+
* REST resources with the internal URL configured, so it is not transferred to the client application, by default.
30+
* Enabling this setting transfers the state to the client application and replaces internal URLs with the public
31+
* URLs used by the client application.
32+
*/
33+
replaceRestUrl: boolean;
34+
1735
/**
1836
* Paths to enable SSR for. Defaults to the home page and paths in the sitemap.
1937
*/
2038
paths: Array<string>;
39+
2140
/**
2241
* Whether to enable rendering of search component on SSR
2342
*/

src/environments/environment.production.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ export const environment: Partial<BuildConfig> = {
99
async: true,
1010
time: false,
1111
inlineCriticalCss: false,
12+
transferState: true,
13+
replaceRestUrl: false,
1214
paths: [ '/home', '/items/', '/entities/', '/collections/', '/communities/', '/bitstream/', '/bitstreams/', '/handle/', '/reload/' ],
1315
enableSearchComponent: false,
1416
enableBrowseComponent: false,

src/environments/environment.test.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ export const environment: BuildConfig = {
1212
async: true,
1313
time: false,
1414
inlineCriticalCss: false,
15+
transferState: true,
16+
replaceRestUrl: false,
1517
paths: [ '/home', '/items/', '/entities/', '/collections/', '/communities/', '/bitstream/', '/bitstreams/', '/handle/', '/reload/' ],
1618
enableSearchComponent: false,
1719
enableBrowseComponent: false,

src/environments/environment.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ export const environment: Partial<BuildConfig> = {
1414
async: true,
1515
time: false,
1616
inlineCriticalCss: false,
17+
transferState: true,
18+
replaceRestUrl: false,
1719
paths: [ '/home', '/items/', '/entities/', '/collections/', '/communities/', '/bitstream/', '/bitstreams/', '/handle/', '/reload/' ],
1820
enableSearchComponent: false,
1921
enableBrowseComponent: false,

src/modules/app/browser-init.service.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ import { RootDataService } from '../../app/core/data/root-data.service';
3535
import { firstValueFrom, lastValueFrom, Subscription } from 'rxjs';
3636
import { ServerCheckGuard } from '../../app/core/server-check/server-check.guard';
3737
import { HALEndpointService } from '../../app/core/shared/hal-endpoint.service';
38+
import { BuildConfig } from '../../config/build-config.interface';
3839

3940
/**
4041
* Performs client-side initialization.
@@ -48,7 +49,7 @@ export class BrowserInitService extends InitService {
4849
protected store: Store<AppState>,
4950
protected correlationIdService: CorrelationIdService,
5051
protected transferState: TransferState,
51-
@Inject(APP_CONFIG) protected appConfig: AppConfig,
52+
@Inject(APP_CONFIG) protected appConfig: BuildConfig,
5253
protected translate: TranslateService,
5354
protected localeService: LocaleService,
5455
protected angulartics2DSpace: Angulartics2DSpace,
@@ -90,9 +91,7 @@ export class BrowserInitService extends InitService {
9091

9192
protected init(): () => Promise<boolean> {
9293
return async () => {
93-
if (this.appConfig.ui.transferState) {
94-
await this.loadAppState();
95-
}
94+
await this.loadAppState();
9695
this.checkAuthenticationToken();
9796
this.externalAuthCheck();
9897
this.initCorrelationId();
@@ -124,7 +123,7 @@ export class BrowserInitService extends InitService {
124123
*/
125124
private async loadAppState(): Promise<boolean> {
126125
// The app state can be transferred only when SSR and CSR are using the same base url for the REST API
127-
if (this.appConfig.ui.transferState && (!this.appConfig.rest.hasSsrBaseUrl || this.appConfig.ui.replaceRestUrl)) {
126+
if (this.appConfig.universal.transferState) {
128127
const state = this.transferState.get<any>(InitService.NGRX_STATE, null);
129128
this.transferState.remove(InitService.NGRX_STATE);
130129
this.store.dispatch(new StoreAction(StoreActionTypes.REHYDRATE, state));

src/modules/app/server-init.service.ts

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@ import { BreadcrumbsService } from '../../app/breadcrumbs/breadcrumbs.service';
2121
import { ThemeService } from '../../app/shared/theme-support/theme.service';
2222
import { take } from 'rxjs/operators';
2323
import { MenuService } from '../../app/shared/menu/menu.service';
24-
import { isNotEmpty } from '../../app/shared/empty.util';
24+
import { isEmpty, isNotEmpty } from '../../app/shared/empty.util';
25+
import { BuildConfig } from '../../config/build-config.interface';
2526

2627
/**
2728
* Performs server-side initialization.
@@ -32,7 +33,7 @@ export class ServerInitService extends InitService {
3233
protected store: Store<AppState>,
3334
protected correlationIdService: CorrelationIdService,
3435
protected transferState: TransferState,
35-
@Inject(APP_CONFIG) protected appConfig: AppConfig,
36+
@Inject(APP_CONFIG) protected appConfig: BuildConfig,
3637
protected translate: TranslateService,
3738
protected localeService: LocaleService,
3839
protected angulartics2DSpace: Angulartics2DSpace,
@@ -59,9 +60,7 @@ export class ServerInitService extends InitService {
5960
return async () => {
6061
this.checkAuthenticationToken();
6162
this.saveAppConfigForCSR();
62-
if (this.appConfig.ui.transferState) {
63-
this.saveAppState();
64-
}
63+
this.saveAppState();
6564
this.initCorrelationId();
6665

6766
this.checkEnvironment();
@@ -83,14 +82,16 @@ export class ServerInitService extends InitService {
8382
* @private
8483
*/
8584
private saveAppState() {
86-
this.transferState.onSerialize(InitService.NGRX_STATE, () => {
87-
let state;
88-
this.store.pipe(take(1)).subscribe((saveState: any) => {
89-
state = saveState;
90-
});
85+
if (this.appConfig.universal.transferState && (isEmpty(this.appConfig.rest.ssrBaseUrl) || this.appConfig.universal.replaceRestUrl)) {
86+
this.transferState.onSerialize(InitService.NGRX_STATE, () => {
87+
let state;
88+
this.store.pipe(take(1)).subscribe((saveState: any) => {
89+
state = saveState;
90+
});
9191

92-
return state;
93-
});
92+
return state;
93+
});
94+
}
9495
}
9596

9697
private saveAppConfigForCSR(): void {

0 commit comments

Comments
 (0)