mirror of
https://github.com/DSpace/dspace-angular.git
synced 2025-10-07 01:54:15 +00:00
[DURACOM-288] Refactoring configuration to transfer state
This commit is contained in:
@@ -273,10 +273,8 @@ function serverSideRender(req, res, sendToUser: boolean = true) {
|
||||
}, (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
|
||||
if (environment.universal.replaceRestUrl && REST_BASE_URL !== environment.rest.baseUrl) {
|
||||
data = data.replace(new RegExp(REST_BASE_URL, 'g'), environment.rest.baseUrl);
|
||||
}
|
||||
|
||||
// save server side rendered page to cache (if any are enabled)
|
||||
|
@@ -48,9 +48,6 @@ export class DefaultAppConfig implements AppConfig {
|
||||
|
||||
// Trust X-FORWARDED-* headers from proxies
|
||||
useProxies: true,
|
||||
|
||||
transferState: true,
|
||||
replaceRestUrl: false,
|
||||
};
|
||||
|
||||
// The REST API server settings
|
||||
|
@@ -13,7 +13,4 @@ export class UIServerConfig extends ServerConfig {
|
||||
|
||||
// Trust X-FORWARDED-* headers from proxies
|
||||
useProxies: boolean;
|
||||
|
||||
transferState: boolean;
|
||||
replaceRestUrl: boolean;
|
||||
}
|
||||
|
@@ -14,10 +14,29 @@ export interface UniversalConfig extends Config {
|
||||
*/
|
||||
inlineCriticalCss?: boolean;
|
||||
|
||||
/**
|
||||
* Enable state transfer from the server-side application to the client-side application.
|
||||
* Defaults to true.
|
||||
*
|
||||
* Note: When using an external application cache layer, it's recommended not to transfer the state to avoid caching it.
|
||||
* Disabling it ensures that dynamic state information is not inadvertently cached, which can improve security and
|
||||
* ensure that users always use the most up-to-date state.
|
||||
*/
|
||||
transferState: boolean;
|
||||
|
||||
/**
|
||||
* When a different REST base URL is used for the server-side application, the generated state contains references to
|
||||
* REST resources with the internal URL configured, so it is not transferred to the client application, by default.
|
||||
* Enabling this setting transfers the state to the client application and replaces internal URLs with the public
|
||||
* URLs used by the client application.
|
||||
*/
|
||||
replaceRestUrl: boolean;
|
||||
|
||||
/**
|
||||
* Paths to enable SSR for. Defaults to the home page and paths in the sitemap.
|
||||
*/
|
||||
paths: Array<string>;
|
||||
|
||||
/**
|
||||
* Whether to enable rendering of search component on SSR
|
||||
*/
|
||||
|
@@ -9,6 +9,8 @@ export const environment: Partial<BuildConfig> = {
|
||||
async: true,
|
||||
time: false,
|
||||
inlineCriticalCss: false,
|
||||
transferState: true,
|
||||
replaceRestUrl: false,
|
||||
paths: [ '/home', '/items/', '/entities/', '/collections/', '/communities/', '/bitstream/', '/bitstreams/', '/handle/', '/reload/' ],
|
||||
enableSearchComponent: false,
|
||||
enableBrowseComponent: false,
|
||||
|
@@ -12,6 +12,8 @@ export const environment: BuildConfig = {
|
||||
async: true,
|
||||
time: false,
|
||||
inlineCriticalCss: false,
|
||||
transferState: true,
|
||||
replaceRestUrl: false,
|
||||
paths: [ '/home', '/items/', '/entities/', '/collections/', '/communities/', '/bitstream/', '/bitstreams/', '/handle/', '/reload/' ],
|
||||
enableSearchComponent: false,
|
||||
enableBrowseComponent: false,
|
||||
|
@@ -14,6 +14,8 @@ export const environment: Partial<BuildConfig> = {
|
||||
async: true,
|
||||
time: false,
|
||||
inlineCriticalCss: false,
|
||||
transferState: true,
|
||||
replaceRestUrl: false,
|
||||
paths: [ '/home', '/items/', '/entities/', '/collections/', '/communities/', '/bitstream/', '/bitstreams/', '/handle/', '/reload/' ],
|
||||
enableSearchComponent: false,
|
||||
enableBrowseComponent: false,
|
||||
|
@@ -35,6 +35,7 @@ import { RootDataService } from '../../app/core/data/root-data.service';
|
||||
import { firstValueFrom, lastValueFrom, Subscription } from 'rxjs';
|
||||
import { ServerCheckGuard } from '../../app/core/server-check/server-check.guard';
|
||||
import { HALEndpointService } from '../../app/core/shared/hal-endpoint.service';
|
||||
import { BuildConfig } from '../../config/build-config.interface';
|
||||
|
||||
/**
|
||||
* Performs client-side initialization.
|
||||
@@ -48,7 +49,7 @@ export class BrowserInitService extends InitService {
|
||||
protected store: Store<AppState>,
|
||||
protected correlationIdService: CorrelationIdService,
|
||||
protected transferState: TransferState,
|
||||
@Inject(APP_CONFIG) protected appConfig: AppConfig,
|
||||
@Inject(APP_CONFIG) protected appConfig: BuildConfig,
|
||||
protected translate: TranslateService,
|
||||
protected localeService: LocaleService,
|
||||
protected angulartics2DSpace: Angulartics2DSpace,
|
||||
@@ -90,9 +91,7 @@ export class BrowserInitService extends InitService {
|
||||
|
||||
protected init(): () => Promise<boolean> {
|
||||
return async () => {
|
||||
if (this.appConfig.ui.transferState) {
|
||||
await this.loadAppState();
|
||||
}
|
||||
await this.loadAppState();
|
||||
this.checkAuthenticationToken();
|
||||
this.externalAuthCheck();
|
||||
this.initCorrelationId();
|
||||
@@ -124,7 +123,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.ui.transferState && (!this.appConfig.rest.hasSsrBaseUrl || this.appConfig.ui.replaceRestUrl)) {
|
||||
if (this.appConfig.universal.transferState) {
|
||||
const state = this.transferState.get<any>(InitService.NGRX_STATE, null);
|
||||
this.transferState.remove(InitService.NGRX_STATE);
|
||||
this.store.dispatch(new StoreAction(StoreActionTypes.REHYDRATE, state));
|
||||
|
@@ -21,7 +21,8 @@ import { BreadcrumbsService } from '../../app/breadcrumbs/breadcrumbs.service';
|
||||
import { ThemeService } from '../../app/shared/theme-support/theme.service';
|
||||
import { take } from 'rxjs/operators';
|
||||
import { MenuService } from '../../app/shared/menu/menu.service';
|
||||
import { isNotEmpty } from '../../app/shared/empty.util';
|
||||
import { isEmpty, isNotEmpty } from '../../app/shared/empty.util';
|
||||
import { BuildConfig } from '../../config/build-config.interface';
|
||||
|
||||
/**
|
||||
* Performs server-side initialization.
|
||||
@@ -32,7 +33,7 @@ export class ServerInitService extends InitService {
|
||||
protected store: Store<AppState>,
|
||||
protected correlationIdService: CorrelationIdService,
|
||||
protected transferState: TransferState,
|
||||
@Inject(APP_CONFIG) protected appConfig: AppConfig,
|
||||
@Inject(APP_CONFIG) protected appConfig: BuildConfig,
|
||||
protected translate: TranslateService,
|
||||
protected localeService: LocaleService,
|
||||
protected angulartics2DSpace: Angulartics2DSpace,
|
||||
@@ -59,9 +60,7 @@ export class ServerInitService extends InitService {
|
||||
return async () => {
|
||||
this.checkAuthenticationToken();
|
||||
this.saveAppConfigForCSR();
|
||||
if (this.appConfig.ui.transferState) {
|
||||
this.saveAppState();
|
||||
}
|
||||
this.saveAppState();
|
||||
this.initCorrelationId();
|
||||
|
||||
this.checkEnvironment();
|
||||
@@ -83,14 +82,16 @@ export class ServerInitService extends InitService {
|
||||
* @private
|
||||
*/
|
||||
private saveAppState() {
|
||||
this.transferState.onSerialize(InitService.NGRX_STATE, () => {
|
||||
let state;
|
||||
this.store.pipe(take(1)).subscribe((saveState: any) => {
|
||||
state = saveState;
|
||||
});
|
||||
if (this.appConfig.universal.transferState && (isEmpty(this.appConfig.rest.ssrBaseUrl) || this.appConfig.universal.replaceRestUrl)) {
|
||||
this.transferState.onSerialize(InitService.NGRX_STATE, () => {
|
||||
let state;
|
||||
this.store.pipe(take(1)).subscribe((saveState: any) => {
|
||||
state = saveState;
|
||||
});
|
||||
|
||||
return state;
|
||||
});
|
||||
return state;
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
private saveAppConfigForCSR(): void {
|
||||
|
Reference in New Issue
Block a user