nextjs generate sitemap

nextjs

To generate a sitemap for a Next.js app, you can use the sitemap module. This module allows you to create a sitemap for your app by specifying the routes that should be included in the sitemap.

To use the sitemap module, you will first need to install it:

npm install sitemap

Once the module is installed, you can use it to generate a sitemap for your app by creating a script that defines your routes and then calling the createSitemap function:

const Sitemap = require('sitemap');

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

const sitemap = Sitemap.createSitemap({
  hostname: 'https://www.example.com',
  cacheTime: 600000,        // 600 sec - cache purge period
  urls: routes.map(path => {
    return { url: path,  changefreq: 'weekly', priority: 0.5 };
  })
});

sitemap.toXML((err, xml) => {
  if (err) {
    console.error(err);
    return;
  }
  console.log(xml);
});

This script will generate an XML sitemap for the routes specified in the routes array. You can then save the XML sitemap to a file or serve it dynamically from your app.

For more information on the sitemap module, you can check out the documentation.