Skip to content

Commit

Permalink
fix: /localization call should wait until all server configurations a…
Browse files Browse the repository at this point in the history
…re applied (#1457)
  • Loading branch information
Eisie96 authored Jul 7, 2023
1 parent be31fef commit 31028be
Showing 1 changed file with 15 additions and 5 deletions.
20 changes: 15 additions & 5 deletions src/app/core/utils/translate/icm-translate-loader.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import { Inject, Injectable, InjectionToken } from '@angular/core';
import { TransferState, makeStateKey } from '@angular/platform-browser';
import { Actions, ofType } from '@ngrx/effects';
import { routerNavigationAction } from '@ngrx/router-store';
import { TranslateLoader } from '@ngx-translate/core';
import { memoize } from 'lodash-es';
import { combineLatest, defer, from, iif, of } from 'rxjs';
import { catchError, map, shareReplay, tap } from 'rxjs/operators';
import { catchError, first, map, shareReplay, switchMap, tap } from 'rxjs/operators';

import { LocalizationsService } from 'ish-core/services/localizations/localizations.service';
import { InjectSingle } from 'ish-core/utils/injection';
Expand All @@ -20,6 +22,7 @@ export const LOCAL_TRANSLATIONS = new InjectionToken<LocalTranslations>('transla
@Injectable()
export class ICMTranslateLoader implements TranslateLoader {
constructor(
private actions$: Actions,
private transferState: TransferState,
private localizations: LocalizationsService,
@Inject(LOCAL_TRANSLATIONS) private localTranslations: InjectSingle<typeof LOCAL_TRANSLATIONS>
Expand All @@ -33,10 +36,17 @@ export class ICMTranslateLoader implements TranslateLoader {
const server$ = iif(
() => !SSR && this.transferState.hasKey(SSR_TRANSLATIONS),
of(this.transferState.get(SSR_TRANSLATIONS, {})),
this.localizations.getServerTranslations(lang).pipe(
tap(data => {
this.transferState.set(SSR_TRANSLATIONS, data);
})
// the localization call should wait until all server supplied configuration parameter are applied to the state
this.actions$.pipe(
ofType(routerNavigationAction),
first(),
switchMap(() =>
this.localizations.getServerTranslations(lang).pipe(
tap(data => {
this.transferState.set(SSR_TRANSLATIONS, data);
})
)
)
)
);
return combineLatest([local$, server$]).pipe(
Expand Down

0 comments on commit 31028be

Please sign in to comment.