Skip to content

Commit

Permalink
fix: repair setting of locales meta tags (#451)
Browse files Browse the repository at this point in the history
  • Loading branch information
dhhyi authored Nov 3, 2020
1 parent dc36c87 commit dc3a206
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,9 @@ export const getAvailableLocales = createSelector(getConfigurationState, state =
/**
* selects the current locale if set. If not returns the first available locale
*/
export const getCurrentLocale = createSelector(getConfigurationState, state =>
state.lang ? state.locales.find(l => l.lang === state.lang) : state.locales[0]
export const getCurrentLocale = createSelector(
getConfigurationState,
state => state.locales.find(l => l.lang === state.lang) || state.locales[0]
);

export const getDeviceType = createSelector(getConfigurationState, state => state._deviceType);
Expand Down
47 changes: 15 additions & 32 deletions src/app/extensions/seo/store/seo/seo.effects.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { APP_BASE_HREF, DOCUMENT } from '@angular/common';
import { ApplicationRef, Inject, Injectable, Optional } from '@angular/core';
import { APP_BASE_HREF, DOCUMENT, isPlatformServer } from '@angular/common';
import { ApplicationRef, Inject, Injectable, Optional, PLATFORM_ID } from '@angular/core';
import { Actions, createEffect, ofType } from '@ngrx/effects';
import { routerNavigatedAction, routerNavigationAction } from '@ngrx/router-store';
import { Store, select } from '@ngrx/store';
Expand All @@ -8,8 +8,8 @@ import { MetaService } from '@ngx-meta/core';
import { TranslateService } from '@ngx-translate/core';
import { Request } from 'express';
import { isEqual } from 'lodash-es';
import { Observable, merge, race } from 'rxjs';
import { distinctUntilChanged, filter, first, map, mapTo, switchMap, switchMapTo, tap } from 'rxjs/operators';
import { merge, race } from 'rxjs';
import { distinctUntilChanged, filter, map, mapTo, switchMap, takeWhile, tap, withLatestFrom } from 'rxjs/operators';

import { CategoryHelper } from 'ish-core/models/category/category.model';
import { ProductView } from 'ish-core/models/product-view/product-view.model';
Expand All @@ -34,7 +34,8 @@ export class SeoEffects {
@Inject(DOCUMENT) private doc: Document,
@Optional() @Inject(REQUEST) private request: Request,
@Inject(APP_BASE_HREF) private baseHref: string,
private appRef: ApplicationRef
private appRef: ApplicationRef,
@Inject(PLATFORM_ID) private platformId: string
) {}

private productPage$ = this.store.pipe(
Expand Down Expand Up @@ -130,30 +131,16 @@ export class SeoEffects {
{ dispatch: false }
);

seoLanguage$ = createEffect(
seoLanguages$ = createEffect(
() =>
this.waitAppStable(
this.store.pipe(
select(getCurrentLocale),
whenTruthy(),
tap(current => {
this.meta.setTag('og:locale', current.lang);
})
)
),
{ dispatch: false }
);

seoAlternateLanguages$ = createEffect(
() =>
this.waitAppStable(
this.store.pipe(
select(getAvailableLocales),
whenTruthy(),
tap(locales => {
this.meta.setTag('og:locale:alternate', locales.map(x => x.lang).join(','));
})
)
this.actions$.pipe(
takeWhile(() => isPlatformServer(this.platformId)),
ofType(routerNavigatedAction),
withLatestFrom(this.store.pipe(select(getCurrentLocale)), this.store.pipe(select(getAvailableLocales))),
tap(([, current, locales]) => {
this.meta.setTag('og:locale', current.lang);
this.meta.setTag('og:locale:alternate', locales.map(x => x.lang).join(','));
})
),
{ dispatch: false }
);
Expand Down Expand Up @@ -193,8 +180,4 @@ export class SeoEffects {
}
});
}

private waitAppStable<T>(obs: Observable<T>) {
return this.appRef.isStable.pipe(whenTruthy(), first(), switchMapTo(obs));
}
}

0 comments on commit dc3a206

Please sign in to comment.