Advertisement
Advanced Next.js, Metadata API & SEO Optimization

How would you generate a sitemap and robots file to help search engines properly crawl a large Next.js website?

Short answer

You create special files named sitemap.js and robots.js in your app folder, exporting functions that return the list of URLs to include in your sitemap and the crawling rules for search engines, and Next.js automatically builds these into the proper sitemap.xml and robots.txt…

Save Next.js mock interview
Advertisement
AModel answer
You create special files named sitemap.js and robots.js in your app folder, exporting functions that return the list of URLs to include in your sitemap and the crawling rules for search engines, and Next.js automatically builds these into the proper sitemap.xml and robots.txt files.
// app/sitemap.js
export default async function sitemap() {
  const posts = await getAllPosts();
  return posts.map(post => ({
    url: `https://example.com/blog/${post.slug}`,
    lastModified: post.updatedAt
  }));
}
Real-world example
A large content website automatically generates its sitemap from its full list of published articles, ensuring search engines can discover and index every article without anything being manually maintained.
Likely follow-ups
  • How often should a sitemap be regenerated for a frequently updated website?
  • What information can a robots.js file control for search engine crawlers?
Related topics