A simple sitemap.xml module for Nuxt 3.
Status: v1 Stable Please report any issues ๐ Made possible by my Sponsor Program ๐ Follow me @harlan_zw ๐ฆ โข Join Discord for help |
โน๏ธ Looking for a complete SEO solution? Check out Nuxt SEO Kit.
- ๐ช Minimal config, powerful API
- ๐ Route config using route rules
- ๐๏ธ Handle trailing slashes
- ๐ฆ Uses sitemap.js
npm install --save-dev nuxt-simple-sitemap
# Using yarn
yarn add --dev nuxt-simple-sitemap
nuxt.config.ts
export default defineNuxtConfig({
modules: [
'nuxt-simple-sitemap',
],
})
You'll need to provide the host of your site in order to generate the sitemap.xml.
export default defineNuxtConfig({
// Recommended
runtimeConfig: {
public: {
siteUrl: process.env.NUXT_PUBLIC_SITE_URL || 'https://example.com',
}
},
// OR
sitemap: {
hostname: 'https://example.com',
},
})
By default, all static routes are included within the sitemap.xml
To enable dynamic routes to be included, you can either manually provide them via the urls
config or enable the Nitro crawler.
If your dynamic links are linked on your site, you can enable the Nitro crawler to automatically include them.
This is recommended as having internal links for all your pages is a good practice for SEO.
export default defineNuxtConfig({
nitro: {
prerender: {
crawlLinks: true,
routes: [
'/',
]
}
}
})
export default defineNuxtConfig({
sitemap: {
// provide dynamic URLs to be included
urls: async () => {
const blogPages = await getBlogPages()
return blogPages.map((page) => ({
url: `/blog/${page.slug}`,
lastmod: page.updatedAt,
changefreq: 'daily',
priority: 0.8,
}))
},
},
})
To change the behavior of sitemap.xml entries, you can use Nitro route rules.
nuxt.config.ts
export default defineNuxtConfig({
routeRules: {
// Don't add any /secret/** URLs to the sitemap.xml
'/secret/**': { index: false },
// modify the sitemap.xml entry for specific URLs
'/about': { sitemap: { changefreq: 'daily', priority: 0.3 } }
}
})
See sitemaps.org for all available options.
In development, you can visit /sitemap.preview.xml
.
If you're using the Nitro crawler, this sitemap.xml will only be a preview, as the dynamic URLs won't be resolved.
If you need further control over the sitemap.xml URLs, you can provide config on the sitemap
key.
- Type:
string
- Default:
undefined
- Required:
true
The host of your site. This is required to generate the sitemap.xml. Example: https://example.com
- Type:
boolean
- Default:
false
Whether to add a trailing slash to the URLs in the sitemap.xml.
- Type:
boolean
- Default:
true
Whether to generate the sitemap.xml.
- Type:
object
- Default:
{}
Default values for the sitemap.xml entries. See sitemaps.org for all available options.
- Type:
() => MaybePromise<SitemapEntry[]> | MaybePromise<SitemapEntry[]>
- Default:
[]
Provide custom URLs to be included in the sitemap.xml.
- Type:
string[]
- Default:
['/**']
Filter routes that match the given rules.
export default defineNuxtConfig({
sitemap: {
include: [
'/my-hidden-url'
]
}
})
- Type:
string[]
- Default:
undefined
Filter routes that match the given rules.
export default defineNuxtConfig({
sitemap: {
exclude: [
'/my-secret-section/**'
]
}
})
Additional config extends sitemap.js.
- Type:
boolean
- Default:
true
Whether to generate the sitemap.xml preview in development. It can be useful to disable if you have fetch requests to external APIs.
- Type:
boolean
- Default:
true
Will generate routes from your static page files. Useful to disable if you're using the i18n module with custom routes.
export default defineNuxtConfig({
hooks: {
'sitemap:generate': (ctx) => {
// add custom URLs
ctx.urls.push({
url: '/my-custom-url',
changefreq: 'daily',
priority: 0.3
})
}
}
})
MIT License ยฉ 2022-PRESENT Harlan Wilton