0
0
NextJSframework~20 mins

GenerateMetadata for dynamic metadata in NextJS - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Dynamic Metadata Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What is the output of this dynamic metadata function?
Consider this Next.js dynamic metadata function. What metadata will be generated for the page?
NextJS
export async function generateMetadata({ params }) {
  const { id } = params;
  return {
    title: `Post ${id}`,
    description: `Details about post ${id}`,
  };
}

// Assume params = { id: '42' }
A{"title":"Post id","description":"Details about post id"}
B{"title":"Post","description":"Details about post"}
C{"title":"Post 42","description":"Details about post 42"}
D{"title":"42","description":"42"}
Attempts:
2 left
💡 Hint
Look at how the id from params is used inside the template strings.
📝 Syntax
intermediate
2:00remaining
Which option causes a syntax error in generateMetadata?
Identify which generateMetadata function code snippet will cause a syntax error in Next.js.
A
export async function generateMetadata({ params }) {
  return {
    title: `Page ${params.slug}`,
  };
}
B
export async function generateMetadata({ params }) {
  return {
    title: `Page ${params.slug}`
  }
}
C
export async function generateMetadata({ params }) {
  return {
    title: 'Page ' + params.slug
  }
}
D
export async function generateMetadata(params) {
  return {
    title: `Page ${params.slug}`
  };
}
Attempts:
2 left
💡 Hint
Check for missing closing braces or parentheses.
state_output
advanced
2:00remaining
What metadata is generated when params.slug is undefined?
Given this generateMetadata function, what will be the output if params.slug is undefined?
NextJS
export async function generateMetadata({ params }) {
  return {
    title: `Page ${params.slug || 'default'}`,
    description: `Content for ${params.slug || 'default'}`
  };
}
A{"title":"Page null","description":"Content for null"}
B{"title":"Page undefined","description":"Content for undefined"}
C{"title":"Page ","description":"Content for "}
D{"title":"Page default","description":"Content for default"}
Attempts:
2 left
💡 Hint
Look at the fallback value used with || operator.
🔧 Debug
advanced
2:00remaining
Why does this generateMetadata function cause a runtime error?
Examine the code and identify the cause of the runtime error when generateMetadata is called.
NextJS
export async function generateMetadata({ params }) {
  const data = await fetch(`https://api.example.com/posts/${params.id}`);
  const post = await data.json();
  return {
    title: post.title,
    description: post.summary
  };
}

// Assume params.id is undefined
Afetch URL is invalid because params.id is undefined, causing a network error
Bpost.title is undefined causing a TypeError
Cdata.json() is not a function causing a TypeError
DNo error, function returns default metadata
Attempts:
2 left
💡 Hint
Check the URL used in fetch and the value of params.id.
🧠 Conceptual
expert
2:00remaining
Which option best describes the purpose of generateMetadata in Next.js dynamic routes?
Select the most accurate description of what generateMetadata does in Next.js dynamic routes.
AIt dynamically creates metadata for each page request based on route parameters
BIt statically generates metadata at build time for all pages regardless of params
CIt replaces the need for a head component by injecting metadata client-side only
DIt caches metadata on the client to improve page load speed
Attempts:
2 left
💡 Hint
Think about when and how metadata changes for dynamic routes.