The transform function allows you to add, remove, or exclude path or properties from the generated sitemap. This function runs for each relative path in the sitemap.
- To exclude a path: Return
null from the transformation function. This will prevent that specific relative path from being included in the generated sitemap. - To customize properties: Return an object containing the desired XML fields. Returning a partial object (e.g., only
loc and changefreq) will result in an XML entry containing only those specific fields. - The
loc property: When returning an object, the loc value is exported as http(s)://<config.siteUrl>/<path>.
/** @type {import('next-sitemap').IConfig} */
module.exports = {
transform: async (config, path) => {
// custom function to ignore the path
if (customIgnoreFunction(path)) {
return null
}
// only create changefreq along with path
// returning partial properties will result in generation of XML field with only returned values.
if (customLimitedField(path)) {
// This returns `path` & `changefreq`. Hence it will result in the generation of XML field with `path`
// and `changefreq` properties only.
return {
loc: path, // => this will be exported as http(s)://<config.siteUrl>/<path>
changefreq: 'weekly',
}
}
// Use default transformation for all other cases
return {
loc: path, // => this will be exported as http(s)://<config.siteUrl>/<path>
changefreq: config.changefreq,
priority: config.priority,
lastmod: config.autoLastmod ? new Date().toISOString() : undefined,
alternateRefs: config.alternateRefs ?? [],
}
},
}