From a7276b8a3092a8b10bcf99cbc15af3eba7dca3c0 Mon Sep 17 00:00:00 2001 From: Yury Bondarenko Date: Wed, 20 Nov 2024 12:58:38 +0100 Subject: [PATCH] Add configuration to toggle replace/transfer --- server.ts | 7 +++++++ src/config/default-app-config.ts | 3 +++ src/config/ui-server-config.interface.ts | 3 +++ src/modules/app/browser-init.service.ts | 6 ++++-- src/modules/app/server-init.service.ts | 4 +++- 5 files changed, 20 insertions(+), 3 deletions(-) diff --git a/server.ts b/server.ts index 127a179e7a..e959600d28 100644 --- a/server.ts +++ b/server.ts @@ -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) { diff --git a/src/config/default-app-config.ts b/src/config/default-app-config.ts index 3c5e0ef0da..e81f77bff5 100644 --- a/src/config/default-app-config.ts +++ b/src/config/default-app-config.ts @@ -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 diff --git a/src/config/ui-server-config.interface.ts b/src/config/ui-server-config.interface.ts index 70e2fa3e26..2e58f9d6de 100644 --- a/src/config/ui-server-config.interface.ts +++ b/src/config/ui-server-config.interface.ts @@ -13,4 +13,7 @@ export class UIServerConfig extends ServerConfig { // Trust X-FORWARDED-* headers from proxies useProxies: boolean; + + transferState: boolean; + replaceRestUrl: boolean; } diff --git a/src/modules/app/browser-init.service.ts b/src/modules/app/browser-init.service.ts index b68b4b426a..9ba103a655 100644 --- a/src/modules/app/browser-init.service.ts +++ b/src/modules/app/browser-init.service.ts @@ -113,7 +113,9 @@ export class BrowserInitService extends InitService { protected init(): () => Promise { return async () => { - await this.loadAppState(); + 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 { // 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(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 851c2c24c0..8e96e3e61d 100644 --- a/src/modules/app/server-init.service.ts +++ b/src/modules/app/server-init.service.ts @@ -68,7 +68,9 @@ export class ServerInitService extends InitService { return async () => { this.checkAuthenticationToken(); this.saveAppConfigForCSR(); - this.saveAppState(); + if (this.appConfig.ui.transferState) { + this.saveAppState(); + } this.initCorrelationId(); this.checkEnvironment();