2023-12-27 12:19:03 +00:00
|
|
|
import OGImage from "../images/og_default.png";
|
2023-12-27 16:20:55 +00:00
|
|
|
import type {
|
|
|
|
ServerRuntimeMetaArgs,
|
|
|
|
ServerRuntimeMetaDescriptor,
|
|
|
|
} from "@remix-run/server-runtime";
|
2023-12-27 12:19:03 +00:00
|
|
|
|
2023-12-27 16:20:55 +00:00
|
|
|
interface GenerateMetaTagsParams {
|
|
|
|
title: string;
|
|
|
|
description: string;
|
|
|
|
imageUrl?: {};
|
|
|
|
ogType?: "website" | "article";
|
|
|
|
parentMeta?: ServerRuntimeMetaArgs;
|
|
|
|
}
|
|
|
|
export function generateMetaTags({
|
|
|
|
title,
|
|
|
|
description,
|
2023-12-27 12:19:03 +00:00
|
|
|
imageUrl = OGImage,
|
2023-12-27 16:20:55 +00:00
|
|
|
ogType = "website",
|
|
|
|
parentMeta,
|
|
|
|
}: GenerateMetaTagsParams) {
|
2023-12-27 16:41:13 +00:00
|
|
|
const metaTags: Record<string, ServerRuntimeMetaDescriptor> = {};
|
|
|
|
|
|
|
|
// Helper function to generate a unique key for each meta tag
|
|
|
|
const generateKey = (tag: ServerRuntimeMetaDescriptor): string => {
|
|
|
|
if ("name" in tag && tag.name) {
|
|
|
|
return `name:${tag.name}`;
|
|
|
|
}
|
|
|
|
if ("property" in tag && tag.property) {
|
|
|
|
return `property:${tag.property}`;
|
|
|
|
}
|
|
|
|
if ("httpEquiv" in tag && tag.httpEquiv)
|
|
|
|
return `httpEquiv:${tag.httpEquiv}`;
|
|
|
|
if ("charSet" in tag) {
|
|
|
|
return "charSet";
|
|
|
|
}
|
|
|
|
if ("title" in tag) {
|
|
|
|
return "title";
|
|
|
|
}
|
|
|
|
if ("script:ld+json" in tag) {
|
|
|
|
return "script:ld+json";
|
|
|
|
}
|
|
|
|
if ("tagName" in tag && tag.tagName) {
|
|
|
|
return `tagName:${tag.tagName}`;
|
|
|
|
}
|
|
|
|
// Fallback for complex or unknown types - convert to JSON string
|
|
|
|
return `unknown:${JSON.stringify(tag)}`;
|
|
|
|
};
|
2023-12-27 16:20:55 +00:00
|
|
|
|
2023-12-27 16:41:13 +00:00
|
|
|
// Merge parent meta tags
|
|
|
|
parentMeta?.matches?.forEach((match) => {
|
|
|
|
match.meta?.forEach((tag) => {
|
|
|
|
const key = generateKey(tag);
|
|
|
|
metaTags[key] = tag;
|
|
|
|
});
|
|
|
|
});
|
2023-12-27 16:20:55 +00:00
|
|
|
|
2023-12-27 16:41:13 +00:00
|
|
|
// Define new meta tags and add or overwrite them in the metaTags object
|
|
|
|
const newMetaTags: ServerRuntimeMetaDescriptor[] = [
|
2023-12-27 15:43:22 +00:00
|
|
|
{ title },
|
2023-12-27 12:19:03 +00:00
|
|
|
{ name: "title", content: title },
|
|
|
|
{ name: "description", content: description },
|
|
|
|
{ name: "og:title", content: title },
|
|
|
|
{ name: "og:description", content: description },
|
|
|
|
{ name: "og:image", content: imageUrl },
|
|
|
|
{ name: "og:type", content: ogType },
|
|
|
|
];
|
2023-12-27 16:41:13 +00:00
|
|
|
|
|
|
|
newMetaTags.forEach((tag) => {
|
|
|
|
const key = generateKey(tag);
|
|
|
|
metaTags[key] = tag;
|
|
|
|
});
|
|
|
|
|
|
|
|
return Object.values(metaTags);
|
2023-12-27 12:19:03 +00:00
|
|
|
}
|