Sitemap Module in Nuxt.js

Tutorial 4 of 5

Tutorial: Sitemap Module in Nuxt.js

1. Introduction

  • Goal of the Tutorial: This tutorial aims to introduce you to the Sitemap module in Nuxt.js and teach you how to use it to generate a sitemap for your Nuxt.js application.
  • Learning outcomes: By the end of this tutorial, you'll be able to use the Sitemap module effectively to create dynamic sitemaps for your application, which will help search engines navigate through your site.
  • Prerequisites: A basic understanding of Nuxt.js and JavaScript is required to follow this tutorial.

2. Step-by-Step Guide

  1. Installation: To begin, you'll need to install the @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
  1. Configuration: Once installed, you need to add @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.

3. Code Examples

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.

4. Summary

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.

5. Practice Exercises

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.