mirror of
https://github.com/akveo/ngx-admin.git
synced 2025-12-18 16:30:13 +01:00
feat(seo): add canonical tag (#5580)
This commit is contained in:
parent
c046c0d950
commit
d25cc1a4fb
4 changed files with 65 additions and 1 deletions
|
|
@ -9,6 +9,7 @@ import {
|
||||||
AnalyticsService,
|
AnalyticsService,
|
||||||
LayoutService,
|
LayoutService,
|
||||||
PlayerService,
|
PlayerService,
|
||||||
|
SeoService,
|
||||||
StateService,
|
StateService,
|
||||||
} from './utils';
|
} from './utils';
|
||||||
import { UserData } from './data/users';
|
import { UserData } from './data/users';
|
||||||
|
|
@ -141,6 +142,7 @@ export const NB_CORE_PROVIDERS = [
|
||||||
AnalyticsService,
|
AnalyticsService,
|
||||||
LayoutService,
|
LayoutService,
|
||||||
PlayerService,
|
PlayerService,
|
||||||
|
SeoService,
|
||||||
StateService,
|
StateService,
|
||||||
AbService,
|
AbService,
|
||||||
];
|
];
|
||||||
|
|
|
||||||
|
|
@ -2,3 +2,4 @@ export { LayoutService } from './layout.service';
|
||||||
export { AnalyticsService } from './analytics.service';
|
export { AnalyticsService } from './analytics.service';
|
||||||
export { PlayerService } from './player.service';
|
export { PlayerService } from './player.service';
|
||||||
export { StateService } from './state.service';
|
export { StateService } from './state.service';
|
||||||
|
export { SeoService } from './seo.service';
|
||||||
|
|
|
||||||
58
src/app/@core/utils/seo.service.ts
Normal file
58
src/app/@core/utils/seo.service.ts
Normal file
|
|
@ -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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -10,6 +10,7 @@ import { NbThemeService } from '@nebular/theme';
|
||||||
import { AnalyticsService } from './@core/utils/analytics.service';
|
import { AnalyticsService } from './@core/utils/analytics.service';
|
||||||
import { AbService } from './@core/utils/ab.service';
|
import { AbService } from './@core/utils/ab.service';
|
||||||
import { withLatestFrom, filter } from 'rxjs/operators';
|
import { withLatestFrom, filter } from 'rxjs/operators';
|
||||||
|
import { SeoService } from './@core/utils/seo.service';
|
||||||
|
|
||||||
@Component({
|
@Component({
|
||||||
selector: 'ngx-app',
|
selector: 'ngx-app',
|
||||||
|
|
@ -22,7 +23,8 @@ export class AppComponent implements OnInit {
|
||||||
constructor(private analytics: AnalyticsService,
|
constructor(private analytics: AnalyticsService,
|
||||||
private activatedRoute: ActivatedRoute,
|
private activatedRoute: ActivatedRoute,
|
||||||
private abService: AbService,
|
private abService: AbService,
|
||||||
private themeService: NbThemeService) {
|
private themeService: NbThemeService,
|
||||||
|
private seoService: SeoService) {
|
||||||
|
|
||||||
this.themeService.onThemeChange()
|
this.themeService.onThemeChange()
|
||||||
.subscribe((theme: any) => {
|
.subscribe((theme: any) => {
|
||||||
|
|
@ -58,5 +60,6 @@ export class AppComponent implements OnInit {
|
||||||
this.themeService.changeTheme(themeName);
|
this.themeService.changeTheme(themeName);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
this.seoService.trackCanonicalChanges();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue