|
| 1 | +import { ImageResponse } from "next/og" |
| 2 | +import { NextRequest } from "next/server" |
| 3 | + |
| 4 | +export const runtime = "edge" |
| 5 | + |
| 6 | +async function fetchWithTimeout(url: string, init?: RequestInit, timeoutMs = 3000) { |
| 7 | + const controller = new AbortController() |
| 8 | + const id = setTimeout(() => controller.abort(), timeoutMs) |
| 9 | + try { |
| 10 | + return await fetch(url, { ...init, signal: controller.signal }) |
| 11 | + } finally { |
| 12 | + clearTimeout(id) |
| 13 | + } |
| 14 | +} |
| 15 | + |
| 16 | +async function loadGoogleFont(font: string, text: string): Promise<ArrayBuffer | null> { |
| 17 | + try { |
| 18 | + const url = `https://fonts.googleapis.com/css2?family=${font}&text=${encodeURIComponent(text)}` |
| 19 | + const cssRes = await fetchWithTimeout(url) |
| 20 | + if (!cssRes.ok) return null |
| 21 | + const css = await cssRes.text() |
| 22 | + |
| 23 | + const match = |
| 24 | + css.match(/src:\s*url\(([^)]+)\)\s*format\('(?:woff2|woff|opentype|truetype)'\)/i) || |
| 25 | + css.match(/url\(([^)]+)\)/i) |
| 26 | + |
| 27 | + const fontUrl = match && match[1] ? match[1].replace(/^['"]|['"]$/g, "") : null |
| 28 | + if (!fontUrl) return null |
| 29 | + |
| 30 | + const res = await fetchWithTimeout(fontUrl, undefined, 5000) |
| 31 | + if (!res.ok) return null |
| 32 | + return await res.arrayBuffer() |
| 33 | + } catch { |
| 34 | + return null |
| 35 | + } |
| 36 | +} |
| 37 | + |
| 38 | +export async function GET(request: NextRequest) { |
| 39 | + const requestUrl = new URL(request.url) |
| 40 | + const { searchParams } = requestUrl |
| 41 | + |
| 42 | + // Get title and description from query params |
| 43 | + const title = searchParams.get("title") || "Roo Code" |
| 44 | + const description = searchParams.get("description") || "" |
| 45 | + |
| 46 | + // Combine all text that will be displayed for font loading |
| 47 | + const displayText = title + description |
| 48 | + |
| 49 | + // Check if we should try to use the background image |
| 50 | + const useBackgroundImage = searchParams.get("bg") !== "false" |
| 51 | + |
| 52 | + // Dynamically get the base URL from the current request |
| 53 | + // This ensures it works correctly in development, preview, and production environments |
| 54 | + const baseUrl = `${requestUrl.protocol}//${requestUrl.host}` |
| 55 | + const variant = title.length % 2 === 0 ? "a" : "b" |
| 56 | + const backgroundUrl = `${baseUrl}/og/base_${variant}.png` |
| 57 | + |
| 58 | + // Preload fonts with graceful fallbacks |
| 59 | + const regularFont = await loadGoogleFont("Inter", displayText) |
| 60 | + const boldFont = await loadGoogleFont("Inter:wght@700", displayText) |
| 61 | + const fonts: { name: string; data: ArrayBuffer; style?: "normal" | "italic"; weight?: 400 | 700 }[] = [] |
| 62 | + if (regularFont) { |
| 63 | + fonts.push({ name: "Inter", data: regularFont, style: "normal", weight: 400 }) |
| 64 | + } |
| 65 | + if (boldFont) { |
| 66 | + fonts.push({ name: "Inter", data: boldFont, style: "normal", weight: 700 }) |
| 67 | + } |
| 68 | + |
| 69 | + return new ImageResponse( |
| 70 | + ( |
| 71 | + <div |
| 72 | + style={{ |
| 73 | + width: "100%", |
| 74 | + height: "100%", |
| 75 | + display: "flex", |
| 76 | + position: "relative", |
| 77 | + // Use gradient background as default/fallback |
| 78 | + background: "linear-gradient(135deg, #1e3a5f 0%, #0f1922 50%, #1a2332 100%)", |
| 79 | + }}> |
| 80 | + {/* Optional Background Image - only render if explicitly requested */} |
| 81 | + {useBackgroundImage && ( |
| 82 | + <div |
| 83 | + style={{ |
| 84 | + position: "absolute", |
| 85 | + top: 0, |
| 86 | + left: 0, |
| 87 | + width: "100%", |
| 88 | + height: "100%", |
| 89 | + display: "flex", |
| 90 | + }}> |
| 91 | + {/* eslint-disable-next-line @next/next/no-img-element */} |
| 92 | + <img |
| 93 | + src={backgroundUrl} |
| 94 | + alt="" |
| 95 | + width={1200} |
| 96 | + height={630} |
| 97 | + style={{ |
| 98 | + width: "100%", |
| 99 | + height: "100%", |
| 100 | + objectFit: "cover", |
| 101 | + }} |
| 102 | + /> |
| 103 | + </div> |
| 104 | + )} |
| 105 | + |
| 106 | + {/* Text Content */} |
| 107 | + <div |
| 108 | + style={{ |
| 109 | + position: "absolute", |
| 110 | + display: "flex", |
| 111 | + flexDirection: "column", |
| 112 | + justifyContent: "flex-end", |
| 113 | + top: "220px", |
| 114 | + left: "80px", |
| 115 | + right: "80px", |
| 116 | + bottom: "80px", |
| 117 | + }}> |
| 118 | + {/* Main Title */} |
| 119 | + <h1 |
| 120 | + style={{ |
| 121 | + fontSize: 70, |
| 122 | + fontWeight: 700, |
| 123 | + fontFamily: "Inter, Helvetica Neue, Helvetica, sans-serif", |
| 124 | + color: "white", |
| 125 | + lineHeight: 1.2, |
| 126 | + margin: 0, |
| 127 | + maxHeight: "2.4em", |
| 128 | + overflow: "hidden", |
| 129 | + }}> |
| 130 | + {title} |
| 131 | + </h1> |
| 132 | + |
| 133 | + {/* Secondary Description */} |
| 134 | + {description && ( |
| 135 | + <h2 |
| 136 | + style={{ |
| 137 | + fontSize: 70, |
| 138 | + fontWeight: 400, |
| 139 | + fontFamily: "Inter, Helvetica Neue, Helvetica, Arial, sans-serif", |
| 140 | + color: "rgba(255, 255, 255, 0.9)", |
| 141 | + lineHeight: 1.2, |
| 142 | + margin: 0, |
| 143 | + maxHeight: "2.4em", |
| 144 | + overflow: "hidden", |
| 145 | + }}> |
| 146 | + {description} |
| 147 | + </h2> |
| 148 | + )} |
| 149 | + </div> |
| 150 | + </div> |
| 151 | + ), |
| 152 | + { |
| 153 | + width: 1200, |
| 154 | + height: 630, |
| 155 | + fonts: fonts.length ? fonts : undefined, |
| 156 | + // Cache for 7 days in production, 3 seconds in development |
| 157 | + headers: { |
| 158 | + "Cache-Control": |
| 159 | + process.env.NODE_ENV === "production" |
| 160 | + ? "public, max-age=604800, s-maxage=604800, stale-while-revalidate=86400" |
| 161 | + : "public, max-age=3, s-maxage=3", |
| 162 | + }, |
| 163 | + }, |
| 164 | + ) |
| 165 | +} |
0 commit comments