|
| 1 | +import { ImageResponse } from 'next/og' |
| 2 | +import type { NextRequest } from 'next/server' |
| 3 | + |
| 4 | +export const runtime = 'edge' |
| 5 | + |
| 6 | +const TITLE_FONT_SIZE = { |
| 7 | + large: 64, |
| 8 | + medium: 56, |
| 9 | + small: 48, |
| 10 | +} as const |
| 11 | + |
| 12 | +function getTitleFontSize(title: string): number { |
| 13 | + if (title.length > 45) return TITLE_FONT_SIZE.small |
| 14 | + if (title.length > 30) return TITLE_FONT_SIZE.medium |
| 15 | + return TITLE_FONT_SIZE.large |
| 16 | +} |
| 17 | + |
| 18 | +/** |
| 19 | + * Loads a Google Font dynamically by fetching the CSS and extracting the font URL. |
| 20 | + */ |
| 21 | +async function loadGoogleFont(font: string, weights: string, text: string): Promise<ArrayBuffer> { |
| 22 | + const url = `https://fonts.googleapis.com/css2?family=${font}:wght@${weights}&text=${encodeURIComponent(text)}` |
| 23 | + const css = await (await fetch(url)).text() |
| 24 | + const resource = css.match(/src: url\((.+)\) format\('(opentype|truetype)'\)/) |
| 25 | + |
| 26 | + if (resource) { |
| 27 | + const response = await fetch(resource[1]) |
| 28 | + if (response.status === 200) { |
| 29 | + return await response.arrayBuffer() |
| 30 | + } |
| 31 | + } |
| 32 | + |
| 33 | + throw new Error('Failed to load font data') |
| 34 | +} |
| 35 | + |
| 36 | +/** |
| 37 | + * Generates dynamic Open Graph images for documentation pages. |
| 38 | + */ |
| 39 | +export async function GET(request: NextRequest) { |
| 40 | + const { searchParams } = new URL(request.url) |
| 41 | + const title = searchParams.get('title') || 'Documentation' |
| 42 | + const category = searchParams.get('category') || 'DOCUMENTATION' |
| 43 | + const description = searchParams.get('description') || '' |
| 44 | + |
| 45 | + const baseUrl = new URL(request.url).origin |
| 46 | + const backgroundImageUrl = `${baseUrl}/static/og-background.png` |
| 47 | + |
| 48 | + const allText = `${title}${category}${description}docs.sim.ai` |
| 49 | + const fontData = await loadGoogleFont('Geist', '400;500;600', allText) |
| 50 | + |
| 51 | + return new ImageResponse( |
| 52 | + <div |
| 53 | + style={{ |
| 54 | + height: '100%', |
| 55 | + width: '100%', |
| 56 | + display: 'flex', |
| 57 | + flexDirection: 'column', |
| 58 | + background: 'linear-gradient(315deg, #1e1e3f 0%, #1a1a2e 40%, #0f0f0f 100%)', |
| 59 | + position: 'relative', |
| 60 | + fontFamily: 'Geist', |
| 61 | + }} |
| 62 | + > |
| 63 | + {/* Background texture */} |
| 64 | + <img |
| 65 | + src={backgroundImageUrl} |
| 66 | + alt='' |
| 67 | + style={{ |
| 68 | + position: 'absolute', |
| 69 | + top: 0, |
| 70 | + left: 0, |
| 71 | + width: '100%', |
| 72 | + height: '100%', |
| 73 | + objectFit: 'cover', |
| 74 | + opacity: 0.04, |
| 75 | + }} |
| 76 | + /> |
| 77 | + |
| 78 | + {/* Subtle purple glow from bottom right */} |
| 79 | + <div |
| 80 | + style={{ |
| 81 | + position: 'absolute', |
| 82 | + bottom: 0, |
| 83 | + right: 0, |
| 84 | + width: '50%', |
| 85 | + height: '100%', |
| 86 | + background: |
| 87 | + 'radial-gradient(ellipse at bottom right, rgba(112, 31, 252, 0.1) 0%, transparent 50%)', |
| 88 | + display: 'flex', |
| 89 | + }} |
| 90 | + /> |
| 91 | + |
| 92 | + {/* Content */} |
| 93 | + <div |
| 94 | + style={{ |
| 95 | + display: 'flex', |
| 96 | + flexDirection: 'column', |
| 97 | + padding: '56px 72px', |
| 98 | + height: '100%', |
| 99 | + justifyContent: 'space-between', |
| 100 | + }} |
| 101 | + > |
| 102 | + {/* Logo */} |
| 103 | + <img src={`${baseUrl}/static/logo.png`} alt='sim' height={32} /> |
| 104 | + |
| 105 | + {/* Category + Title + Description */} |
| 106 | + <div |
| 107 | + style={{ |
| 108 | + display: 'flex', |
| 109 | + flexDirection: 'column', |
| 110 | + gap: 12, |
| 111 | + }} |
| 112 | + > |
| 113 | + <span |
| 114 | + style={{ |
| 115 | + fontSize: 15, |
| 116 | + fontWeight: 600, |
| 117 | + color: '#802fff', |
| 118 | + letterSpacing: '0.02em', |
| 119 | + }} |
| 120 | + > |
| 121 | + {category} |
| 122 | + </span> |
| 123 | + <span |
| 124 | + style={{ |
| 125 | + fontSize: getTitleFontSize(title), |
| 126 | + fontWeight: 600, |
| 127 | + color: '#ffffff', |
| 128 | + lineHeight: 1.1, |
| 129 | + letterSpacing: '-0.02em', |
| 130 | + }} |
| 131 | + > |
| 132 | + {title} |
| 133 | + </span> |
| 134 | + {description && ( |
| 135 | + <span |
| 136 | + style={{ |
| 137 | + fontSize: 18, |
| 138 | + fontWeight: 400, |
| 139 | + color: '#a1a1aa', |
| 140 | + lineHeight: 1.4, |
| 141 | + marginTop: 4, |
| 142 | + }} |
| 143 | + > |
| 144 | + {description.length > 100 ? `${description.slice(0, 100)}...` : description} |
| 145 | + </span> |
| 146 | + )} |
| 147 | + </div> |
| 148 | + |
| 149 | + {/* Footer */} |
| 150 | + <span |
| 151 | + style={{ |
| 152 | + fontSize: 15, |
| 153 | + fontWeight: 500, |
| 154 | + color: '#52525b', |
| 155 | + }} |
| 156 | + > |
| 157 | + docs.sim.ai |
| 158 | + </span> |
| 159 | + </div> |
| 160 | + </div>, |
| 161 | + { |
| 162 | + width: 1200, |
| 163 | + height: 630, |
| 164 | + fonts: [ |
| 165 | + { |
| 166 | + name: 'Geist', |
| 167 | + data: fontData, |
| 168 | + style: 'normal', |
| 169 | + }, |
| 170 | + ], |
| 171 | + } |
| 172 | + ) |
| 173 | +} |
0 commit comments