Generating Dynamic Open Graph Images in a Next.js 13 App Router (ai‑business‑academy)

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.

The Problem

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" 

Without og:image and og:title, social platforms fell back to the default Chrome thumbnail, which looks unprofessional and reduces click‑through rates.

What I Tried First

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.

The Implementation

1. Enable Dynamic OG Image Generation

Next.js 13’s App Router lets us return an ImageResponse from a route segment. I created a new route file:

File: src/app/opengraph-image.tsx

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, }, ], } ); } 

Key points:

  • Edge runtime – the function runs at the CDN edge, keeping latency sub‑100 ms.
  • Custom fonts – stored locally under src/app/_og-fonts/ to avoid external font fetches.
  • Query param title – the page that requests the OG image can pass any headline, making the generator reusable.

2. Wire the OG Image into the Layout

I updated the root layout to compute the image URL based on the current route’s title and inject the proper meta tags.

File: src/app/layout.tsx

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> ); } 
  • 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.

File: next.config.ts

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; 

Now the edge function can readFile the fonts without a “file not found” error.

4. Update Documentation

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. 

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.

What's Next

  • Cache OG responses with Cache-Control: s-maxage=86400 to reduce edge compute cost.
  • Add dynamic data (e.g., instructor name, course date) by pulling from the DB in the GET handler.
  • Unit‑test the route with @vercel/edge-runtime mocks to guarantee the image renders correctly after future refactors.

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

#playadev #buildinpublic