|
| 1 | +import { Injectable, OnDestroy } from '@angular/core'; |
| 2 | +import { Router, NavigationEnd } from '@angular/router'; |
| 3 | +import { Subscription } from 'rxjs'; |
| 4 | +import { Meta, MetaDefinition, Title } from '@angular/platform-browser'; |
| 5 | + |
| 6 | +import { meta } from '../helpers/meta-data'; |
| 7 | + |
| 8 | +@Injectable({ |
| 9 | + providedIn: 'root', |
| 10 | +}) |
| 11 | +export class SeoService implements OnDestroy { |
| 12 | + private subscription = new Subscription(); |
| 13 | + |
| 14 | + constructor( |
| 15 | + private router: Router, |
| 16 | + private meta: Meta, |
| 17 | + private title: Title |
| 18 | + ) { |
| 19 | + this.subscription.add( |
| 20 | + this.router.events.subscribe(event => { |
| 21 | + if (event instanceof NavigationEnd) { |
| 22 | + const url = event.url; |
| 23 | + this.updateTitle(url); |
| 24 | + this.updateMeta(url); |
| 25 | + } |
| 26 | + }) |
| 27 | + ); |
| 28 | + } |
| 29 | + |
| 30 | + private updateTitle(url: string): void { |
| 31 | + this.title.setTitle(meta[url].title); |
| 32 | + } |
| 33 | + |
| 34 | + private updateMeta(url: string): void { |
| 35 | + const oldTagOgTitle = this.meta.getTag('property="og:title"'); |
| 36 | + const newTagOgTitle = { |
| 37 | + property: 'og:title', |
| 38 | + content: meta[url].title, |
| 39 | + }; |
| 40 | + const oldTagTwitterTitle = this.meta.getTag('name="twitter:title"'); |
| 41 | + const newTagTwitterTitle = { |
| 42 | + name: 'twitter:title', |
| 43 | + content: meta[url].title, |
| 44 | + }; |
| 45 | + const oldTagDescription = this.meta.getTag('name="description"'); |
| 46 | + const newTagDescription = { |
| 47 | + name: 'description', |
| 48 | + content: meta[url].description, |
| 49 | + }; |
| 50 | + const oldTagOgDescription = this.meta.getTag('property="og:description"'); |
| 51 | + const newTagOgDescription = { |
| 52 | + property: 'og:description', |
| 53 | + content: meta[url].description, |
| 54 | + }; |
| 55 | + const oldTagTwitterDescription = this.meta.getTag( |
| 56 | + 'property="og:description"' |
| 57 | + ); |
| 58 | + const newTagTwitterDescription = { |
| 59 | + property: 'og:description', |
| 60 | + content: meta[url].description, |
| 61 | + }; |
| 62 | + const oldTagOgImage = this.meta.getTag('property="og:image"'); |
| 63 | + const imageTag = |
| 64 | + meta[url].metaTags?.['image'] ?? |
| 65 | + this.meta.getTag('property="og:image"')!.content; |
| 66 | + const newTagOgImage = { |
| 67 | + property: 'og:image', |
| 68 | + content: imageTag, |
| 69 | + }; |
| 70 | + const oldTagTwitterImage = this.meta.getTag('name="twitter:image"'); |
| 71 | + const newTagTwitterImage = { |
| 72 | + name: 'twitter:image', |
| 73 | + content: imageTag, |
| 74 | + }; |
| 75 | + const oldTagOgUrl = this.meta.getTag('property="og:url"'); |
| 76 | + const newTagOgUrl = { |
| 77 | + property: 'og:url', |
| 78 | + content: meta[url].metaTags?.['og:url'], |
| 79 | + }; |
| 80 | + const oldTagKeywords = this.meta.getTag('name="keywords"'); |
| 81 | + const newTagKeywords = { |
| 82 | + name: 'keywords', |
| 83 | + content: meta[url].keywords, |
| 84 | + }; |
| 85 | + |
| 86 | + // Update description |
| 87 | + oldTagDescription |
| 88 | + ? this.meta.updateTag(newTagDescription as MetaDefinition) |
| 89 | + : this.meta.addTag(newTagDescription as MetaDefinition); |
| 90 | + // Update og:description |
| 91 | + oldTagOgDescription |
| 92 | + ? this.meta.updateTag(newTagOgDescription as MetaDefinition) |
| 93 | + : this.meta.addTag(newTagOgDescription as MetaDefinition); |
| 94 | + // Update twitter:description |
| 95 | + oldTagTwitterDescription |
| 96 | + ? this.meta.updateTag(newTagTwitterDescription as MetaDefinition) |
| 97 | + : this.meta.addTag(newTagTwitterDescription as MetaDefinition); |
| 98 | + // Update og:title |
| 99 | + oldTagOgTitle |
| 100 | + ? this.meta.updateTag(newTagOgTitle as MetaDefinition) |
| 101 | + : this.meta.addTag(newTagOgTitle as MetaDefinition); |
| 102 | + // Update twitter:title |
| 103 | + oldTagTwitterTitle |
| 104 | + ? this.meta.updateTag(newTagTwitterTitle as MetaDefinition) |
| 105 | + : this.meta.addTag(newTagTwitterTitle as MetaDefinition); |
| 106 | + // Update og:image |
| 107 | + oldTagOgImage |
| 108 | + ? this.meta.updateTag(newTagOgImage as MetaDefinition) |
| 109 | + : this.meta.addTag(newTagOgImage as MetaDefinition); |
| 110 | + // Update twitter:image |
| 111 | + oldTagTwitterImage |
| 112 | + ? this.meta.updateTag(newTagTwitterImage as MetaDefinition) |
| 113 | + : this.meta.addTag(newTagTwitterImage as MetaDefinition); |
| 114 | + // Update og:url |
| 115 | + oldTagOgUrl |
| 116 | + ? this.meta.updateTag(newTagOgUrl as MetaDefinition) |
| 117 | + : this.meta.addTag(newTagOgUrl as MetaDefinition); |
| 118 | + // Update keywords |
| 119 | + oldTagKeywords |
| 120 | + ? this.meta.updateTag(newTagKeywords as MetaDefinition) |
| 121 | + : this.meta.addTag(newTagKeywords as MetaDefinition); |
| 122 | + } |
| 123 | + |
| 124 | + ngOnDestroy() { |
| 125 | + this.subscription.unsubscribe(); |
| 126 | + } |
| 127 | +} |
0 commit comments