39 lines
889 B
TypeScript
39 lines
889 B
TypeScript
import { Metadata } from 'next';
|
|
import { SUPPORTED_LOCALES } from './locales';
|
|
|
|
const baseUrl = process.env.NEXT_PUBLIC_SITE_URL || 'http://localhost:3000';
|
|
|
|
type SeoMetadataProps = {
|
|
title: string;
|
|
description: string;
|
|
path: string; // e.g. '/shop', '/category/1', or ''
|
|
lang: string;
|
|
openGraph?: Metadata['openGraph'];
|
|
};
|
|
|
|
export function generateSeoMetadata({
|
|
title,
|
|
description,
|
|
path,
|
|
lang,
|
|
openGraph,
|
|
}: SeoMetadataProps): Metadata {
|
|
const cleanPath = path.startsWith('/') ? path : `/${path}`;
|
|
const pathPart = cleanPath === '/' ? '' : cleanPath;
|
|
|
|
const languages: Record<string, string> = {};
|
|
SUPPORTED_LOCALES.forEach((locale) => {
|
|
languages[locale] = `${baseUrl}/${locale}${pathPart}`;
|
|
});
|
|
|
|
return {
|
|
title,
|
|
description,
|
|
alternates: {
|
|
canonical: `${baseUrl}/${lang}${pathPart}`,
|
|
languages,
|
|
},
|
|
openGraph,
|
|
};
|
|
}
|