yarn add next-sitemap
next-sitemap requires a basic config file (next-sitemap.config.js) under your project root
✅
next-sitemapwill load environment variables from.envfiles by default.
/** @type {import('next-sitemap').IConfig} */
module.exports = {
siteUrl: process.env.SITE_URL || 'https://example.com',
generateRobotsTxt: true, // (optional)
// ...other options
}
Add next-sitemap as your postbuild script
{
"build": "next build",
"postbuild": "next-sitemap"
}
You can also use a custom config file instead of next-sitemap.config.js. Just pass --config <your-config-file>.js to build command (Example: custom-config-file)
{
"build": "next build",
"postbuild": "next-sitemap --config awesome.config.js"
}
When using pnpm you need to create a .npmrc file in the root of your project if you want to use a postbuild step:
//.npmrc
enable-pre-post-scripts=true
📣 From next-sitemap v2.x onwards, sitemap.xml will be Index Sitemap. It will contain urls of all other generated sitemap endpoints.
Index sitemap generation can be turned off by setting generateIndexSitemap: false in next-sitemap config file. (This is useful for small/hobby sites which does not require an index sitemap) (Example: no-index-sitemaps)
Define the sitemapSize property in next-sitemap.config.js to split large sitemap into multiple files.
/** @type {import('next-sitemap').IConfig} */
module.exports = {
siteUrl: 'https://example.com',
generateRobotsTxt: true,
sitemapSize: 7000,
}
Above is the minimal configuration to split a large sitemap. When the number of URLs in a sitemap is more than 7000, next-sitemap will create sitemap (e.g. sitemap-0.xml, sitemap-1.xml) and index (e.g. sitemap.xml) files.
| property | description | type |
|---|---|---|
| siteUrl | Base url of your website | string |
| output (optional) | Next.js output modes. Check documentation. | standalone, export |
| changefreq (optional) | Change frequency. Default daily |
string |
| priority (optional) | Priority. Default 0.7 |
number |
| sitemapBaseFileName (optional) | The name of the generated sitemap file before the file extension. Default "sitemap" |
string |
| alternateRefs (optional) | Denote multi-language support by unique URL. Default [] |
AlternateRef[] |
| sitemapSize(optional) | Split large sitemap into multiple files by specifying sitemap size. Default 5000 |
number |
| autoLastmod (optional) | Add <lastmod/> property. Default true |
true |
| exclude (optional) | Array of relative paths (wildcard pattern supported) to exclude from listing on sitemap.xml or sitemap-*.xml. e.g.: ['/page-0', '/page-*', '/private/*']. |
Apart from this option next-sitemap also offers a custom transform option which could be used to exclude urls that match specific patterns | string[] |
| sourceDir (optional) | next.js build directory. Default .next | string |
| outDir (optional) | All the generated files will be exported to this directory. Default public | string |
| transform (optional) | A transformation function, which runs for each relative-path in the sitemap. Returning null value from the transformation function will result in the exclusion of that specific path from the generated sitemap list. | async function |
| additionalPaths (optional) | Async function that returns a list of additional paths to be added to the generated sitemap list. | async function |
| generateIndexSitemap | Generate index sitemaps. Default true | boolean |
| generateRobotsTxt (optional) | Generate a robots.txt file and list the generated sitemaps. Default false | boolean
$ claude mcp add next-sitemap \
-- python -m otcore.mcp_server <graph>