@nuxtjs/sitemap
module. You can add it as a dependency to your project by running the following command:yarn add @nuxtjs/sitemap
or with npm:
npm install @nuxtjs/sitemap
@nuxtjs/sitemap
to the modules
section of your nuxt.config.js
file, and configure it according to your needs:export default {
modules: [
'@nuxtjs/sitemap'
],
sitemap: {
path: '/sitemap.xml',
hostname: 'https://yourwebsite.com',
cacheTime: 1000 * 60 * 15,
gzip: true,
generate: false, // Enable me when using nuxt generate
exclude: [
'/secret',
'/admin/**'
],
routes: [
'/page/1',
{
url: '/page/2',
changefreq: 'daily',
priority: 1,
lastmodISO: '2017-06-30T13:30:00.000Z'
}
]
}
}
In the above configuration:
- path
is the path where your sitemap will be available.
- hostname
is the URL of your site.
- cacheTime
is the time in milliseconds before the sitemap is regenerated.
- gzip
enables gzip compression for your sitemap.
- exclude
is an array of routes to exclude from the sitemap.
- routes
is an array of routes to include in the sitemap. Each route can be a string or an object with url
, changefreq
, priority
, and lastmodISO
properties.
Example 1: A basic sitemap configuration
Here's a simple example of how to configure a sitemap for a Nuxt.js application:
export default {
modules: [
'@nuxtjs/sitemap'
],
sitemap: {
path: '/sitemap.xml',
hostname: 'https://yourwebsite.com',
routes: [
'/',
'/about',
'/contact'
]
}
}
In this example, the sitemap will be available at https://yourwebsite.com/sitemap.xml
and will include the /
, /about
, and /contact
routes.
In this tutorial, we have learned how to install and configure the Sitemap module in a Nuxt.js application. We've also seen how to include and exclude specific routes from the sitemap.
For more information on the Sitemap module, check out the official documentation.
Exercise 1: Create a sitemap for a Nuxt.js application with /home
, /about
, and /products
routes.
Solution:
export default {
modules: [
'@nuxtjs/sitemap'
],
sitemap: {
path: '/sitemap.xml',
hostname: 'https://yourwebsite.com',
routes: [
'/home',
'/about',
'/products'
]
}
}
Exercise 2: Exclude the /admin
and /secret
routes from the sitemap.
Solution:
export default {
modules: [
'@nuxtjs/sitemap'
],
sitemap: {
path: '/sitemap.xml',
hostname: 'https://yourwebsite.com',
exclude: [
'/admin',
'/secret'
],
routes: [
'/home',
'/about',
'/products'
]
}
}
For further practice, try to generate a sitemap for a larger application with more routes.