| |
| |
| |
| |
|
|
| export interface SEOConfig { |
| title: string; |
| description: string; |
| keywords: string; |
| schema?: string; |
| canonicalUrl?: string; |
| } |
|
|
| export function updateSEO({ title, description, keywords, schema, canonicalUrl }: SEOConfig) { |
| |
| document.title = title; |
|
|
| |
| let metaDesc = document.querySelector('meta[name="description"]'); |
| if (!metaDesc) { |
| metaDesc = document.createElement("meta"); |
| metaDesc.setAttribute("name", "description"); |
| document.head.appendChild(metaDesc); |
| } |
| metaDesc.setAttribute("content", description); |
|
|
| |
| let metaKeywords = document.querySelector('meta[name="keywords"]'); |
| if (!metaKeywords) { |
| metaKeywords = document.createElement("meta"); |
| metaKeywords.setAttribute("name", "keywords"); |
| document.head.appendChild(metaKeywords); |
| } |
| metaKeywords.setAttribute("content", keywords); |
|
|
| |
| let ogTitle = document.querySelector('meta[property="og:title"]'); |
| if (!ogTitle) { |
| ogTitle = document.createElement("meta"); |
| ogTitle.setAttribute("property", "og:title"); |
| document.head.appendChild(ogTitle); |
| } |
| ogTitle.setAttribute("content", title); |
|
|
| let ogDesc = document.querySelector('meta[property="og:description"]'); |
| if (!ogDesc) { |
| ogDesc = document.createElement("meta"); |
| ogDesc.setAttribute("property", "og:description"); |
| document.head.appendChild(ogDesc); |
| } |
| ogDesc.setAttribute("content", description); |
|
|
| |
| const fullUrl = canonicalUrl || window.location.href; |
| let linkCanonical = document.querySelector('link[rel="canonical"]'); |
| if (!linkCanonical) { |
| linkCanonical = document.createElement("link"); |
| linkCanonical.setAttribute("rel", "canonical"); |
| document.head.appendChild(linkCanonical); |
| } |
| linkCanonical.setAttribute("href", fullUrl); |
|
|
| |
| let schemaScript = document.getElementById("lumina-structured-data"); |
| if (schemaScript) { |
| schemaScript.remove(); |
| } |
| |
| if (schema) { |
| schemaScript = document.createElement("script"); |
| schemaScript.id = "lumina-structured-data"; |
| schemaScript.setAttribute("type", "application/ld+json"); |
| schemaScript.innerHTML = schema; |
| document.head.appendChild(schemaScript); |
| } |
| } |
|
|