Dynamische Open Graph Bilder im Next.js 13 App Router generieren
Ein serverseitiger OG Image Generator und passende Meta Tags sorgen dafür, dass geteilte Links der Caribe Business & Wellness Academy eine passende Vorschau anzeigen. Die Änderung liegt in src/app/opengraph-image.tsx.

TL;DR: Ich habe einen serverseitigen OG-Bildgenerator und passende Meta-Tags hinzugefügt, sodass jeder geteilte Link der Caribe Business & Wellness Academy eine umfangreiche Vorschau anzeigt. Die Änderung liegt in src/app/opengraph-image.tsx, aktualisiert src/app/layout.tsx und erweitert next.config.ts.
Unsere Landingpages wurden in Slack, Teams und LinkedIn mit einer generischen Vorschau geteilt: nur die URL und ein unscheinbares Favicon. Die Community wünschte sich eine informativere Card, die das Branding der Akademie und den aktuellen Titel des jeweiligen Angebots zeigt. Das Fehlen von Open-Graph-Metadaten führte zu folgenden Symptomen:
Request URL: https://caribe-academy.vercel.app/ og:image: missing og:title: "missing" Enter fullscreen mode Exit fullscreen mode Ohne og:image und og:title griffen die sozialen Plattformen auf das Standard-Thumbnail von Chrome zurück, was unprofessionell wirkt und die Klickraten senkt.
Mein erster Versuch war der klassische Ansatz mit statischen Meta-Tags: Ich fügte hartkodierte <meta property="og:image"...>-Tags in src/app/layout.tsx ein. Das funktionierte für die Startseite, aber jedes neue Angebot (z. B. „AI Productivity“) erforderte eine manuelle Anpassung, und die Bildgröße passte nicht für mobile Vorschauen. Ich probierte außerdem einen Drittanbieter-Dienst (https://og-image.vercel.app), indem ich eine URL mit Query-Parametern konstruierte, aber der Dienst drosselte unseren Traffic während einer Demo und fügte Latenz hinzu.
Ergebnis: statische Tags gleich Wartungsalptraum; externer Dienst gleich unzuverlässig.
Der App Router von Next.js 13 erlaubt es, eine ImageResponse aus einem Routensegment zurückzugeben. Ich erstellte eine neue Routendatei:
import { readFile } from "node:fs/promises"; import { join } from "node:path"; import { ImageResponse } from "next/og"; // Load custom fonts (stored in src/app/_og-fonts) const fraunces = readFile ( join ( process. cwd ( ), "src/app/_og-fonts/fraunces-latin-500-normal.woff" ) ); const inter = readFile ( join ( process. cwd ( ), "src/app/_og-fonts/inter-latin-500-normal.woff" ) ); export const runtime = "edge"; export async function GET ( request: Request ) { const { searchParams } = new URL ( request. url ); const title = searchParams. get ( "title" )?? "Caribe Academy"; // Load fonts in parallel const [ frauncesData, interData ] = await Promise. all ( [ fraunces, inter ] ); return new ImageResponse ( ( < div style = { { height: "100%", width: "100%", background: "#1E1E1E", color: "#F5F5F5", display: "flex", flexDirection: "column", justifyContent: "center", alignItems: "center", padding: "4rem", } } > < h1 style = { { fontFamily: "Fraunces", fontSize: "72px", margin: 0, } } > { title } </ h1 > < p style = { { fontFamily: "Inter", fontSize: "36px", marginTop: "1rem", } } > Learn, Build, Grow. </ p > </ div > ), { width: 1200, height: 630, fonts: [ { name: "Fraunces", data: frauncesData, style: "normal", weight: 500, }, { name: "Inter", data: interData, style: "normal", weight: 500, }, ], } ); } Enter fullscreen mode Exit fullscreen mode Kernpunkte:
Ich aktualisierte das Root-Layout so, dass es die Bild-URL anhand des Titels der aktuellen Route berechnet und die passenden Meta-Tags einfügt.
import "./globals.css"; import { Inter } from "next/font/google"; const inter = Inter ( { subsets: [ "latin" ], display: "swap" } ); const SITE_URL = process. env. NEXT_PUBLIC_SITE_URL?? "https://caribe-academy.vercel.app"; const SHARE_TITLE = "Caribe Business and Wellness Academy"; export default function RootLayout ( { children, }: { children: React.
ReactNode; } ) { // Example: derive title from route (simplified) const pageTitle = process. env. NEXT_PUBLIC_PAGE_TITLE?? SHARE_TITLE; const ogImageUrl = `${SITE_URL}/opengraph-image?title=${encodeURIComponent( pageTitle )}`; return ( < html lang = "en" > < head > < meta property = "og:title" content = { pageTitle } /> < meta property = "og:description" content = "AI‑powered labs, VIP access, and more." /> < meta property = "og:image" content = { ogImageUrl } /> < meta property = "og:url" content = { SITE_URL } /> < meta name = "twitter:card" content = "summary_large_image" /> </ head > < body className = { inter. className } > { children } </ body > </ html > ); } Enter fullscreen mode Exit fullscreen mode NEXT_PUBLIC_SITE_URL steht in.env.example und ist für Vercel-Deployments erforderlich.
Das Layout baut die OG-URL nun bei jedem Request neu auf, wodurch die Vorschau stets den Titel der aktuellen Seite widerspiegelt. 3. Schriftarten zum Build hinzufügen Die für das Bild benötigten Schriftarten sind binäre Assets, also habe ich sie ins Repository aufgenommen und Next.js angewiesen, sie in das Edge-Bundle einzubinden.
import type { NextConfig } from "next"; const nextConfig: NextConfig = { // Existing config… outputFileTracingIncludes: { "/vip/**/*": [ "./curriculum/**/*.md" ], // Include OG fonts for edge runtime "/opengraph-image": [ "src/app/_og-fonts/**/*.woff" ], }, }; export default nextConfig; Enter fullscreen mode Exit fullscreen mode Jetzt kann die Edge-Funktion die Schriftarten per readFile laden, ohne einen „file not found“-Fehler zu verursachen.
Ich fügte in ARCHITECTURE.md eine kurze Notiz hinzu, die den OG-Ablauf beschreibt und ein Anwendungsbeispiel für künftige Mitwirkende enthält.
## Open Graph Image Generation - Route: `/opengraph-image` - Query param: `title` – the headline to render. - Fonts: stored under `src/app/_og-fonts`. - Edge runtime ensures sub‑100 ms response. Enter fullscreen mode Exit fullscreen mode Wichtigste Erkenntnis Dynamische OG-Bilder gehören in die Edge-Schicht, nicht in statische Assets. Durch die Nutzung von ImageResponse aus Next.js 13 und das lokale Halten der Schriftarten erhält man eine schnelle, anpassbare Vorschau, die mit neuen Seiten mitwächst, ohne dass Meta-Tags ständig von Hand gepflegt werden müssen.
Roberto Luna Osorio – Full Stack Developer & Project Lead Playa del Carmen, México
Tags: #vibecoding #buildinpublic #nextjs #react #typescript #opengraph #docker #linux
Teil meiner Build-in-Public-Serie, in der ich den echten Prozess des Baus von SaaS-Projekten aus Playa del Carmen, México teile.
Repo: zaerohell/ai-business-academy · 2026-11-30
Automatisch übersetzt. Das Original auf Englisch ist unten aufklappbar.
Original (Englisch) anzeigen
TL;DR: I added a server‑side OG image generator and proper meta tags so every shared link from Caribe Business & Wellness Academy renders a rich preview. The change lives in src/app/opengraph-image.tsx, updates src/app/layout.tsx, and tweaks next.config.ts.
Our landing pages were being shared on Slack, Teams, and LinkedIn with a generic preview: just the URL and a bland favicon. The community asked for a richer card that displayed the academy’s branding and the current pillar title. The lack of Open Graph metadata caused the following symptoms:
Request URL: https://caribe-academy.vercel.app/ og:image: missing og:title: "missing" Enter fullscreen mode Exit fullscreen mode Without og:image and og:title, social platforms fell back to the default Chrome thumbnail, which looks unprofessional and reduces click‑through rates.
My first attempt was the classic “static meta tags” approach: I added hard‑coded <meta property="og:image"...> tags to src/app/layout.tsx. It worked for the home page, but every new pillar (e.g., “AI Productivity”) required a manual edit, and the image size was off for mobile previews. I also tried using a third‑party service ( https://og-image.vercel.app ) by constructing a URL with query parameters, but the service throttled our traffic during a demo and added latency.
Result: static tags = maintenance nightmare; external service = unreliable.
Next.js 13’s App Router lets us return an ImageResponse from a route segment. I created a new route file:
import { readFile } from "node:fs/promises"; import { join } from "node:path"; import { ImageResponse } from "next/og"; // Load custom fonts (stored in src/app/_og-fonts) const fraunces = readFile ( join ( process. cwd ( ), "src/app/_og-fonts/fraunces-latin-500-normal.woff" ) ); const inter = readFile ( join ( process. cwd ( ), "src/app/_og-fonts/inter-latin-500-normal.woff" ) ); export const runtime = "edge"; export async function GET ( request: Request ) { const { searchParams } = new URL ( request. url ); const title = searchParams. get ( "title" )?? "Caribe Academy"; // Load fonts in parallel const [ frauncesData, interData ] = await Promise. all ( [ fraunces, inter ] ); return new ImageResponse ( ( < div style = { { height: "100%", width: "100%", background: "#1E1E1E", color: "#F5F5F5", display: "flex", flexDirection: "column", justifyContent: "center", alignItems: "center", padding: "4rem", } } > < h1 style = { { fontFamily: "Fraunces", fontSize: "72px", margin: 0, } } > { title } </ h1 > < p style = { { fontFamily: "Inter", fontSize: "36px", marginTop: "1rem", } } > Learn, Build, Grow. </ p > </ div > ), { width: 1200, height: 630, fonts: [ { name: "Fraunces", data: frauncesData, style: "normal", weight: 500, }, { name: "Inter", data: interData, style: "normal", weight: 500, }, ], } ); } Enter fullscreen mode Exit fullscreen mode Key points:
I updated the root layout to compute the image URL based on the current route’s title and inject the proper meta tags.
import "./globals.css"; import { Inter } from "next/font/google"; const inter = Inter ( { subsets: [ "latin" ], display: "swap" } ); const SITE_URL = process. env. NEXT_PUBLIC_SITE_URL?? "https://caribe-academy.vercel.app"; const SHARE_TITLE = "Caribe Business and Wellness Academy"; export default function RootLayout ( { children, }: { children: React.
ReactNode; } ) { // Example: derive title from route (simplified) const pageTitle = process. env. NEXT_PUBLIC_PAGE_TITLE?? SHARE_TITLE; const ogImageUrl = `${SITE_URL}/opengraph-image?title=${encodeURIComponent( pageTitle )}`; return ( < html lang = "en" > < head > < meta property = "og:title" content = { pageTitle } /> < meta property = "og:description" content = "AI‑powered labs, VIP access, and more." /> < meta property = "og:image" content = { ogImageUrl } /> < meta property = "og:url" content = { SITE_URL } /> < meta name = "twitter:card" content = "summary_large_image" /> </ head > < body className = { inter. className } > { children } </ body > </ html > ); } Enter fullscreen mode Exit fullscreen mode NEXT_PUBLIC_SITE_URL lives in.env.example and is required for Vercel deployments.
The layout now builds the OG URL on every request, ensuring the preview reflects the current page’s title. 3. Add Fonts to the Build The fonts needed for the image are binary assets, so I added them to the repo and told Next.js to include them in the edge bundle.
import type { NextConfig } from "next"; const nextConfig: NextConfig = { // Existing config… outputFileTracingIncludes: { "/vip/**/*": [ "./curriculum/**/*.md" ], // Include OG fonts for edge runtime "/opengraph-image": [ "src/app/_og-fonts/**/*.woff" ], }, }; export default nextConfig; Enter fullscreen mode Exit fullscreen mode Now the edge function can readFile the fonts without a “file not found” error.
I added a short note in ARCHITECTURE.md describing the OG flow and a usage example for future contributors.
## Open Graph Image Generation - Route: `/opengraph-image` - Query param: `title` – the headline to render. - Fonts: stored under `src/app/_og-fonts`. - Edge runtime ensures sub‑100 ms response. Enter fullscreen mode Exit fullscreen mode Key Takeaway Dynamic OG images belong in the edge layer, not as static assets. By leveraging Next.js 13’s ImageResponse and keeping fonts local, you get a fast, customizable preview that scales with new pages without any manual meta‑tag churn.
Roberto Luna Osorio – Full Stack Developer & Project Lead Playa del Carmen, México
Tags: #vibecoding #buildinpublic #nextjs #react #typescript #opengraph #docker #linux
Part of my Build in Public series — sharing the real process of building SaaS projects from Playa del Carmen, México.
Repo: zaerohell/ai-business-academy · 2026-11-30