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