From 9e93e7cbe2c04034eef0438c15cf4f877284873e Mon Sep 17 00:00:00 2001 From: Arik Chakma Date: Wed, 24 Apr 2024 21:55:26 +0600 Subject: [PATCH] wip --- src/lib/open-graph.ts | 5 +++++ src/pages/og/[slug].ts | 25 +++++++++++-------------- 2 files changed, 16 insertions(+), 14 deletions(-) diff --git a/src/lib/open-graph.ts b/src/lib/open-graph.ts index 17beff819..5bc95e173 100644 --- a/src/lib/open-graph.ts +++ b/src/lib/open-graph.ts @@ -6,3 +6,8 @@ type RoadmapOpenGraphQuery = { export function getOpenGraphImageUrl(params: RoadmapOpenGraphQuery) { return `${import.meta.env.DEV ? 'http://localhost:3000' : 'https://roadmap.sh'}/og-images/${params.group}/${params.resourceId}.png`; } + +export async function getDefaultOpenGraphImageBuffer() { + const defaultImageUrl = `${import.meta.env.DEV ? 'http://localhost:3000' : 'https://roadmap.sh'}/images/og-img.png`; + return fetch(defaultImageUrl).then((response) => response.arrayBuffer()); +} diff --git a/src/pages/og/[slug].ts b/src/pages/og/[slug].ts index 74f3fe2d9..128ec3256 100644 --- a/src/pages/og/[slug].ts +++ b/src/pages/og/[slug].ts @@ -1,4 +1,5 @@ import type { APIRoute } from 'astro'; +import { getDefaultOpenGraphImageBuffer } from '../../lib/open-graph'; export const prerender = false; @@ -10,26 +11,22 @@ export const GET: APIRoute = async (context) => { const { slug } = context.params; if (!slug.startsWith('user-')) { - return new Response( - JSON.stringify({ - error: 'Invalid slug', - }), - { - status: 400, + const buffer = await getDefaultOpenGraphImageBuffer(); + return new Response(buffer, { + headers: { + 'Content-Type': 'image/png', }, - ); + }); } const username = slug.replace('user-', ''); if (!username) { - return new Response( - JSON.stringify({ - error: 'Invalid username', - }), - { - status: 400, + const buffer = await getDefaultOpenGraphImageBuffer(); + return new Response(buffer, { + headers: { + 'Content-Type': 'image/png', }, - ); + }); } const response = await fetch(