Created
February 28, 2021 02:09
-
-
Save arturovt/043f38f810b06466682967a89bf72b8c to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { ɵɵdirectiveInject as directiveInject } from '@angular/core'; | |
export class ArticleComponent { | |
article$: Observable<Article>; | |
constructor(route: ActivatedRoute, transferState: TransferState) { | |
if (isPlatformServer(directiveInject(PLATFORM_ID))) { | |
const meta = directiveInject(Meta); | |
const http = directiveInject(HttpClient); | |
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