]> git.otsuka.systems Git - cotsuka.github.io/commitdiff
separate out opengraph routes
authorCameron Otsuka <cameron@otsuka.haus>
Tue, 14 Apr 2026 03:39:56 +0000 (20:39 -0700)
committerCameron Otsuka <cameron@otsuka.haus>
Tue, 14 Apr 2026 03:39:56 +0000 (20:39 -0700)
src/pages/articles/[date]-[id]/opengraph.png.ts
src/pages/podcasts/[id]/opengraph.png.ts
src/pages/reviews/[category]/[id]/opengraph.png.ts
src/utils/createOgRoutes.ts [deleted file]

index bc18a6e4a0dd8aab0936a1e711699fb8d42bbf0b..f810548579869136815d325f7c004b70eeed64fe 100644 (file)
@@ -1,10 +1,21 @@
-import { createOgRoutes } from '@utils/createOgRoutes';
+import type { APIRoute } from 'astro';
+import { getCollection } from 'astro:content';
 import formatDate from '@utils/formatDate';
+import generateOpenGraphImage from '@utils/generateOpenGraphImage';
 
-export const { getStaticPaths, GET } = createOgRoutes(
-  'articles',
-  (article) => ({
-    date: formatDate(article.data.date),
-    id: article.id,
-  }),
-);
+export const GET = (async ({ props, url }) => {
+  const entry = props.entry;
+  return generateOpenGraphImage(
+    entry.data.title,
+    entry.data.description,
+    url.origin,
+  );
+}) satisfies APIRoute;
+
+export async function getStaticPaths() {
+  const articles = await getCollection('articles');
+  return articles.map((article) => ({
+    params: { date: formatDate(article.data.date), id: article.id },
+    props: { entry: article },
+  }));
+}
index a7feecd2315b49bf83cbff457459d98b980f8b11..25d66f2479ecbee5bc6dbbb5c2295edfd50a6575 100644 (file)
@@ -1,8 +1,20 @@
-import { createOgRoutes } from '@utils/createOgRoutes';
+import type { APIRoute } from 'astro';
+import { getCollection } from 'astro:content';
+import generateOpenGraphImage from '@utils/generateOpenGraphImage';
 
-export const { getStaticPaths, GET } = createOgRoutes(
-  'podcasts',
-  (podcast) => ({
-    id: podcast.id,
-  }),
-);
+export const GET = (async ({ props, url }) => {
+  const entry = props.entry;
+  return generateOpenGraphImage(
+    entry.data.title,
+    entry.data.description,
+    url.origin,
+  );
+}) satisfies APIRoute;
+
+export async function getStaticPaths() {
+  const podcasts = await getCollection('podcasts');
+  return podcasts.map((podcast) => ({
+    params: { id: podcast.id },
+    props: { entry: podcast },
+  }));
+}
index 958b80e8d644e2a96873a1aa04ea315def461112..ffcc5551b85a43a34c5ca1fcc0d5b31ddd2ddaa0 100644 (file)
@@ -1,11 +1,21 @@
-import { createOgRoutes } from '@utils/createOgRoutes';
+import type { APIRoute } from 'astro';
+import { getCollection } from 'astro:content';
+import generateOpenGraphImage from '@utils/generateOpenGraphImage';
 import generateStarRating from '@utils/generateStarRating';
 
-export const { getStaticPaths, GET } = createOgRoutes(
-  'reviews',
-  (review) => ({
-    category: review.data.category,
-    id: review.id,
-  }),
-  (review) => generateStarRating(review.data.rating) ?? '',
-);
+export const GET = (async ({ props, url }) => {
+  const entry = props.entry;
+  return generateOpenGraphImage(
+    entry.data.title,
+    generateStarRating(entry.data.rating),
+    url.origin,
+  );
+}) satisfies APIRoute;
+
+export async function getStaticPaths() {
+  const reviews = await getCollection('reviews');
+  return reviews.map((review) => ({
+    params: { category: review.data.category, id: review.id },
+    props: { entry: review },
+  }));
+}
diff --git a/src/utils/createOgRoutes.ts b/src/utils/createOgRoutes.ts
deleted file mode 100644 (file)
index de8ed87..0000000
+++ /dev/null
@@ -1,32 +0,0 @@
-import type { APIRoute, GetStaticPaths } from 'astro';
-import { getCollection, type CollectionEntry } from 'astro:content';
-import generateOpenGraphImage from '@utils/generateOpenGraphImage';
-
-type SiteCollection = 'articles' | 'podcasts' | 'reviews';
-
-export function createOgRoutes<T extends SiteCollection>(
-  collectionName: T,
-  getParams: (entry: CollectionEntry<T>) => Record<string, string>,
-  getDescription?: (entry: CollectionEntry<T>) => string,
-) {
-  const getStaticPaths: GetStaticPaths = async () => {
-    const entries = await getCollection(collectionName);
-    return entries.map((entry) => ({
-      params: getParams(entry),
-      props: { entry },
-    }));
-  };
-
-  const GET: APIRoute = async ({ props, url }) => {
-    if (!props.entry) {
-      throw new Error('Missing entry prop in OG route');
-    }
-    const entry = props.entry as CollectionEntry<T>;
-    const description = getDescription
-      ? getDescription(entry)
-      : ((entry.data as { description?: string }).description ?? '');
-    return generateOpenGraphImage(entry.data.title, description, url.origin);
-  };
-
-  return { getStaticPaths, GET };
-}