Files
sass-mohfarawati/lib/metadata.ts
T
2026-03-07 14:10:30 +01:00

57 lines
1.4 KiB
TypeScript

import type { Metadata } from "next";
import { routing } from "@/i18n/routing";
import { AppLocale, getLocalizedPath, resolveLocale } from "@/lib/locale";
function getSiteUrl(): URL {
return new URL(process.env.NEXT_PUBLIC_SITE_URL ?? "https://mohfarawati.de");
}
function toAbsoluteUrl(pathname: string): string {
return new URL(pathname, getSiteUrl()).toString();
}
export function buildLocaleAlternates(pathname: string) {
const languages = Object.fromEntries(
routing.locales.map((locale) => [locale, toAbsoluteUrl(getLocalizedPath(locale, pathname))]),
) as Record<AppLocale, string>;
return {
canonical: toAbsoluteUrl(getLocalizedPath(routing.defaultLocale, pathname)),
languages: {
...languages,
"x-default": toAbsoluteUrl(getLocalizedPath(routing.defaultLocale, pathname)),
},
};
}
type LocalizedMetadataInput = {
locale: string;
pathname: string;
title: string;
description: string;
};
export function buildLocalizedMetadata({
locale,
pathname,
title,
description,
}: LocalizedMetadataInput): Metadata {
const localeKey = resolveLocale(locale);
return {
title,
description,
alternates: buildLocaleAlternates(pathname),
openGraph: {
title,
description,
url: toAbsoluteUrl(getLocalizedPath(localeKey, pathname)),
siteName: "moh-sass",
locale: localeKey,
type: "website",
},
};
}