-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: implement social card meta tags
- Loading branch information
1 parent
285d2b5
commit 5c824e0
Showing
8 changed files
with
91 additions
and
2 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import { Injectable } from '@angular/core' | ||
import { Meta } from '@angular/platform-browser' | ||
import { Router, ActivatedRoute, NavigationEnd } from '@angular/router' | ||
import type { ActivatedRouteSnapshot, Event } from '@angular/router' | ||
import { filter } from 'rxjs/operators' | ||
import { BrowserApiService } from 'shared/browser-api/browser-api.service' | ||
|
||
export interface ActivatedRouteSnapshotWithMetadata extends ActivatedRouteSnapshot { | ||
data: { | ||
description: string | ||
image: string | ||
} | ||
} | ||
|
||
@Injectable() | ||
export class MetadataService { | ||
public constructor( | ||
private readonly meta: Meta, | ||
private readonly router: Router, | ||
private readonly route: ActivatedRoute, | ||
private readonly browserApiService: BrowserApiService, | ||
) { | ||
this.router.events | ||
.pipe(filter((event: Event): event is NavigationEnd => event instanceof NavigationEnd)) | ||
.subscribe(() => { | ||
const { title, data } = this.route.snapshot as ActivatedRouteSnapshotWithMetadata | ||
|
||
this.browserApiService.with('window', window => { | ||
this.update(window.location.href, title ?? '', data.description, data.image) | ||
}) | ||
}) | ||
} | ||
|
||
public update(url: string, title: string, description: string, image: string): void { | ||
// title & description | ||
this.meta.updateTag({ name: 'title', content: title }) | ||
this.meta.updateTag({ name: 'description', content: description }) | ||
|
||
// open graph | ||
this.meta.updateTag({ property: 'og:type', content: 'website' }) | ||
this.meta.updateTag({ property: 'og:url', content: url }) | ||
this.meta.updateTag({ property: 'og:title', content: title }) | ||
this.meta.updateTag({ property: 'og:description', content: description }) | ||
this.meta.updateTag({ property: 'og:image', content: image }) | ||
|
||
this.meta.updateTag({ property: 'twitter:card', content: 'summary_large_image' }) | ||
this.meta.updateTag({ property: 'twitter:url', content: url }) | ||
this.meta.updateTag({ property: 'twitter:title', content: title }) | ||
this.meta.updateTag({ property: 'twitter:description', content: description }) | ||
this.meta.updateTag({ property: 'twitter:image', content: image }) | ||
} | ||
} |
This file contains 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