|
| 1 | +class SeoManager { |
| 2 | + |
| 3 | + static setFavicon(url: string) { |
| 4 | + const link: HTMLLinkElement = document.createElement('link'); |
| 5 | + link.rel = 'icon'; |
| 6 | + link.href = url; |
| 7 | + |
| 8 | + const existingLink = document.querySelector("link[rel='icon']"); |
| 9 | + if (existingLink) { |
| 10 | + document.head.removeChild(existingLink); |
| 11 | + } |
| 12 | + |
| 13 | + document.head.appendChild(link); |
| 14 | + } |
| 15 | + |
| 16 | + static setTitle(title: string) { |
| 17 | + document.title = title; |
| 18 | + SeoManager.setOgTitle(title); |
| 19 | + SeoManager.setMetaTag('og:type', 'website'); |
| 20 | + SeoManager.setTwitterCard('summary_large_image'); |
| 21 | + SeoManager.setTwitterTitle(title); |
| 22 | + } |
| 23 | + |
| 24 | + static setDescription(description: string) { |
| 25 | + SeoManager.setMetaTag('description', description); |
| 26 | + SeoManager.setOgDescription(description); |
| 27 | + SeoManager.setTwitterDescription(description); |
| 28 | + } |
| 29 | + |
| 30 | + static setTwitterCard(cardType: string) { |
| 31 | + SeoManager.setMetaTag('twitter:card', cardType); |
| 32 | + } |
| 33 | + |
| 34 | + static setTwitterTitle(title: string) { |
| 35 | + SeoManager.setMetaTag('twitter:title', title); |
| 36 | + } |
| 37 | + |
| 38 | + static setTwitterDescription(description: string) { |
| 39 | + SeoManager.setMetaTag('twitter:description', description); |
| 40 | + } |
| 41 | + |
| 42 | + static setTwitterImage(imageUrl: string) { |
| 43 | + SeoManager.setMetaTag('twitter:image', imageUrl); |
| 44 | + } |
| 45 | + |
| 46 | + static setOgTitle(title: string) { |
| 47 | + SeoManager.setMetaProperty('og:title', title); |
| 48 | + } |
| 49 | + |
| 50 | + static setOgDescription(description: string) { |
| 51 | + SeoManager.setMetaProperty('og:description', description); |
| 52 | + } |
| 53 | + |
| 54 | + static setOgImage(imageUrl: string) { |
| 55 | + SeoManager.setMetaProperty('og:image', imageUrl); |
| 56 | + } |
| 57 | + |
| 58 | + static setOgUrl(url: string) { |
| 59 | + SeoManager.setMetaProperty('og:url', url); |
| 60 | + } |
| 61 | + |
| 62 | + static setKeywords(keywords: string) { |
| 63 | + SeoManager.setMetaTag('keywords', keywords); |
| 64 | + } |
| 65 | + |
| 66 | + static setAuthor(author: string) { |
| 67 | + SeoManager.setMetaTag('author', author); |
| 68 | + } |
| 69 | + |
| 70 | + private static setMetaTag(name: string, content: string) { |
| 71 | + let meta = document.querySelector(`meta[name='${name}']`) as HTMLMetaElement | null; |
| 72 | + if (!meta) { |
| 73 | + meta = document.createElement('meta'); |
| 74 | + meta.name = name; |
| 75 | + document.head.appendChild(meta); |
| 76 | + } |
| 77 | + meta.content = content; |
| 78 | + } |
| 79 | + |
| 80 | + private static setMetaProperty(property: string, content: string) { |
| 81 | + let meta = document.querySelector(`meta[property='${property}']`); |
| 82 | + if (!meta) { |
| 83 | + meta = document.createElement('meta'); |
| 84 | + meta.setAttribute('property', property); |
| 85 | + document.head.appendChild(meta); |
| 86 | + } |
| 87 | + (meta as HTMLMetaElement).content = content; |
| 88 | + } |
| 89 | +} |
| 90 | + |
| 91 | +export default SeoManager; |
0 commit comments