From 7dd70152fa052611ed8f9ca93c54831611575eba Mon Sep 17 00:00:00 2001 From: arcabotai <261107832+arcabotai@users.noreply.github.com> Date: Mon, 22 Jun 2026 08:37:34 -0400 Subject: [PATCH] fix(og): fall back to default pfp when a cast avatar is missing/invalid The cast OG/share-preview image fetched the author avatar with `new URL(avatar, import.meta.url)` and fed it straight to @vercel/og, so a missing/null avatar, an unreachable URL, or a non-image response 500'd the whole image. (Previously masked by the `superlogo` ReferenceError fixed in #17, which 500'd every cast OG regardless.) Now: only fetch http(s) avatars, require an image content-type, and fall back to public/user.png on any failure. Verified: typecheck + build green; cast OG returns 200 for valid avatars, and the missing/invalid-avatar case no longer 500s. --- src/app/api/og/route.tsx | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/src/app/api/og/route.tsx b/src/app/api/og/route.tsx index 57c2c36..f50167c 100644 --- a/src/app/api/og/route.tsx +++ b/src/app/api/og/route.tsx @@ -43,7 +43,24 @@ export async function GET(req: NextRequest) { if (type === 'cast') { const textLines = truncateLongWord(text, 80)?.split(/\\n|\n/) || ['']; - const avatarImage = await fetch(new URL(avatar, import.meta.url)).then((res) => res.arrayBuffer()); + // Load the author avatar defensively — a missing, unreachable, or non-image + // avatar must not 500 the whole share-preview image. Fall back to the default + // pfp. + const loadAvatar = async (): Promise => { + if (avatar && /^https?:\/\//.test(avatar)) { + try { + const res = await fetch(avatar); + const contentType = res.headers.get("content-type") || ""; + if (res.ok && contentType.startsWith("image/")) { + return await res.arrayBuffer(); + } + } catch (e) { + // fall through to the default avatar + } + } + return fetch(new URL("../../../../public/user.png", import.meta.url)).then((res) => res.arrayBuffer()); + }; + const avatarImage = await loadAvatar(); const farcasterLogo = await fetch( new URL("../../../../public/farcaster.png", import.meta.url)