Dynamic OG images let your website create custom preview pictures automatically. This helps your links look nice and unique when shared on social media.
You want each blog post to have its own special preview image.
Your product pages need images showing product details dynamically.
You want to show user-specific info in the preview image when sharing.
You want to update preview images automatically without manual design.
You want to improve click rates by making previews more attractive.
This Next.js edge API route generates a dynamic OG image. It reads a title from the URL query and displays it with styled text on a dark background. The image size matches common social media preview dimensions.
The code gets the 'title' parameter from the URL. If missing, it defaults to 'Default'. Here, the query is '?title=Hello', so title = 'Hello'.
Step 2: Render the image with the title text
The ImageResponse renders a div with the title text centered. Since title is 'Hello', the image will show 'Hello' centered.
Final Answer:
An image with the text 'Hello' centered -> Option B
Quick Check:
title param 'Hello' = text shown [OK]
Hint: Check URL param, fallback used only if missing [OK]
Common Mistakes:
Assuming 'Default' shows even when title is provided
Thinking missing text causes error
Ignoring the URL query parameters
4. Identify the error in this Next.js dynamic OG image code snippet:
import { ImageResponse } from 'next/server';
export async function GET(request) {
const { searchParams } = new URL(request.url);
const name = searchParams.get('name');
return new ImageResponse(
(
<div style={{ fontSize: 40 }}>
Welcome, {name.toUpperCase()}!
</div>
),
{ width: 800, height: 400 }
);
}
medium
A. Calling toUpperCase() on null if 'name' is missing causes an error
B. ImageResponse must be imported from 'next/image' instead of 'next/server'
C. The style object cannot be used inside JSX for ImageResponse
D. The GET function must be synchronous, not async
Solution
Step 1: Check usage of name variable
The code calls name.toUpperCase() without checking if name is null. If the URL has no 'name' parameter, name is null, causing a runtime error.
Step 2: Verify other parts
Import from 'next/server' is correct. Style objects are allowed in JSX for ImageResponse. The GET function can be async.
Final Answer:
Calling toUpperCase() on null if 'name' is missing causes an error -> Option A
Quick Check:
Null check missing before toUpperCase() [OK]
Hint: Always check for null before string methods [OK]
Common Mistakes:
Assuming name is always present
Confusing import source for ImageResponse
Thinking style objects are disallowed in JSX
5. You want to create a dynamic OG image in Next.js that shows a user's name and their score from URL parameters. The score should show 'N/A' if missing or zero. Which code snippet correctly implements this logic?
A:
The score should be shown as a number if positive, otherwise 'N/A' if missing or zero.
Step 2: Analyze each option
Snippet A uses string fallback and compares score === 0 but score is string, so '0' === 0 false and displays '0'. Snippet B converts score to number but uses ?? which only replaces null/undefined, not 0 or NaN, so displays 0 for score=0. Snippet C converts score to number and uses a clear check: if score > 0 show score else 'N/A'. This matches requirement perfectly. Snippet D compares score as string with === 0, but '0' === 0 false, so displays '0'.
Final Answer:
Option C -> Option C
Quick Check:
Number conversion + >0 check = Option C [OK]
Hint: Convert to number and check > 0 for score display [OK]
Common Mistakes:
Not converting score to number before comparison
Using nullish coalescing (??) that ignores zero
Comparing string '0' instead of numeric zero
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.