0
0
NextJSframework~20 mins

Dynamic OG images in NextJS - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Dynamic OG Image Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What does this Next.js dynamic OG image component render?

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?

NextJS
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,
    }
  );
}
AAn image with white background and black text 'Hello World' centered
BAn image with black background and white text 'title' centered
CAn image with black background and white text 'Default Title' centered
DAn image with black background and white text 'Hello World' centered
Attempts:
2 left
💡 Hint

Look at how the title variable is set and used inside the JSX.

📝 Syntax
intermediate
2:00remaining
Which option fixes the syntax error in this dynamic OG image code?

This Next.js API route code snippet has a syntax error. Which option fixes it correctly?

NextJS
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,
    }
  );
}
AAdd a semicolon after the title assignment line
BChange <div> to <div></div> with closing tag
CWrap the JSX in parentheses after return
DAdd async keyword before function GET
Attempts:
2 left
💡 Hint

JSX expressions returned directly must be wrapped in parentheses if multiline.

🔧 Debug
advanced
2:00remaining
Why does this dynamic OG image API route return a 500 error?

Given this Next.js API route code for dynamic OG images, why does it return a 500 Internal Server Error when accessed?

NextJS
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,
    }
  );
}
AImageResponse requires a string, but JSX is passed instead
Btitle is null if query param missing, so calling toUpperCase() causes error
CMissing await before new ImageResponse causes runtime error
DThe runtime 'edge' is not supported for this API route
Attempts:
2 left
💡 Hint

Consider what happens if the title query parameter is not provided.

state_output
advanced
2:00remaining
What is the width and height of the generated OG image?

In this Next.js dynamic OG image API route, what are the dimensions of the generated image?

NextJS
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,
    }
  );
}
AWidth 1200px and height 630px
BWidth 630px and height 1200px
CWidth 1024px and height 768px
DWidth 800px and height 600px
Attempts:
2 left
💡 Hint

Check the options object passed to ImageResponse.

🧠 Conceptual
expert
3:00remaining
Which statement about Next.js dynamic OG images is true?

Choose the correct statement about generating dynamic OG images in Next.js using the @vercel/og package and edge runtime.

AUsing the edge runtime allows generating OG images on-demand with low latency at the CDN edge
BDynamic OG images can only be generated at build time, not on-demand at request time
CThe <code>ImageResponse</code> component requires a React class component as input
DDynamic OG images cannot include custom fonts or styles
Attempts:
2 left
💡 Hint

Think about the benefits of edge runtime and @vercel/og for image generation.