Skip to content

NicoPennec/nuxt-sitemap

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

6 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

Sitemap

Generate a sitemap.xml with no pain!

Module based on the awesome sitemap ❀️

Setup

  • Add @nuxtjs/sitemap dependency using yarn or npm to your project
  • Add @nuxtjs/sitemap module to nuxt.config.js
  modules: [
   '@nuxtjs/sitemap'
  ]
  • Add additional options to sitemap section of nuxt.config.js to override defaults
  sitemap: {
    path: '/sitemap.xml',
    hostname: 'https://example.com',
    excludes: [
      '/secret',
      '/admin/**'
    ]
    routes: [
      '/page/1',
      {
        url: '/page/2',
        changefreq: 'daily',
        priority: 1,
        lastmodISO: '2017-06-30T13:30:00.000Z'
      }
    ]
  }

The excludes parameter is an array of glob patterns to exclude static routes from the generated sitemap.

The routes parameter follows the same way than the generate configuration: https://nuxtjs.org/api/configuration-generate/

See as well the routes examples bellow.

Routes

Dynamic routes are ignored by the sitemap module.

Example:

-| pages/
---| index.vue
---| users/
-----| _id.vue

Only the route / will be added to sitemap by the module.

If you want the module to add routes with dynamic params, you need to set an array of dynamic routes.

We add routes for /users/:id in nuxt.config.js:

  sitemap: {
    routes: [
      '/users/1',
      '/users/2',
      '/users/3'
    ]
  }

Function which returns a Promise

nuxt.config.js

const axios = require('axios')

module.exports = {
  sitemap: {
    routes: function () {
      return axios.get('https://my-api/users')
      .then((res) => {
        return res.data.map((user) => {
          return '/users/' + user.id
        })
      })
    }
  }
}

Function with a callback

nuxt.config.js

const axios = require('axios')

module.exports = {
  sitemap: {
    routes: function (callback) {
      axios.get('https://my-api/users')
      .then((res) => {
        var routes = res.data.map((user) => {
          return '/users/' + user.id
        })
        callback(null, routes)
      })
      .catch(callback)
    }
  }
}

About

A Nuxt module to generate a sitemap.xml | the project has moved to the Nuxt-Community πŸš€ https://github.com/nuxt-community/sitemap-module

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published