A sitemap is an XML file containing the list of indexable URLs of a domain. When sitemaps become large, they are split into 1 sitemap index file that point to multiple sitemap files. Learn more about splitting sitemaps with Google’s documentation. next-sitemap is a library that conveniently generates the sitemap XML document after reading the Next.js build manifests or when given a list of URLs. Check out some real examples, like the Google sitemap index or the sitemap of this website.
getServerSideSitemapIndex to generate the sitemap index file.
getServerSideSitemap to generate a single sitemap file.
getServerSideSitemapIndexLegacy
.For the individual sitemaps, we need to fetch their corresponding page and pass the URLs getServerSideSitemapLegacy
.Caching the dynamic sitemapsSince the sitemaps are hitting our API or DB to load many items, we don’t want to execute those queries too often.With the Cache-Control header, Next.js allows caching at the framework level the result of server-side functions, including getServerSideProps. It works automatically when deployed to Vercel. Otherwise, you’ll need to set it up with Redis or similar.Learn more about Vercel caching here. Note that the response size can’t exceed 10 MB!