# Image Metadata Files

> Generating social cards dynamically with JSX.

Subject: Next.js | Lesson 57 of 65 | 3 min read
Interactive lesson: https://codewithanimation.in/nextjs/image-metadata-files
Previous: [Dynamic SEO Files](https://codewithanimation.in/nextjs/dynamic-seo-files.md) | Next: [Middleware](https://codewithanimation.in/nextjs/middleware.md)

## Dynamic OG Images

When someone pastes a link to your site in iMessage or Twitter, it unfurls into a large preview card. This is powered by Open Graph (OG) images.

Instead of using a single static image, Next.js allows you to create an `opengraph-image.tsx` file. Inside this file, you write standard React/JSX to design the image, and Next.js compiles it into a `.png` on the fly.

## Automated Marketing

For dynamic routes like `/blog/[slug]`, a single static OG image looks generic.

By using `opengraph-image.tsx`, you can intercept the `slug` parameter, fetch the specific article's title and author, and render them directly onto the image.

This massively increases click-through rates on social media without requiring a designer to manually create thousands of images.

## Implementation

```tsx
import { ImageResponse } from 'next/og';

export default async function Image({ params }) {
  const post = await fetchPost(params.slug);
  
  return new ImageResponse(
    (
      <div style={{ display: 'flex', background: 'blue', color: 'white' }}>
        <h1>{post.title}</h1>
      </div>
    ),
    { width: 1200, height: 630 }
  );
}
```

## Common mistake: The Satori CSS Trap

Under the hood, Next.js uses a Vercel library called Satori to convert your HTML/CSS into an image.

Satori does **NOT** run in a real browser. It only supports a strict subset of CSS.

If you try to use `display: grid`, `gap: 1rem`, `calc()`, or modern CSS features, the image generation will silently crash or render incorrectly. You must stick to basic flexbox and absolute positioning.

## Try it: JSX to Image Pipeline

Watch the compilation pipeline take a JSX component and render it into a physical 1200x630 Open Graph image.

The animation is on the lesson page and is not part of this text version: https://codewithanimation.in/nextjs/image-metadata-files

## Check yourself

### Question 1

Why can't you use CSS Grid or `gap` inside `opengraph-image.tsx`?

- A. Because the Edge runtime doesn't support them.
- B. Because social media platforms don't support modern CSS.
- C. Because the Satori engine that converts HTML to SVG only supports a limited subset of CSS.
- D. Because Tailwind is not allowed in metadata files.

Correct answer: C

Satori is the engine that actually draws the image. It is essentially a custom layout engine written from scratch, and it currently only supports a highly restricted subset of flexbox.

- Why not A: The runtime supports CSS, but the Satori conversion engine does not.
- Why not B: The platform receives a compiled PNG image, not CSS.
- Why not D: Tailwind works fine, but only if the underlying utility classes map to supported CSS.

## Remember this

- `opengraph-image.tsx` and `twitter-image.tsx` generate dynamic `.png` images.
- You write the design using standard React JSX and `ImageResponse`.
- You can access URL `params` to fetch dynamic data for the image.
- The CSS engine (Satori) is highly restricted. Avoid Grid, calc(), and gap.
