0
0
NextJSframework~20 mins

Sitemap.xml generation in NextJS - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Sitemap Mastery in Next.js
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What does this Next.js sitemap generation code output?
Consider this Next.js server component that generates a sitemap.xml response. What is the output when visiting the sitemap route?
NextJS
import { NextResponse } from 'next/server';

export async function GET() {
  const sitemap = `<?xml version="1.0" encoding="UTF-8"?>\n<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">\n  <url>\n    <loc>https://example.com/</loc>\n    <changefreq>daily</changefreq>\n  </url>\n</urlset>`;
  return new NextResponse(sitemap, {
    headers: {
      'Content-Type': 'application/xml'
    }
  });
}
AA JSON response with the sitemap data as an object
BAn XML sitemap with one URL entry for https://example.com/, served with Content-Type application/xml
CA plain text response showing the sitemap XML as a string but with Content-Type text/plain
DA runtime error because NextResponse cannot be used in server components
Attempts:
2 left
💡 Hint
Think about what NextResponse does and the Content-Type header for XML.
📝 Syntax
intermediate
2:00remaining
Which option correctly generates a sitemap.xml in Next.js using server actions?
You want to create a sitemap.xml route in Next.js 14+ using server actions. Which code snippet correctly returns the sitemap XML with proper headers?
A
export async function GET() {
  return new Response('&lt;urlset&gt;&lt;/urlset&gt;', { headers: { 'Content-Type': 'application/xml' } });
}
B
export function GET() {
  return '&lt;urlset&gt;&lt;/urlset&gt;';
}
C
export async function GET() {
  return Response('&lt;urlset&gt;&lt;/urlset&gt;');
}
D
export async function GET() {
  return new NextResponse('&lt;urlset&gt;&lt;/urlset&gt;');
}
Attempts:
2 left
💡 Hint
Remember to use the Response constructor and set headers explicitly.
🔧 Debug
advanced
2:00remaining
Why does this sitemap.xml route return a 404 error in Next.js?
Given this Next.js route file named sitemap.xml.js inside app directory, why does visiting /sitemap.xml return 404? ```js export async function GET() { const xml = ``; return new Response(xml, { headers: { 'Content-Type': 'application/xml' } }); } ```
AThe Response constructor is not imported from 'next/server'
BThe GET function is missing the export default keyword
CThe file name sitemap.xml.js is invalid; Next.js expects route files without dots except for extensions
DThe XML string is malformed and causes a runtime error
Attempts:
2 left
💡 Hint
Check Next.js routing conventions for file names in the app directory.
state_output
advanced
2:00remaining
What is the output of this dynamic sitemap generation code in Next.js?
This Next.js server component generates sitemap XML dynamically from a list of pages. What is the output?
NextJS
import { NextResponse } from 'next/server';

const pages = ['/', '/about', '/contact'];

export async function GET() {
  const urls = pages.map(page => `  <url><loc>https://example.com${page}</loc></url>`).join('\n');
  const sitemap = `<?xml version="1.0" encoding="UTF-8"?>\n<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">\n${urls}\n</urlset>`;
  return new NextResponse(sitemap, { headers: { 'Content-Type': 'application/xml' } });
}
AA runtime error because pages is not awaited
BA JSON array of URLs as the response body
CAn empty sitemap with no URLs inside <urlset>
D
&lt;?xml version="1.0" encoding="UTF-8"?&gt;
&lt;urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"&gt;
  &lt;url&gt;&lt;loc&gt;https://example.com/&lt;/loc&gt;&lt;/url&gt;
  &lt;url&gt;&lt;loc&gt;https://example.com/about&lt;/loc&gt;&lt;/url&gt;
  &lt;url&gt;&lt;loc&gt;https://example.com/contact&lt;/loc&gt;&lt;/url&gt;
&lt;/urlset&gt;
Attempts:
2 left
💡 Hint
Look at how pages are mapped to XML strings and joined.
🧠 Conceptual
expert
2:00remaining
Why is using a server component with NextResponse preferred for sitemap.xml in Next.js 14+?
Select the best reason why generating sitemap.xml using a Next.js server component with NextResponse is recommended over static files or client components.
AIt allows dynamic generation of sitemap content on each request with correct headers, improving SEO and freshness.
BIt enables client-side rendering of sitemap.xml for faster user interaction.
CIt forces the sitemap to be cached indefinitely, reducing server load but risking stale data.
DIt automatically minifies the sitemap XML without extra configuration.
Attempts:
2 left
💡 Hint
Think about SEO needs and how server components handle responses.