{"id":26217029,"url":"https://github.com/moxystudio/next-sitemaps","last_synced_at":"2025-07-05T00:07:07.945Z","repository":{"id":38424970,"uuid":"247058561","full_name":"moxystudio/next-sitemaps","owner":"moxystudio","description":"An API handler and a plugin that enables your Next.js application to have working sitemaps","archived":false,"fork":false,"pushed_at":"2023-01-24T07:00:28.000Z","size":1350,"stargazers_count":12,"open_issues_count":8,"forks_count":1,"subscribers_count":0,"default_branch":"master","last_synced_at":"2025-05-18T14:38:19.609Z","etag":null,"topics":["nextjs","react"],"latest_commit_sha":null,"homepage":"","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/moxystudio.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2020-03-13T11:43:10.000Z","updated_at":"2022-03-19T01:19:40.000Z","dependencies_parsed_at":"2023-02-13T18:48:30.320Z","dependency_job_id":null,"html_url":"https://github.com/moxystudio/next-sitemaps","commit_stats":null,"previous_names":["moxystudio/next-sitemaps-plugin"],"tags_count":10,"template":false,"template_full_name":null,"purl":"pkg:github/moxystudio/next-sitemaps","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/moxystudio%2Fnext-sitemaps","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/moxystudio%2Fnext-sitemaps/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/moxystudio%2Fnext-sitemaps/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/moxystudio%2Fnext-sitemaps/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/moxystudio","download_url":"https://codeload.github.com/moxystudio/next-sitemaps/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/moxystudio%2Fnext-sitemaps/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":263636811,"owners_count":23492308,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["nextjs","react"],"created_at":"2025-03-12T12:18:38.211Z","updated_at":"2025-07-05T00:07:07.889Z","avatar_url":"https://github.com/moxystudio.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# next-sitemaps\n\n[![NPM version][npm-image]][npm-url] [![Downloads][downloads-image]][npm-url] [![Build Status][build-status-image]][build-status-url] [![Coverage Status][codecov-image]][codecov-url] [![Dependency status][david-dm-image]][david-dm-url] [![Dev Dependency status][david-dm-dev-image]][david-dm-dev-url]\n\n[npm-url]:https://npmjs.org/package/@moxy/next-sitemaps\n[downloads-image]:https://img.shields.io/npm/dm/@moxy/next-sitemaps.svg\n[npm-image]:https://img.shields.io/npm/v/@moxy/next-sitemaps.svg\n[build-status-url]:https://github.com/moxystudio/next-sitemaps/actions\n[build-status-image]:https://img.shields.io/github/workflow/status/moxystudio/next-sitemaps/Node%20CI/master\n[codecov-url]:https://codecov.io/gh/moxystudio/next-sitemaps\n[codecov-image]:https://img.shields.io/codecov/c/github/moxystudio/next-sitemaps/master.svg\n[david-dm-url]:https://david-dm.org/moxystudio/next-sitemaps\n[david-dm-image]:https://img.shields.io/david/moxystudio/next-sitemaps.svg\n[david-dm-dev-url]:https://david-dm.org/moxystudio/next-sitemaps?type=dev\n[david-dm-dev-image]:https://img.shields.io/david/dev/moxystudio/next-sitemaps.svg\n\nThis module provides an API handler and a plugin that enables your Next.js application to have working sitemaps so that search engine crawlers can index all your pages.\n\n## Installation\n\n```sh\nnpm install @moxy/next-sitemaps\n```\n\n## Usage\n\n**1. Add the plugin to `next.config.js`**\n\n```js\nconst withSitemap = require('@moxy/next-sitemaps/plugin');\n\nmodule.exports = (phase) =\u003e withSitemap(phase, 'https://moxy.studio')({ ...nextConfig });\n```\n\n\u003e ℹ️ If you have multiple plugins, you may simply compose them or use [`next-compose-plugins`](https://github.com/cyrilwanner/next-compose-plugins) for better readability.\n\n**2. Add the API handler on `pages/api/sitemap.xml.js`**\n\n```js\nimport createSitemapApiHandler from '@moxy/next-sitemaps';\n\nexport default createSitemapApiHandler({\n    mapDynamicRoutes: {\n        '/[id]': () =\u003e ['id1', 'id2'],\n    },\n});\n```\n\n**3. Add robots.txt file**\n\n```txt\nSitemap: ${siteUrl}/api/sitemap.xml\n\nUser-agent:*\nDisallow:\n\n# ...\n```\n\nPlease note that `${siteUrl}` will be replaced automatically by the `siteUrl` specified when instantiating the plugin. However, the replacement will not happen during development to avoid checking in the changed file into source-control (e.g.: git).\n\n\u003e ⚠️ During development, new pages will not be present in the XML. Restart the development server for page changes to take effect.\n\n## API\n\n### withSitemap(phase, siteUrl, [options])\n\nThe Next.js plugin that should be installed in `next.config.js`. It will do two things:\n\n- Iterate over the `/pages` (or `src/pages`) folder and map all files into routes. These mappings will be used by the API Handler.\n- Replace the `${siteUrl}` placeholder inside `robots.txt` with `siteUrl`.\n\n#### phase\n\nType: `string`\n\nThe Next.js current context phase. This plugin will only be active if `phase` is `PHASE_DEVELOPMENT_SERVER` or `PHASE_PRODUCTION_BUILD`.\n\n##### siteUrl\n\nType: `string`   \nDefault: `'/'`\n\nThe website URL.\n\n#### options\n\nType: `object`\n\n##### replaceSiteUrlInRobots\n\nType: `boolean`   \nDefault: `true` if `process.env.NODE_ENV` is set to production\n\nThis option will replace `${siteUrl}` inside `robots.txt`. During development, you want this option disabled to avoid checking in the resulting file into source-control (e.g.: git).\n\n### createSitemapApiHandler([options])\n\nDefines an API handler that will respond with a valid sitemap XML file containing the website pages.\n\n#### options\n\nType: `object`\n\n##### mapDynamicRoutes\n\nType: `object`   \nDefault: `{}`\n\nAn object that indicates the possible values for each dynamic route. [More info](#handling-dynamic-routing).\n\n##### cacheControl\n\nType: `string`   \nDefault: `public, max-age=3600` (max-age is set to 0 if not in production)\n\nA string defining the [Cache-Control](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Cache-Control) header for HTTP responses. Since sitemaps can be rather expensive to generate, you may leverage a [Reverse Proxy](https://en.wikipedia.org/wiki/Reverse_proxy) cache or a pull CDN such as [CloudFlare](https://www.cloudflare.com/) (via a page rule), to cache the sitemap response.\n\nWhile a reverse-proxy or pull CDN is preferable, you may cache the sitemap programmatically like so:\n\n```js\nimport pMemoize from 'p-memoize';\nimport createSitemapApiHandler from '@moxy/next-sitemaps';\n\nconst apiHandler = createSitemapApiHandler('https://moxy.studio', {\n    mapDynamicRoutes: {\n        '/[id]': () =\u003e ['id1', 'id2'],\n    },\n});\n\nexport default pMemoize(apiHandler, {\n    cacheKey: () =\u003e 'sitemap',\n    cachePromiseRejection: true,\n    maxAge: 5 * 60 * 1000, // 5 minutes\n});\n```\n\nThe example above uses [`p-memoize`](https://github.com/sindresorhus/p-memoize), which caches the result in-memory for `5 minutes`. However, you can use a distributed cache, such as Redis.\n\n##### logWarning\n\nType: `function`    \nDefault: see `logWarningDefault` in [src/api-handler/index.js](./src/api-handler/index.js)\n\nA function to log possible warnings. It has the following signature: `(message) =\u003e {}`.\n\n##### logError\n\nType: `function`    \nDefault: see `logErrorDefault` in [src/api-handler/index.js](./src/api-handler/index.js)\n\nA function to log possible errors. It has the following signature: `(err) =\u003e {}`.\n\n### Handling dynamic routing\n\n```js\ncreateSitemapApiHandler('https://moxy.studio', {\n    mapDynamicRoutes: {\n        '/[project]': () =\u003e ['some-project', 'another-project'],\n        '/old/[post]': () =\u003e ['old-post', 'ancient-post'],\n        '/[post]/new': () =\u003e ['new-post', 'another-new-post'],\n    },\n});\n```\n\nThe API route above would respond with the following XML:\n\n```xml\n\u003c?xml version=\"1.0\" encoding=\"UTF-8\"?\u003e\n\u003curlset xmlns=\"http://www.sitemaps.org/schemas/sitemap/0.9\"\u003e\n    \u003curl\u003e\n        \u003cloc\u003ehttps://moxy.studio/another-project\u003c/loc\u003e\n    \u003c/url\u003e\n    \u003curl\u003e\n        \u003cloc\u003ehttps://moxy.studio/some-project\u003c/loc\u003e\n    \u003c/url\u003e\n    \u003curl\u003e\n        \u003cloc\u003ehttps://moxy.studio/old/old-post\u003c/loc\u003e\n    \u003c/url\u003e\n    \u003curl\u003e\n        \u003cloc\u003ehttps://moxy.studio/old/ancient-post\u003c/loc\u003e\n    \u003c/url\u003e\n    \u003curl\u003e\n        \u003cloc\u003ehttps://moxy.studio/new-post/new\u003c/loc\u003e\n    \u003c/url\u003e\n    \u003curl\u003e\n        \u003cloc\u003ehttps://moxy.studio/another-new-post/new\u003c/loc\u003e\n    \u003c/url\u003e\n\u003c/urlset\u003e\n```\n\n\u003e ℹ️ [Catch all](https://nextjs.org/docs/routing/dynamic-routes#catch-all-routes) routes are also supported.\n\nYou might have routes with several dynamic placeholders. In this case, first-level mappers will be called first, second-level mappers will be called secondly, one time for each result of the first mapper, and so on. In short, calls to mappers are permuted and they receive the current iteration values to aid you in data-fetching.\n\n```js\ncreateSitemapApiHandler('https://moxy.studio', {\n    mapDynamicRoutes: {\n        '/[company]': async () =\u003e {\n            // Returns ['microsoft', 'apple']\n            const companies = await fetchCompanies();\n\n            return companies;\n        },\n        // The function below will be called two times.\n        // The first will have `company` set microsoft and the second one will have it set to `apple`.\n        '/[company]/[apps]': ({ company }) =\u003e {\n            // Returns ['vscode', 'vsstudio'] for microsoft and returns ['itunes'] for apple.\n            const apps = fetchCompanyApps(company);\n\n            return apps;\n        },\n    },\n});\n```\n\nAssuming the return values of the mappers above, the generated sitemap would look like:\n\n```xml\n\u003c?xml version=\"1.0\" encoding=\"UTF-8\"?\u003e\n\u003curlset xmlns=\"http://www.sitemaps.org/schemas/sitemap/0.9\"\u003e\n    \u003curl\u003e\n        \u003cloc\u003ehttps://moxy.studio/apple\u003c/loc\u003e\n    \u003c/url\u003e\n    \u003curl\u003e\n        \u003cloc\u003ehttps://moxy.studio/apple/itunes\u003c/loc\u003e\n    \u003c/url\u003e\n    \u003curl\u003e\n        \u003cloc\u003ehttps://moxy.studio/microsoft\u003c/loc\u003e\n    \u003c/url\u003e\n    \u003curl\u003e\n        \u003cloc\u003ehttps://moxy.studio/microsoft/vscode\u003c/loc\u003e\n    \u003c/url\u003e\n    \u003curl\u003e\n        \u003cloc\u003ehttps://moxy.studio/microsoft/vsstudio\u003c/loc\u003e\n    \u003c/url\u003e\n\u003c/urlset\u003e\n```\n\n## Tests\n\n```sh\n$ npm t\n$ npm t -- --watch  # To run watch mode\n```\n\n## License\n\n[MIT License](./LICENSE)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmoxystudio%2Fnext-sitemaps","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmoxystudio%2Fnext-sitemaps","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmoxystudio%2Fnext-sitemaps/lists"}