-
Notifications
You must be signed in to change notification settings - Fork 8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(seo): add canonical tag (#5580)
- Loading branch information
Showing
4 changed files
with
65 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
import { Injectable, Inject, PLATFORM_ID, OnDestroy } from '@angular/core'; | ||
import { isPlatformBrowser } from '@angular/common'; | ||
import { NavigationEnd, Router } from '@angular/router'; | ||
import { NB_DOCUMENT } from '@nebular/theme'; | ||
import { filter, takeUntil } from 'rxjs/operators'; | ||
import { Subject } from 'rxjs'; | ||
|
||
@Injectable() | ||
export class SeoService implements OnDestroy { | ||
|
||
private readonly destroy$ = new Subject<void>(); | ||
private readonly dom: Document; | ||
private readonly isBrowser: boolean; | ||
private linkCanonical: HTMLLinkElement; | ||
|
||
constructor( | ||
private router: Router, | ||
@Inject(NB_DOCUMENT) document, | ||
@Inject(PLATFORM_ID) platformId, | ||
) { | ||
this.isBrowser = isPlatformBrowser(platformId); | ||
this.dom = document; | ||
|
||
if (this.isBrowser) { | ||
this.createCanonicalTag(); | ||
} | ||
} | ||
|
||
ngOnDestroy() { | ||
this.destroy$.next(); | ||
this.destroy$.complete(); | ||
} | ||
|
||
createCanonicalTag() { | ||
this.linkCanonical = this.dom.createElement('link'); | ||
this.linkCanonical.setAttribute('rel', 'canonical'); | ||
this.dom.head.appendChild(this.linkCanonical); | ||
this.linkCanonical.setAttribute('href', this.getCanonicalUrl()); | ||
} | ||
|
||
trackCanonicalChanges() { | ||
if (!this.isBrowser) { | ||
return; | ||
} | ||
|
||
this.router.events.pipe( | ||
filter((event) => event instanceof NavigationEnd), | ||
takeUntil(this.destroy$), | ||
) | ||
.subscribe(() => { | ||
this.linkCanonical.setAttribute('href', this.getCanonicalUrl()); | ||
}); | ||
} | ||
|
||
private getCanonicalUrl(): string { | ||
return this.dom.location.origin + this.dom.location.pathname; | ||
} | ||
} |
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