Deep Dive: Rendering Real-Time SVG Cohort & Stack Badges Dynamically in Next.js App Router
Server-Rendered SVGs with Zero Client Hydration Dynamic badges on GitHub READMEs (e.g., shields.io style) face strict caching and sanitization constraints. GitHub's camo proxy aggressively caches images and strips unsafe scripts or stylesheets. In DevCastle, we built /api/badges/[slug]/cohort.svg and /api/badges/[slug]/stack.svg using pure Next.js Route Handlers that output clean XML vectors with precise geometry, embedded web-safe monospace typography, and appropriate cache headers. Notice the Cache-Control header: GitHub Camo can cache for 1 hour while stale-while-revalidate guarantees instant delivery without hitting our database on every README view. Have you built dynamic SVG generators or edge banners in Next.js? How do you handle cache invalidation when live stats change?
The stale-while-revalidate header configuration is spot on for GitHub Camo. Keeps database load at practically zero even if a repo gets 50k views on Hacker News.