diff --git a/server.ts b/server.ts index 99fb5d5775..2fa7b9b947 100644 --- a/server.ts +++ b/server.ts @@ -272,6 +272,13 @@ function serverSideRender(req, res, sendToUser: boolean = true) { requestUrl: req.originalUrl, }, (err, data) => { if (hasNoValue(err) && hasValue(data)) { + // Replace REST URL with UI URL + if (environment.ui.replaceRestUrl && REST_BASE_URL !== environment.rest.baseUrl) { + const t0 = Date.now(); + 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) saveToCache(req, data); if (sendToUser) { diff --git a/src/config/default-app-config.ts b/src/config/default-app-config.ts index de4f3bd56e..1e19815e24 100644 --- a/src/config/default-app-config.ts +++ b/src/config/default-app-config.ts @@ -48,6 +48,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 c86a3d4323..30e03cc19d 100644 --- a/src/modules/app/browser-init.service.ts +++ b/src/modules/app/browser-init.service.ts @@ -90,7 +90,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(); @@ -122,7 +124,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 b162106553..bb32e917ee 100644 --- a/src/modules/app/server-init.service.ts +++ b/src/modules/app/server-init.service.ts @@ -59,7 +59,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();