diff --git a/src/api/api.ts b/src/api/api.ts index 8917e95be..911a2a9f2 100644 --- a/src/api/api.ts +++ b/src/api/api.ts @@ -1,4 +1,3 @@ -import Cookies from 'js-cookie'; import { TOKEN_COOKIE_NAME } from '../lib/jwt.ts'; import type { APIContext } from 'astro'; diff --git a/src/pages/og/[slug].ts b/src/pages/og/[slug].ts new file mode 100644 index 000000000..74f3fe2d9 --- /dev/null +++ b/src/pages/og/[slug].ts @@ -0,0 +1,45 @@ +import type { APIRoute } from 'astro'; + +export const prerender = false; + +type Params = { + slug: string; +}; + +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 username = slug.replace('user-', ''); + if (!username) { + return new Response( + JSON.stringify({ + error: 'Invalid username', + }), + { + status: 400, + }, + ); + } + + const response = await fetch( + `${import.meta.env.PUBLIC_API_URL}/v1-profile-open-graph/${username}`, + ); + + const svg = await response.text(); + return new Response(svg, { + headers: { + 'Content-Type': 'image/svg+xml', + }, + }); +}; diff --git a/src/pages/u/[username].astro b/src/pages/u/[username].astro index 2c2004668..1b414b715 100644 --- a/src/pages/u/[username].astro +++ b/src/pages/u/[username].astro @@ -23,7 +23,8 @@ if (error || !userDetails) { errorMessage = error?.message || 'User not found'; } -const ogImage = `${import.meta.env.PUBLIC_API_URL}/v1-profile-open-graph/${username}`; +const origin = Astro.url.origin; +const ogImage = `${origin}/og/user-${username}`; ---