express-sitemap-xml is an Express.js middleware designed to automatically generate and serve sitemap.xml files based on a dynamic list of URLs provided by an async function. It intelligently handles large sitemaps, automatically splitting them into multiple files and generating a sitemap index (sitemap.xml) when more than 50,000 URLs are present, adhering to the sitemap protocol. The current stable version is 3.1.0, with recent updates indicating active maintenance and feature additions, such as image support and base URL fixes. It differentiates itself by its automatic handling of large sitemaps, caching mechanisms for performance, and a flexible API that can also be used independently of Express for pure sitemap generation. The package calls the URL-fetching function at most once every 24 hours, caching the results to optimize performance for subsequent requests.
npm install express-sitemap-xmlVerified import paths — ran on the pinned version, not inferred.
This quickstart sets up an Express server with the express-sitemap-xml middleware, demonstrating how to provide a dynamic list of URLs and serve the sitemap.xml file. It includes a simulated asynchronous URL fetching function and shows both simple URL strings and more detailed URL objects.
Consider application restart for immediate updates or manually clear caches if a more granular control is needed, acknowledging the 24-hour cache limit. For most use cases, this caching behavior is beneficial.
Do not include a `priority` field in URL objects. Focus on `lastMod` and `changeFreq` if you need to provide additional details beyond the URL itself.
Always point your `robots.txt` to the root `sitemap.xml` (e.g., `Sitemap: https://yourdomain.com/sitemap.xml`). The package handles the internal linking to sub-sitemaps automatically.
If using ES modules: `import expressSitemapXml from 'express-sitemap-xml';`. If using CommonJS: `const expressSitemapXml = require('express-sitemap-xml');`.Ensure the first argument is a function, preferably an `async` function, for example: `app.use(expressSitemapXml(async () => ['/'], 'https://example.com'))`.
Switch to ES module `import` syntax: `import express from 'express'; import expressSitemapXml from 'express-sitemap-xml';`.