Skip to content

Instantly share code, notes, and snippets.

@arturovt
Last active February 28, 2021 03:34
Show Gist options
  • Save arturovt/940a337e2ef95a0c303682a659fdc4a4 to your computer and use it in GitHub Desktop.
Save arturovt/940a337e2ef95a0c303682a659fdc4a4 to your computer and use it in GitHub Desktop.
export class ArticleComponent {
article$: Observable<Article>;
constructor(
http: HttpClient,
route: ActivatedRoute,
@Inject(PLATFORM_ID) platformId: string,
meta: Meta,
transferState: TransferState,
) {
if (isPlatformServer(platformId)) {
this.article$ = route.params.pipe(
switchMap(params =>
http.get<Article>(`${environment.apiUrl}/api/article/${params.id}`),
),
tap(article => {
meta.addTags([
{
property: 'og:type',
content: 'article',
},
{
property: 'og:title',
content: article.title,
},
{
property: 'og:url',
content: article.url,
},
{
property: 'og:image',
content: article.image,
},
]);
const key = makeStateKey<Article>(`article:${article.id}`);
transferState.set(key, article);
}),
);
} else {
this.article$ = route.params.pipe(
map(params => {
const key = makeStateKey<Article>(`article:${params.id}`);
const article = transferState.get(key, null!);
return article;
}),
);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment