I would suggest using https://github.com/iamvishnusankar/next-sitemap instead of using this library going forward, as it is not being maintained anymore.
Higher order Next.js config to generate sitemap.xml
and robots.txt
.
Read more at Medium.
Heavily inspired from IlusionDev/nextjs-sitemap-generator.
yarn add next-with-sitemap
Update or create next.config.js
with
const withSitemap = require("next-with-sitemap");
module.exports = withSitemap({
sitemap: {
baseUrl: "https://www.example.com",
},
// .
// ..
// ... other Next.js config
});
Add sitemap.xml
and robots.txt
to your .gitignore
robots.txt
sitemap.xml
There are options you can use to customize the behavior of this plugin
by adding sitemap
object in the Next.js config in next.config.js
.
Alongside those given sitemap
options, this library would also rely
on your Next.js config values exportPathMap
,
exportTrailingSlash
,
and pageExtensions
to come up with the correct sitemap.xml
content.
const withSitemap = require("next-with-sitemap");
module.exports = withSitemap({
sitemap: {
baseUrl: "https://www.example.com",
alternateBaseUrls: [
{
lang: "jp",
url: "https://example.jp",
},
{
lang: "es",
url: "https://example.com/es",
},
],
dest: "public",
excludedPaths: ["/login", "/signup"],
extraPaths: ["/extra/path"],
pages: "pages",
pageTags: [
{
path: "/",
priority: 1.0,
},
{
path: "/extra/path",
changefreq: "monthly",
},
{
path: "/about",
changefreq: "weekly",
priority: 0.5,
},
],
robots: true,
sitemap: true,
},
});
- baseUrl: string (required) - the baseUrl name including protocol and subdomain (e.g.
https://www.example.com
). - alternateBaseUrls: array - an array of alternative base urls for given languages.
- defaults to
[]
. - for each object in array:
- set
lang
(required) to the language identifier, - set
url
(required) to the base url that should be used for thatlang
.
- set
- defaults to
- dest: string - the destination folder to put generated files, relative to the project root.
- defaults to
public
.
- defaults to
- excludedPaths: array - an array of paths that should be excluded from the sitemap.
- defaults to
[]
.
- defaults to
- extraPaths: array - an array of extra paths that cannot be found at
pages
folder and that should be included to the sitemap.- defaults to
[]
.
- defaults to
- pages: string - the folder that contains page files, relative to the project root.
- defaults to
pages
.
- defaults to
- pageTags: array - an array of additional tags for any given path.
- defaults to
[]
. - for each object in array:
- set
path
(required) to the relative path of the page, - set
changefreq
to one ofalways
,hourly
,daily
,weekly
,monthly
,yearly
,never
values, - set
priority
to a number value between0
and1
(inclusive).
- set
- defaults to
- robots: boolean - whether to enable
robots.txt
generation.- defaults to
true
, - set
robots: false
, so it won't generate arobots.txt
, - set
robots: true
to generate arobots.txt
.
- defaults to
- sitemap: boolean - whether to enable
sitemap.xml
generation.- defaults to
true
, - set
sitemap: false
, so it won't generate asitemap.xml
, - set
sitemap: true
to generate asitemap.xml
.
- defaults to