Files
anouma/lib/seo/metadata.ts
T

53 lines
1.7 KiB
TypeScript

import type { Metadata } from "next";
import { siteConfig } from "@/lib/site";
import { getSiteUrl, isSiteIndexable } from "./config";
/**
* Single shared Metadata builder used by every public page's
* generateMetadata - guarantees every page gets its own canonical, robots,
* Open Graph and Twitter/X card data instead of copy-pasted boilerplate
* (and the accidental duplicate-description bugs that come with that).
*
* `path` is the page's path relative to the site root (e.g.
* "/angebote/doula-begleitung", or "" for the homepage).
*/
export async function buildMetadata(args: {
title: string;
description: string;
path: string;
ogImageUrl?: string;
keywords?: string;
noindex?: boolean;
type?: "website" | "article";
}): Promise<Metadata> {
const [siteUrl, sitewideIndexable] = await Promise.all([getSiteUrl(), isSiteIndexable()]);
const canonical = `${siteUrl}${args.path}`;
const indexable = !args.noindex && sitewideIndexable;
const images = args.ogImageUrl ? [{ url: args.ogImageUrl }] : undefined;
return {
title: args.title,
description: args.description,
keywords: args.keywords,
alternates: { canonical },
robots: indexable
? { index: true, follow: true }
: { index: false, follow: false },
openGraph: {
title: args.title,
description: args.description,
url: canonical,
siteName: siteConfig.name,
type: args.type ?? "website",
images,
locale: siteConfig.locale,
},
twitter: {
card: images ? "summary_large_image" : "summary",
title: args.title,
description: args.description,
images: images?.map((image) => image.url),
},
};
}