Consider this Next.js API route that generates a dynamic OG image using the @vercel/og library. What will be the output image's text content if the query parameter ?title=Hello%20World is passed?
import { ImageResponse } from '@vercel/og'; export const runtime = 'edge'; export async function GET(request) { const { searchParams } = new URL(request.url); const title = searchParams.get('title') || 'Default Title'; return new ImageResponse( ( <div style={{ backgroundColor: 'black', height: '100%', width: '100%', display: 'flex', justifyContent: 'center', alignItems: 'center', color: 'white', fontSize: 48, fontWeight: 'bold', }}> {title} </div> ), { width: 1200, height: 630, } ); }
Look at how the title variable is set and used inside the JSX.
The component reads the title query parameter from the URL. If it exists, it uses that text inside the image. Since the query parameter is ?title=Hello%20World, the text rendered is 'Hello World'. The background is black and text is white as per the styles.
This Next.js API route code snippet has a syntax error. Which option fixes it correctly?
import { ImageResponse } from '@vercel/og'; export const runtime = 'edge'; export async function GET(request) { const { searchParams } = new URL(request.url); const title = searchParams.get('title') || 'Default Title' return new ImageResponse( <div style={{ backgroundColor: 'black' }}>{title}</div>, { width: 1200, height: 630, } ); }
JSX expressions returned directly must be wrapped in parentheses if multiline.
The JSX passed to ImageResponse is multiline and must be wrapped in parentheses to avoid syntax errors. Adding a semicolon or changing tags does not fix this syntax issue. The function is already async.
Given this Next.js API route code for dynamic OG images, why does it return a 500 Internal Server Error when accessed?
import { ImageResponse } from '@vercel/og'; export const runtime = 'edge'; export async function GET(request) { const { searchParams } = new URL(request.url); const title = searchParams.get('title'); return new ImageResponse( <div style={{ backgroundColor: 'black', color: 'white' }}> {title.toUpperCase()} </div>, { width: 1200, height: 630, } ); }
Consider what happens if the title query parameter is not provided.
If title is null (no query parameter), calling toUpperCase() on null causes a runtime error, leading to a 500 response. The other options are incorrect because JSX is valid for ImageResponse, no await is needed, and 'edge' runtime is supported.
In this Next.js dynamic OG image API route, what are the dimensions of the generated image?
import { ImageResponse } from '@vercel/og'; export const runtime = 'edge'; export async function GET(request) { return new ImageResponse( <div style={{ backgroundColor: 'blue', color: 'white' }}>Test</div>, { width: 1200, height: 630, } ); }
Check the options object passed to ImageResponse.
The image dimensions are explicitly set to width 1200 and height 630 in the options object passed to ImageResponse. This is a common size for OG images.
Choose the correct statement about generating dynamic OG images in Next.js using the @vercel/og package and edge runtime.
Think about the benefits of edge runtime and @vercel/og for image generation.
Next.js dynamic OG images with @vercel/og and edge runtime generate images on-demand at request time with low latency near users. They support React functional components, custom fonts, and styles. Build-time only generation is false.