Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(seo): add canonical tag #5578

Merged
merged 4 commits into from
Dec 20, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/app/@core/core.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
AnalyticsService,
LayoutService,
PlayerService,
SeoService,
StateService,
} from './utils';
import { UserData } from './data/users';
Expand Down Expand Up @@ -140,6 +141,7 @@ export const NB_CORE_PROVIDERS = [
AnalyticsService,
LayoutService,
PlayerService,
SeoService,
StateService,
];

Expand Down
2 changes: 2 additions & 0 deletions src/app/@core/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@ import { LayoutService } from './layout.service';
import { AnalyticsService } from './analytics.service';
import { PlayerService } from './player.service';
import { StateService } from './state.service';
import { SeoService } from './seo.service';

export {
LayoutService,
AnalyticsService,
PlayerService,
SeoService,
StateService,
};
58 changes: 58 additions & 0 deletions src/app/@core/utils/seo.service.ts
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;
}
}
4 changes: 3 additions & 1 deletion src/app/app.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,19 @@
*/
import { Component, OnInit } from '@angular/core';
import { AnalyticsService } from './@core/utils/analytics.service';
import { SeoService } from './@core/utils/seo.service';

@Component({
selector: 'ngx-app',
template: '<router-outlet></router-outlet>',
})
export class AppComponent implements OnInit {

constructor(private analytics: AnalyticsService) {
constructor(private analytics: AnalyticsService, private seoService: SeoService) {
}

ngOnInit(): void {
this.analytics.trackPageViews();
this.seoService.trackCanonicalChanges();
}
}