|
| 1 | +import { useEffect } from 'react'; |
| 2 | + |
| 3 | +interface MetaTagsProps { |
| 4 | + title: string; |
| 5 | + description: string; |
| 6 | + image: string; |
| 7 | + url?: string; |
| 8 | + type?: string; |
| 9 | +} |
| 10 | + |
| 11 | +export function useMetaTags({ title, description, image, url, type = 'article' }: MetaTagsProps) { |
| 12 | + useEffect(() => { |
| 13 | + // Get base URL |
| 14 | + const baseUrl = window.location.origin; |
| 15 | + const currentUrl = url || window.location.href; |
| 16 | + |
| 17 | + // Convert relative image URL to absolute |
| 18 | + let imageUrl = image; |
| 19 | + if (!image.startsWith('http')) { |
| 20 | + // Handle relative paths |
| 21 | + if (image.startsWith('/')) { |
| 22 | + imageUrl = `${baseUrl}${image}`; |
| 23 | + } else if (image.startsWith('./') || image.startsWith('../')) { |
| 24 | + // Handle relative paths from current location |
| 25 | + imageUrl = `${baseUrl}/${image}`; |
| 26 | + } else { |
| 27 | + // Assume it's in assets folder |
| 28 | + imageUrl = `${baseUrl}/assets/${image}`; |
| 29 | + } |
| 30 | + } |
| 31 | + |
| 32 | + // Update or create meta tags |
| 33 | + const updateMetaTag = (property: string, content: string, isProperty = true) => { |
| 34 | + const selector = isProperty ? `meta[property="${property}"]` : `meta[name="${property}"]`; |
| 35 | + let element = document.querySelector(selector) as HTMLMetaElement; |
| 36 | + |
| 37 | + if (!element) { |
| 38 | + element = document.createElement('meta'); |
| 39 | + if (isProperty) { |
| 40 | + element.setAttribute('property', property); |
| 41 | + } else { |
| 42 | + element.setAttribute('name', property); |
| 43 | + } |
| 44 | + document.head.appendChild(element); |
| 45 | + } |
| 46 | + |
| 47 | + element.setAttribute('content', content); |
| 48 | + }; |
| 49 | + |
| 50 | + // Update title |
| 51 | + document.title = title; |
| 52 | + |
| 53 | + // Open Graph tags |
| 54 | + updateMetaTag('og:title', title); |
| 55 | + updateMetaTag('og:description', description); |
| 56 | + updateMetaTag('og:image', imageUrl); |
| 57 | + updateMetaTag('og:url', currentUrl); |
| 58 | + updateMetaTag('og:type', type); |
| 59 | + |
| 60 | + // Twitter Card tags |
| 61 | + updateMetaTag('twitter:card', 'summary_large_image', false); |
| 62 | + updateMetaTag('twitter:title', title, false); |
| 63 | + updateMetaTag('twitter:description', description, false); |
| 64 | + updateMetaTag('twitter:image', imageUrl, false); |
| 65 | + |
| 66 | + // Standard meta tags |
| 67 | + updateMetaTag('description', description, false); |
| 68 | + |
| 69 | + // Cleanup function to restore default meta tags |
| 70 | + return () => { |
| 71 | + document.title = 'Regan Maharjan Portfolio Website'; |
| 72 | + const defaultDescription = 'Computer scientist and Application Systems Analyst Senior bridging technology, education, and human-centered design'; |
| 73 | + updateMetaTag('og:title', 'Regan Maharjan Portfolio Website'); |
| 74 | + updateMetaTag('og:description', defaultDescription); |
| 75 | + updateMetaTag('og:image', `${baseUrl}/assets/raylogo.png`); |
| 76 | + updateMetaTag('og:url', baseUrl); |
| 77 | + updateMetaTag('og:type', 'website'); |
| 78 | + updateMetaTag('twitter:card', 'summary_large_image', false); |
| 79 | + updateMetaTag('twitter:title', 'Regan Maharjan Portfolio Website', false); |
| 80 | + updateMetaTag('twitter:description', defaultDescription, false); |
| 81 | + updateMetaTag('twitter:image', `${baseUrl}/assets/raylogo.png`, false); |
| 82 | + updateMetaTag('description', defaultDescription, false); |
| 83 | + }; |
| 84 | + }, [title, description, image, url, type]); |
| 85 | +} |
| 86 | + |
0 commit comments