Skip to content

Commit 1a3fe01

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

9 files changed

Lines changed: 45 additions & 26 deletions

server.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -270,10 +270,8 @@ function serverSideRender(req, res, next, sendToUser: boolean = true) {
270270
.then((html) => {
271271
if (hasValue(html)) {
272272
// Replace REST URL with UI URL
273-
if (environment.ui.replaceRestUrl && REST_BASE_URL !== environment.rest.baseUrl) {
274-
const t0 = Date.now();
273+
if (environment.ssr.replaceRestUrl && REST_BASE_URL !== environment.rest.baseUrl) {
275274
html = html.replace(new RegExp(REST_BASE_URL, 'g'), environment.rest.baseUrl);
276-
console.log(`Replaced all SSR URLs in HTML in ${Date.now() - t0}ms`); // todo: remove this
277275
}
278276

279277
// 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
@@ -52,9 +52,6 @@ export class DefaultAppConfig implements AppConfig {
5252

5353
// Trust X-FORWARDED-* headers from proxies
5454
useProxies: true,
55-
56-
transferState: true,
57-
replaceRestUrl: false,
5855
};
5956

6057
// The REST API server settings

src/config/ssr-config.interface.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,24 @@ export interface SSRConfig extends Config {
2121
*/
2222
inlineCriticalCss: boolean;
2323

24+
/**
25+
* Enable state transfer from the server-side application to the client-side application.
26+
* Defaults to true.
27+
*
28+
* Note: When using an external application cache layer, it's recommended not to transfer the state to avoid caching it.
29+
* Disabling it ensures that dynamic state information is not inadvertently cached, which can improve security and
30+
* ensure that users always use the most up-to-date state.
31+
*/
32+
transferState: boolean;
33+
34+
/**
35+
* When a different REST base URL is used for the server-side application, the generated state contains references to
36+
* REST resources with the internal URL configured, so it is not transferred to the client application, by default.
37+
* Enabling this setting transfers the state to the client application and replaces internal URLs with the public
38+
* URLs used by the client application.
39+
*/
40+
replaceRestUrl: boolean;
41+
2442
/**
2543
* Paths to enable SSR for. Defaults to the home page and paths in the sitemap.
2644
*/

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/environments/environment.production.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ export const environment: Partial<BuildConfig> = {
88
enabled: true,
99
enablePerformanceProfiler: false,
1010
inlineCriticalCss: false,
11+
transferState: true,
12+
replaceRestUrl: false,
1113
paths: [ '/home', '/items/', '/entities/', '/collections/', '/communities/', '/bitstream/', '/bitstreams/', '/handle/', '/reload/' ],
1214
enableSearchComponent: false,
1315
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
enabled: true,
1313
enablePerformanceProfiler: 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
@@ -13,6 +13,8 @@ export const environment: Partial<BuildConfig> = {
1313
enabled: false,
1414
enablePerformanceProfiler: false,
1515
inlineCriticalCss: false,
16+
transferState: true,
17+
replaceRestUrl: false,
1618
paths: [ '/home', '/items/', '/entities/', '/collections/', '/communities/', '/bitstream/', '/bitstreams/', '/handle/', '/reload/' ],
1719
enableSearchComponent: false,
1820
enableBrowseComponent: false,

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

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ import {
5454
APP_CONFIG_STATE,
5555
AppConfig,
5656
} from '../../config/app-config.interface';
57+
import { BuildConfig } from '../../config/build-config.interface';
5758
import { extendEnvironmentWithAppConfig } from '../../config/config.util';
5859
import { DefaultAppConfig } from '../../config/default-app-config';
5960
import { environment } from '../../environments/environment';
@@ -70,7 +71,7 @@ export class BrowserInitService extends InitService {
7071
protected store: Store<AppState>,
7172
protected correlationIdService: CorrelationIdService,
7273
protected transferState: TransferState,
73-
@Inject(APP_CONFIG) protected appConfig: AppConfig,
74+
@Inject(APP_CONFIG) protected appConfig: BuildConfig,
7475
protected translate: TranslateService,
7576
protected localeService: LocaleService,
7677
protected angulartics2DSpace: Angulartics2DSpace,
@@ -113,9 +114,7 @@ export class BrowserInitService extends InitService {
113114

114115
protected init(): () => Promise<boolean> {
115116
return async () => {
116-
if (this.appConfig.ui.transferState) {
117-
await this.loadAppState();
118-
}
117+
await this.loadAppState();
119118
this.checkAuthenticationToken();
120119
this.externalAuthCheck();
121120
this.initCorrelationId();
@@ -147,7 +146,7 @@ export class BrowserInitService extends InitService {
147146
*/
148147
private async loadAppState(): Promise<boolean> {
149148
// The app state can be transferred only when SSR and CSR are using the same base url for the REST API
150-
if (this.appConfig.ui.transferState && (!this.appConfig.rest.hasSsrBaseUrl || this.appConfig.ui.replaceRestUrl)) {
149+
if (this.appConfig.ssr.transferState) {
151150
const state = this.transferState.get<any>(InitService.NGRX_STATE, null);
152151
this.transferState.remove(InitService.NGRX_STATE);
153152
this.store.dispatch(new StoreAction(StoreActionTypes.REHYDRATE, state));

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

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,10 @@ import { LocaleService } from '../../app/core/locale/locale.service';
2121
import { HeadTagService } from '../../app/core/metadata/head-tag.service';
2222
import { CorrelationIdService } from '../../app/correlation-id/correlation-id.service';
2323
import { InitService } from '../../app/init.service';
24-
import { isNotEmpty } from '../../app/shared/empty.util';
24+
import {
25+
isEmpty,
26+
isNotEmpty,
27+
} from '../../app/shared/empty.util';
2528
import { MenuService } from '../../app/shared/menu/menu.service';
2629
import { ThemeService } from '../../app/shared/theme-support/theme.service';
2730
import { Angulartics2DSpace } from '../../app/statistics/angulartics/dspace-provider';
@@ -30,6 +33,7 @@ import {
3033
APP_CONFIG_STATE,
3134
AppConfig,
3235
} from '../../config/app-config.interface';
36+
import { BuildConfig } from '../../config/build-config.interface';
3337
import { environment } from '../../environments/environment';
3438

3539
/**
@@ -41,7 +45,7 @@ export class ServerInitService extends InitService {
4145
protected store: Store<AppState>,
4246
protected correlationIdService: CorrelationIdService,
4347
protected transferState: TransferState,
44-
@Inject(APP_CONFIG) protected appConfig: AppConfig,
48+
@Inject(APP_CONFIG) protected appConfig: BuildConfig,
4549
protected translate: TranslateService,
4650
protected localeService: LocaleService,
4751
protected angulartics2DSpace: Angulartics2DSpace,
@@ -68,9 +72,7 @@ export class ServerInitService extends InitService {
6872
return async () => {
6973
this.checkAuthenticationToken();
7074
this.saveAppConfigForCSR();
71-
if (this.appConfig.ui.transferState) {
72-
this.saveAppState();
73-
}
75+
this.saveAppState();
7476
this.initCorrelationId();
7577

7678
this.checkEnvironment();
@@ -92,14 +94,16 @@ export class ServerInitService extends InitService {
9294
* @private
9395
*/
9496
private saveAppState() {
95-
this.transferState.onSerialize(InitService.NGRX_STATE, () => {
96-
let state;
97-
this.store.pipe(take(1)).subscribe((saveState: any) => {
98-
state = saveState;
99-
});
97+
if (this.appConfig.ssr.transferState && (isEmpty(this.appConfig.rest.ssrBaseUrl) || this.appConfig.ssr.replaceRestUrl)) {
98+
this.transferState.onSerialize(InitService.NGRX_STATE, () => {
99+
let state;
100+
this.store.pipe(take(1)).subscribe((saveState: any) => {
101+
state = saveState;
102+
});
100103

101-
return state;
102-
});
104+
return state;
105+
});
106+
}
103107
}
104108

105109
private saveAppConfigForCSR(): void {

0 commit comments

Comments
 (0)