0
0
NextJSframework~10 mins

Dynamic OG images in NextJS - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Dynamic OG images
Request for OG image URL
Next.js API Route receives request
Generate image dynamically using code
Return image as response
Browser or social media fetches image
Image displayed in preview
The flow shows how a request for an OG image triggers Next.js to generate and return a custom image dynamically for social previews.
Execution Sample
NextJS
import { ImageResponse } from '@vercel/og';
export const runtime = 'edge';
export default function handler() {
  return new ImageResponse(
    <div style={{ fontSize: '48px' }}>Hello OG</div>
  );
}
This Next.js API route dynamically creates an OG image with the text 'Hello OG' when requested.
Execution Table
StepActionInput/StateOutput/Result
1Request receivedGET /api/og-imageHandler function triggered
2Render JSX for image<div style={{ fontSize: 48 }}>Hello OG</div>Image canvas created with text
3Generate image bufferCanvas with textPNG image data generated
4Return image responsePNG image dataHTTP response with image
5Browser/social fetchImage URLImage displayed in preview
💡 Image response sent and displayed; request cycle ends.
Variable Tracker
VariableStartAfter Step 2After Step 3Final
requestundefinedGET /api/og-imageGET /api/og-imageRequest handled
imageJSXundefined<div style={{ fontSize: 48 }}>Hello OG</div><div style={{ fontSize: 48 }}>Hello OG</div>Used to create image
imageBufferundefinedundefinedPNG binary dataSent in response
Key Moments - 3 Insights
Why does the handler return an ImageResponse instead of HTML?
Because OG images require an image file, not HTML. The ImageResponse creates an image from JSX to send as a PNG, as shown in execution_table step 3.
How does Next.js know to run this code on the edge?
The export 'runtime = edge' tells Next.js to run the handler as an edge function, enabling fast image generation (see execution_sample).
What triggers the image generation process?
A request to the API route URL triggers the handler, starting the flow at execution_table step 1.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the output after step 3?
APNG image data generated
BHTTP response with image
CImage displayed in preview
DHandler function triggered
💡 Hint
Check the 'Output/Result' column for step 3 in execution_table.
At which step does the browser fetch and display the image?
AStep 2
BStep 3
CStep 5
DStep 1
💡 Hint
Look for 'Image displayed in preview' in execution_table.
If the 'runtime' export is removed, what changes in the process?
ARequest will fail immediately
BImage generation runs on the server, not edge
CImageResponse will not work
DImage will be cached forever
💡 Hint
Refer to key_moments about 'runtime = edge' and its effect.
Concept Snapshot
Dynamic OG images in Next.js:
- Use API routes to generate images on demand
- Return ImageResponse with JSX for image content
- Export 'runtime = edge' for fast edge execution
- Social sites fetch the image URL to show previews
- Enables custom, dynamic social preview images
Full Transcript
Dynamic OG images in Next.js work by creating an API route that generates an image dynamically when requested. The handler uses the ImageResponse class to convert JSX into an image. The export 'runtime = edge' ensures the code runs at the edge for speed. When a social media site or browser requests the image URL, Next.js runs the handler, generates the image, and returns it as a PNG. This image is then displayed in social previews, allowing dynamic and customized visuals for links.