home / skills / richtabor / agent-skills / og-images

og-images skill

/skills/og-images

This skill helps you generate OpenGraph and Twitter share images for Next.js by applying best-practice layout, fonts, and metadata.

npx playbooks add skill richtabor/agent-skills --skill og-images

Review the files below or copy the command above to add this skill to your agents.

Files (1)
SKILL.md
4.1 KB
---
name: og-images
description: Guides creation of OpenGraph and Twitter share images using next/og ImageResponse. Covers layout patterns, custom fonts, avatars, title case, and Satori rules. Use when building OG images, Twitter cards, or social previews.
---

# Creating Share Images for Next.js

Generate dynamic OpenGraph (1200x630) and Twitter (1200x600) images using `next/og` ImageResponse.

## Choosing an Approach

- **File-based route** (`app/opengraph-image.tsx`): Best for static pages with known titles at build time. Export `runtime`, `alt`, `size`, `contentType`, and a default `Image` function.
- **API route** (`app/api/og/route.tsx`): Best for dynamic content (blog posts, CMS). Accept `slug` and/or `title` as query params. Reference in metadata via `generateMetadata()`.

Use `export const runtime = "edge"` for both approaches.

## File Naming Convention

| File | Purpose | Dimensions |
|------|---------|------------|
| `opengraph-image.tsx` | Facebook, LinkedIn, iMessage | 1200x630 |
| `twitter-image.tsx` | Twitter/X cards | 1200x600 |
| `app/api/og/route.tsx` | Dynamic API route | 1200x630 |

Place file-based routes in the relevant route directory (e.g., `app/about/opengraph-image.tsx` for `/about`).

## Layout Pattern

- Use `flexDirection: "column"` with `justifyContent: "space-between"` to separate content from branding
- Title and subtitle (e.g., author name) go top-left in a stacked flex column
- Title: large, bold/medium weight, dark color
- Subtitle: same size or smaller, lighter weight, muted color (e.g., `#888`)
- Keep text left-aligned with `textWrap: "balance"` and constrain width with `maxWidth`
- Use `letterSpacing: "-0.02em"` for tight, editorial feel at large sizes
- Padding: `48px` on all sides works well at 1200x630

## Avatar / Logo (Optional)

If the project has an avatar or logo, place it in the bottom-right corner using a flex container with `justifyContent: "flex-end"`. Load images via `fetch` + `arrayBuffer`, convert to base64 data URI for the `src`. Use `borderRadius: "50%"` for circular avatars. Cache loaded assets in a `Map` to avoid refetching.

## Custom Fonts

Load `.ttf` files from `public/fonts/` using `new URL("../../../public/fonts/YourFont.ttf", import.meta.url)`. Pass the `ArrayBuffer` to `ImageResponse` via the `fonts` option. Cache the font buffer after first load. Match the `weight` in the fonts config to the actual font file weight.

## Title Case

If titles come from a CMS, apply smart title case:
- Lowercase small words (a, an, the, and, but, for, in, of, etc.) unless first or last
- Always capitalize brand names correctly (WordPress, JavaScript, GitHub, macOS, etc.)
- Uppercase known acronyms (AI, API, CSS, HTML, UI, UX)
- Handle hyphenated words by capitalizing each part independently

## Metadata Integration

Reference the OG route in `generateMetadata()`:

```tsx
export function generateMetadata({ params }) {
  return {
    openGraph: {
      images: [`/api/og?slug=${params.slug}`],
    },
  };
}
```

For static pages, pass the title directly: `/api/og?title=About`.

## Satori Rules

These are hard requirements of the `next/og` rendering engine (Satori):

1. **Every element needs `display: "flex"`** — this is the only layout mode
2. **Inline styles only** — no CSS classes, no external stylesheets, no CSS variables
3. **All text must be in elements** with explicit style props
4. **Use hex colors** — no `rgb()`, `hsl()`, or CSS variables
5. **No `gap` on older versions** — test before relying on it; fallback to margin

## Common Issues

| Issue | Solution |
|-------|----------|
| Text not rendering | Add `display: "flex"` to the text wrapper |
| Layout broken | Ensure all containers have `display: "flex"` |
| Colors wrong | Use hex colors, not CSS variables |
| Font not loading | Check the relative path from route file to `public/fonts/` |
| Image not showing | Convert to base64 data URI, don't use relative paths |

## Testing

Preview during development by visiting the route directly in the browser:

```
http://localhost:3000/api/og?title=Hello+World
```

After building, verify routes register as dynamic (`f` prefix) in the build output.

Overview

This skill guides creation of dynamic OpenGraph and Twitter share images using Next.js' next/og ImageResponse. It covers layout patterns, asset loading and caching, custom fonts, title-casing rules, and Satori-specific constraints to produce reliable social previews. Use it to build consistent OG 1200x630 and Twitter 1200x600 images for static or dynamic pages.

How this skill works

The skill shows two approaches: file-based image routes for static pages and API routes for dynamic content. It explains how to fetch and cache avatars/logos, load .ttf fonts as ArrayBuffers, and pass them to ImageResponse. It also lists Satori rules and layout patterns so rendered images match social platform expectations and avoid runtime errors.

When to use it

  • Generating OpenGraph images for static pages (file-based route)
  • Generating dynamic social preview images (API route with query params)
  • Creating Twitter/X card images with custom sizing
  • Adding branded avatars/logos or custom fonts to social previews
  • Ensuring Satori-compatible layout and rendering

Best practices

  • Use export const runtime = "edge" for image routes to run on the edge
  • Choose file-based routes for known-at-build-time titles and API routes for CMS-driven content
  • Cache fetched images and font ArrayBuffers in a Map to avoid repeated network/file reads
  • Follow the suggested layout: column flex, space-between, left-aligned title block, bottom-right avatar/logo
  • Load fonts from public/fonts via new URL(...) and pass the ArrayBuffer to ImageResponse fonts option
  • Apply smart title case for CMS content and keep known acronyms/brand capitalization exact

Example use cases

  • Static About page OG image at app/about/opengraph-image.tsx with hardcoded title and local font
  • Blog post preview using app/api/og/route.tsx?slug=post-slug that accepts title and slug query params
  • Twitter card route producing 1200x600 images with circular avatar in bottom-right
  • CMS-driven site where generateMetadata references /api/og?slug=... for dynamic OpenGraph metadata

FAQ

What are the Satori constraints I must follow?

Every element must use display: "flex"; styles must be inline; all text in elements with explicit style props; use hex color codes; avoid gap on older Satori versions.

How do I add custom fonts?

Load .ttf from public/fonts via new URL(...), read it to an ArrayBuffer, cache it, and pass it in the fonts option to ImageResponse with matching weight metadata.