import { DOCUMENT } from '@angular/common' ; import { Inject , Injectable } from '@angular/core' ; import { Meta , Title } from '@angular/platform-browser' ; @ Injectable ( { providedIn : 'root' , } ) export class SeoService { private affix = '🍀 Angular References' ; private prefix = '' ; private page = '' ; private keywords = [ 'angular' , 'angular 2' , 'angularjs' , 'references' , 'web' , 'typescript' , 'javascript' , 'web-framework' ] ; constructor ( private title , private meta , @ Inject ( DOCUMENT ) private document ) { this . compileMeta ( ) ; } setAffix ( value ) { this . affix = value ; this . compileMeta ( ) ; } setPrefix ( value ) { this . prefix = value ; this . compileMeta ( ) ; } setPage ( value ) { this . page = value ; this . compileMeta ( ) ; } setLang ( lang ) { if ( this . document && this . document . documentElement && this . document . documentElement . setAttribute ) { this . document . documentElement . setAttribute ( 'lang' , lang ) ; } } private compileMeta ( ) { this . title . setTitle ( this . page ? `${ this . prefix }: ${ this . page } | ${ this . affix }` : this . affix ) ; this . meta . updateTag ( { name : 'keywords' , content : [ ... this . keywords , this . prefix , this . page ] . filter ( k => ! ! k ) . join ( ', ' ) , } ) ; this . document ?. head ?. querySelectorAll ( 'link[rel="canonical"]' ) . forEach ( e => e . parentNode . removeChild ( e ) ) ; } }