A sitemap.xml helps search engines find and understand your website pages easily. It improves your site's visibility on search results.
Sitemap.xml generation in NextJS
export async function GET() { const baseUrl = 'https://example.com'; const routes = ['/', '/about', '/contact']; const sitemap = `<?xml version="1.0" encoding="UTF-8"?>\n<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">\n${routes.map(route => ` <url>\n <loc>${baseUrl}${route}</loc>\n </url>`).join('\n')}\n</urlset>`; return new Response(sitemap, { headers: { 'Content-Type': 'application/xml' } }); }
This code exports a GET function for a Next.js route that returns XML content.
The sitemap XML must follow the standard sitemap protocol for search engines.
const routes = ['/', '/blog', '/products']; const sitemap = `<?xml version="1.0" encoding="UTF-8"?>\n<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">\n${routes.map(route => ` <url>\n <loc>https://example.com${route}</loc>\n </url>`).join('\n')}\n</urlset>`;
return new Response(sitemap, { headers: { 'Content-Type': 'application/xml' } });
This Next.js route handler generates a sitemap.xml dynamically listing four pages. When you visit this route, it returns the sitemap XML with correct headers.
export async function GET() { const baseUrl = 'https://mywebsite.com'; const routes = ['/', '/about', '/blog', '/contact']; const sitemap = `<?xml version="1.0" encoding="UTF-8"?>\n<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">\n${routes.map(route => ` <url>\n <loc>${baseUrl}${route}</loc>\n </url>`).join('\n')}\n</urlset>`; return new Response(sitemap, { headers: { 'Content-Type': 'application/xml' } }); }
Make sure the sitemap URL is accessible publicly so search engines can find it.
Update the routes list whenever you add or remove pages to keep the sitemap accurate.
You can automate sitemap generation for dynamic routes by fetching data inside the GET function.
Sitemap.xml helps search engines find your pages faster.
In Next.js, create a GET route that returns XML with page URLs.
Set the response header to 'application/xml' for correct recognition.
Practice
sitemap.xml in a Next.js application?Solution
Step 1: Understand sitemap.xml role
A sitemap.xml file lists URLs of your website to guide search engines.Step 2: Connect sitemap.xml to Next.js usage
In Next.js, sitemap.xml helps search engines find pages faster by providing URL info.Final Answer:
To help search engines find and index your pages faster -> Option AQuick Check:
Sitemap.xml purpose = Help search engines [OK]
- Confusing sitemap.xml with styling or UI files
- Thinking sitemap.xml stores user data
- Assuming sitemap.xml adds client-side routing
Solution
Step 1: Identify correct content type for sitemap.xml
Sitemap.xml is an XML file, so the content type must be 'application/xml'.Step 2: Match header setting in Next.js API route
Useres.setHeader('Content-Type', 'application/xml')to set correct header.Final Answer:
res.setHeader('Content-Type', 'application/xml') -> Option DQuick Check:
Content-Type for sitemap.xml = application/xml [OK]
- Using 'text/html' instead of 'application/xml'
- Setting content type to 'application/json'
- Forgetting to set content type header
export async function GET(req, res) {
res.setHeader('Content-Type', 'application/xml');
return new Response(`<urlset></urlset>`);
}Solution
Step 1: Understand Next.js 13+ GET handler response
In Next.js 13+, the GET function returns a Response object; setting headers on 'res' does not affect it.Step 2: Check if content type header is set on Response
The Response constructor here lacks headers, so no content type header is sent.Final Answer:
No content type header set -> Option CQuick Check:
Setting header on 'res' ignored, Response lacks headers [OK]
- Assuming res.setHeader affects Response output
- Not adding headers inside Response constructor
- Confusing old API routes with new app router
export async function GET() {
const sitemap = `<urlset><url><loc>/home</loc></url></urlset>`;
return new Response(sitemap);
}Solution
Step 1: Check response headers for sitemap.xml
The code returns XML string but does not set 'Content-Type' header to 'application/xml'.Step 2: Fix by adding headers in Response constructor
Add{ headers: { 'Content-Type': 'application/xml' } }to Response to fix the bug.Final Answer:
Missing 'Content-Type' header; add it in Response headers -> Option AQuick Check:
Set 'Content-Type' header for XML response [OK]
- Forgetting to set content type header
- Using wrong XML tags for sitemap
- Misnaming the GET function
Solution
Step 1: Fetch URLs from database asynchronously
Use async function to get URLs from database or API.Step 2: Build XML string with URLs and return Response with correct header
Create XML sitemap string, then returnnew Response(xmlString, { headers: { 'Content-Type': 'application/xml' } }).Final Answer:
Fetch URLs, build XML string, then return new Response with XML and 'application/xml' header -> Option BQuick Check:
Dynamic sitemap = fetch + XML + Response with header [OK]
- Returning JSON instead of XML for sitemap
- Not setting content type header
- Returning plain string without Response object
Ready to prove it?
Pass the test and this pattern moves unknown
