Add configuration to toggle replace/transfer

This commit is contained in:
Yury Bondarenko
2024-11-20 12:58:38 +01:00
committed by Giuseppe Digilio
parent 587e1546dc
commit a7276b8a30
5 changed files with 20 additions and 3 deletions

View File

@@ -269,6 +269,13 @@ function serverSideRender(req, res, next, sendToUser: boolean = true) {
})
.then((html) => {
if (hasValue(html)) {
// Replace REST URL with UI URL
if (environment.ui.replaceRestUrl && REST_BASE_URL !== environment.rest.baseUrl) {
const t0 = Date.now();
html = html.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)
saveToCache(req, html);
if (sendToUser) {

View File

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

View File

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

View File

@@ -113,7 +113,9 @@ export class BrowserInitService extends InitService {
protected init(): () => Promise<boolean> {
return async () => {
if (this.appConfig.ui.transferState) {
await this.loadAppState();
}
this.checkAuthenticationToken();
this.externalAuthCheck();
this.initCorrelationId();
@@ -145,7 +147,7 @@ export class BrowserInitService extends InitService {
*/
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
if (!this.appConfig.rest.hasSsrBaseUrl) {
if (this.appConfig.ui.transferState && (!this.appConfig.rest.hasSsrBaseUrl || this.appConfig.ui.replaceRestUrl)) {
const state = this.transferState.get<any>(InitService.NGRX_STATE, null);
this.transferState.remove(InitService.NGRX_STATE);
this.store.dispatch(new StoreAction(StoreActionTypes.REHYDRATE, state));

View File

@@ -68,7 +68,9 @@ export class ServerInitService extends InitService {
return async () => {
this.checkAuthenticationToken();
this.saveAppConfigForCSR();
if (this.appConfig.ui.transferState) {
this.saveAppState();
}
this.initCorrelationId();
this.checkEnvironment();