@asgerami/zemenay-blog
Version:
Plug-and-play blog system for Next.js - Get a fully functional blog running in minutes with zero configuration
77 lines (76 loc) • 2.56 kB
JavaScript
;
"use client";
Object.defineProperty(exports, "__esModule", { value: true });
exports.SEOHead = SEOHead;
const react_1 = require("react");
const seo_1 = require("../lib/seo");
function SEOHead({ seoData, baseUrl }) {
(0, react_1.useEffect)(() => {
// Update document title
document.title = seoData.title;
// Generate meta tags
const metaTags = (0, seo_1.generateMetaTags)(seoData);
// Update existing meta tags or create new ones
Object.entries(metaTags).forEach(([key, value]) => {
updateMetaTag(key, value);
});
// Add structured data
const structuredData = (0, seo_1.generateStructuredData)(seoData, baseUrl);
updateStructuredData(structuredData);
// Cleanup function to remove added elements
return () => {
// Note: In a real app, you might want to restore previous meta tags
// For now, we'll leave them as they help with SEO
};
}, [seoData, baseUrl]);
// This component doesn't render anything visible
return null;
}
/**
* Update or create a meta tag
*/
function updateMetaTag(name, content) {
// Handle different meta tag types
let selector;
let attribute;
if (name.startsWith("og:") || name.startsWith("article:")) {
selector = `meta[property="${name}"]`;
attribute = "property";
}
else if (name.startsWith("twitter:")) {
selector = `meta[name="${name}"]`;
attribute = "name";
}
else if (name === "title") {
// Title is handled separately
return;
}
else {
selector = `meta[name="${name}"]`;
attribute = "name";
}
// Find existing meta tag or create new one
let metaTag = document.querySelector(selector);
if (!metaTag) {
metaTag = document.createElement("meta");
metaTag.setAttribute(attribute, name);
document.head.appendChild(metaTag);
}
metaTag.setAttribute("content", content);
}
/**
* Update structured data JSON-LD
*/
function updateStructuredData(data) {
// Remove existing structured data
const existingScript = document.querySelector('script[type="application/ld+json"][data-seo]');
if (existingScript) {
existingScript.remove();
}
// Add new structured data
const script = document.createElement("script");
script.type = "application/ld+json";
script.setAttribute("data-seo", "true");
script.textContent = JSON.stringify(data);
document.head.appendChild(script);
}