From 4806ad5499de49425952b0f2825daa621fc92a0d Mon Sep 17 00:00:00 2001 From: Max Richter Date: Thu, 23 Jan 2025 18:42:29 +0100 Subject: [PATCH] feat(backend): og image and meta tags --- components/MetaTags.tsx | 54 +++++++++++++++++++++++++++++++++++++ routes/articles/[name].tsx | 2 ++ routes/movies/[name].tsx | 2 ++ routes/recipes/[name].tsx | 2 ++ routes/series/[name].tsx | 2 ++ static/og-image.jpg | Bin 0 -> 189761 bytes 6 files changed, 62 insertions(+) create mode 100644 components/MetaTags.tsx create mode 100755 static/og-image.jpg diff --git a/components/MetaTags.tsx b/components/MetaTags.tsx new file mode 100644 index 0000000..2b4b475 --- /dev/null +++ b/components/MetaTags.tsx @@ -0,0 +1,54 @@ +import { GenericResource } from "@lib/types.ts"; +import { Head } from "$fresh/runtime.ts"; + +function generateJsonLd(resource: GenericResource): string { + const baseSchema: Record = { + "@context": "https://schema.org", + "@type": resource.type.charAt(0).toUpperCase() + resource.type.slice(1), // Converts type to PascalCase + name: resource.name, + description: resource.content || resource.meta?.average || "", + keywords: resource.tags?.join(", ") || "", + image: resource.meta?.image || "/images/og-image.jpg", + }; + + if (resource.meta?.author) { + baseSchema.author = { + "@type": "Person", + name: resource.meta.author, + }; + } + + if (resource.meta?.date) { + baseSchema.datePublished = new Date(resource.meta.date).toISOString(); + } + + if (resource.meta?.rating) { + baseSchema.aggregateRating = { + "@type": "AggregateRating", + ratingValue: resource.meta.rating, + bestRating: 10, // Assuming a scale of 1 to 10 + }; + } + + return JSON.stringify(baseSchema, null, 2); +} + +export function MetaTags({ resource }: { resource: GenericResource }) { + const jsonLd = generateJsonLd(resource); + return ( + <> + + + + +