Last active
February 28, 2021 03:34
-
-
Save arturovt/940a337e2ef95a0c303682a659fdc4a4 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
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