codingstuff.io
ExploreTutorialsProblemsCS Subjects
Get Started
ExploreTutorialsProblemsCS Subjects
Get Started
codingstuff.io

Master the art of building software through interactive tutorials, real-world problems, and guided projects.

Pune, Maharashtra, India

codingstuffmail@gmail.com

Product

  • Explore
  • Tutorials
  • Problems
  • CS Subjects

Company

  • About
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Sitemap

© 2026 codingstuff.io. All rights reserved.

Built with ❤️ for developers everywhere

/
/
All Tutorials
▲

Next.js

50 / 73 topics
48SEO Optimization49Meta Tags and Open Graph50Sitemap Generation51Performance Auditing
Tutorials/Next.js/Sitemap Generation
▲Next.js

Sitemap Generation

Updated 2026-04-20
2 min read

Introduction

In this tutorial, we will explore how to generate sitemaps for your Next.js application. A sitemap is an XML file that lists the URLs of a website and provides information about each URL, such as its last modification date, change frequency, and priority. Sitemaps are essential for SEO, helping search engines understand the structure and content of your site.

Prerequisites

Before we dive into generating sitemaps in Next.js, ensure you have the following:

  • A basic understanding of Next.js.
  • Node.js installed on your machine.
  • An existing Next.js project or create a new one using npx create-next-app@latest.

Setting Up Your Project

If you don't already have a Next.js project, let's create one:

npx create-next-app@latest my-nextjs-app
cd my-nextjs-app

Generating Static Sitemaps

Next.js provides built-in support for generating static pages. We can leverage this feature to generate sitemaps.

Step 1: Install Required Packages

First, install the next-sitemap package, which simplifies sitemap generation:

npm install next-sitemap

Step 2: Configure Sitemap Generation

Create a next-sitemap.config.js file in the root of your project with the following configuration:

module.exports = {
  siteUrl: 'https://www.yourdomain.com',
  generateRobotsTxt: true,
};

Replace 'https://www.yourdomain.com' with your actual domain.

Step 3: Update next.config.js

Next, update your next.config.js to include the withSitemap plugin:

const { withSitemap } = require('next-sitemap');

module.exports = withSitemap({
  // Your Next.js config options here
})();

Step 4: Generate Sitemaps

Run the following command to generate your sitemap and robots.txt file:

npm run build

After building, you should find sitemap.xml and robots.txt files in the out directory.

Dynamic Sitemap Generation

If your site has dynamic content that changes frequently, you might need a more dynamic approach to generating sitemaps. We can use serverless functions to handle this.

Step 1: Create a Serverless Function

Create a new file pages/api/sitemap.js:

export default function handler(req, res) {
  const urls = [
    '/',
    '/about',
    '/contact',
    // Add more dynamic URLs here
  ];

  const sitemap = `
    <?xml version="1.0" encoding="UTF-8"?>
    <urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
      ${urls.map((url) => `
        <url>
          <loc>${process.env.NEXT_PUBLIC_SITE_URL}${url}</loc>
          <lastmod>${new Date().toISOString()}</lastmod>
          <changefreq>daily</changefreq>
          <priority>1.0</priority>
        </url>
      `).join('')}
    </urlset>
  `;

  res.setHeader('Content-Type', 'text/xml');
  res.write(sitemap);
  res.end();
}

Step 2: Update Environment Variables

Add your site URL to your environment variables in .env.local:

NEXT_PUBLIC_SITE_URL=https://www.yourdomain.com

Step 3: Access the Sitemap

You can now access your dynamic sitemap at https://yourdomain.com/api/sitemap.

Best Practices

  1. Regular Updates: Ensure your sitemap is updated regularly to reflect changes in your site's content.
  2. Include All Important Pages: Make sure all important pages are included in the sitemap, especially those that might not be easily discoverable by search engines.
  3. Use HTTPS: Always use HTTPS URLs in your sitemap for better security and SEO performance.
  4. Validate Your Sitemap: Use online tools like XML-Sitemaps to validate your sitemap for errors.

Conclusion

Generating a sitemap in Next.js is straightforward, whether you choose a static or dynamic approach. By following the steps outlined in this tutorial, you can ensure that search engines have an accurate and up-to-date map of your site's content, improving both SEO and user experience.


PreviousMeta Tags and Open GraphNext Performance Auditing

Recommended Gear

Meta Tags and Open GraphPerformance Auditing