import { getSiteInfo } from '@/lib/i18n';
import { ReactElement } from 'react';
interface SEOProps {
title: string;
description?: string;
locale?: 'en' | 'de';
path?: string;
type?: 'website' | 'article' | 'product';
publishedTime?: string;
modifiedTime?: string;
authors?: string[];
images?: string[];
}
export function SEO({
title,
description,
locale = 'en',
path = '/',
type = 'website',
publishedTime,
modifiedTime,
authors,
images
}: SEOProps): ReactElement {
const site = getSiteInfo();
const fullTitle = title === 'Home' ? site.title : `${title} | ${site.title}`;
const fullDescription = description || site.description;
const canonicalUrl = `${site.baseUrl}${path}`;
// Generate alternate URLs
const alternateLocale = locale === 'en' ? 'de' : 'en';
const alternatePath = path === '/' ? '' : path;
const alternateUrl = `${site.baseUrl}/${alternateLocale}${alternatePath}`;
// Open Graph images
const ogImages = images && images.length > 0
? images
: [`${site.baseUrl}/og-image.jpg`];
return (
<>
{/* Basic Meta Tags */}
{fullTitle}
{/* Canonical URL */}
{/* Alternate Languages */}
{/* Open Graph */}
{ogImages.map((image, index) => (
))}
{publishedTime && (
)}
{modifiedTime && (
)}
{authors && authors.length > 0 && (
)}
{/* Twitter Card */}
{ogImages[0] && (
)}
{/* Site Info */}
{/* Favicon (placeholder) */}
>
);
}
export function generateSEOMetadata(props: SEOProps) {
const site = getSiteInfo();
const fullTitle = props.title === 'Home' ? site.title : `${props.title} | ${site.title}`;
const description = props.description || site.description;
const canonicalUrl = `${site.baseUrl}${props.path || '/'}`;
const alternateLocale = props.locale === 'en' ? 'de' : 'en';
const alternatePath = props.path && props.path !== '/' ? props.path : '';
const alternateUrl = `${site.baseUrl}/${alternateLocale}${alternatePath}`;
return {
title: fullTitle,
description,
metadataBase: new URL(site.baseUrl),
alternates: {
canonical: canonicalUrl,
languages: {
[props.locale || 'en']: canonicalUrl,
[alternateLocale]: alternateUrl,
},
},
openGraph: {
title: fullTitle,
description,
type: props.type || 'website',
locale: props.locale || 'en',
siteName: site.title,
url: canonicalUrl,
...(props.images && props.images.length > 0 && {
images: props.images.map(img => ({ url: img, alt: fullTitle })),
}),
...(props.publishedTime && { publishedTime: props.publishedTime }),
...(props.modifiedTime && { modifiedTime: props.modifiedTime }),
...(props.authors && { authors: props.authors }),
},
twitter: {
card: 'summary_large_image',
title: fullTitle,
description,
...(props.images && props.images[0] && { images: [props.images[0]] }),
},
authors: props.authors ? props.authors.map(name => ({ name })) : undefined,
};
}