Sitemap for NextJS Application

#googleanalytics
#nextjs
#npm
#seo
#sitemap

There is a really handy npm package to create sitemaps for your NextJS application, next-sitemap can be used for both main pages and also dynamically created pages. Be able to use this:

First, install the package:

npm i next-sitemap

Add postbuild script to package.json:

"postbuild": "next-sitemap"

Create a file for the configurations in the root folder named next-sitemap.config.js

module.exports = {
    siteUrl: process.env.NEXT_PUBLIC_BASE_URL,
    generateRobotsTxt: true,
    exclude: ["/api/**"],
    robotsTxtOptions: {
        additionalSitemaps: [
            `${process.env.NEXT_PUBLIC_BASE_URL}/server-sitemap.xml`,
            `${process.env.NEXT_PUBLIC_BASE_URL}/sitemap.xml`
      ]
    }
}

Now, when you navigate the website.com/server-sitemap you will get all the dynamically created sitemap URLs.