Complete the code to import the Next.js API response type.
import type { [1] } from 'next';
The NextApiResponse type is used to type the response object in API routes, including sitemap generation.
Complete the code to set the response header for XML content.
res.setHeader('Content-Type', '[1]');
Sitemap files are XML files, so the content type must be application/xml to tell browsers and crawlers the correct format.
Fix the error in the sitemap XML string by completing the XML declaration.
const sitemap = `<?xml version=[1] encoding="UTF-8"?> <urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"> <!-- URLs here --> </urlset>`;
The XML declaration version must be "1.0" with double quotes for correct XML syntax.
Fill both blanks to generate URLs dynamically in the sitemap XML.
const sitemap = `<?xml version="1.0" encoding="UTF-8"?> <urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"> $[1].map((page) => ` <url> <loc>${page.[2]</loc> </url> `).join('') </urlset>`;
The sitemap is generated by mapping over the pages array and using the path property of each page for the URL location.
Fill all three blanks to complete the API route that returns the sitemap XML response.
export default function handler(req, res: [1]) { const pages = [ { [2]: 'https://example.com/' }, { [2]: 'https://example.com/about' } ]; const sitemap = `<?xml version="1.0" encoding="UTF-8"?> <urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"> ${pages.map(page => ` <url> <loc>${page.[3]</loc> </url> `).join('')} </urlset>`; res.setHeader('Content-Type', 'application/xml'); res.status(200).send(sitemap); }
The handler function types the response as NextApiResponse. The pages array uses the path property for URLs, which is also used inside the sitemap XML.