Git Product home page Git Product logo

sitemap-module's Introduction

Sitemap Module

npm (scoped with tag) npm CircleCI Codecov Dependencies js-standard-style

Automatically generate or serve dynamic sitemap.xml for Nuxt.js projects!

๐Ÿ“– Release Notes

Module based on the awesome sitemap package โค๏ธ

Setup

  • Add @nuxtjs/sitemap dependency using yarn or npm to your project

  • Add @nuxtjs/sitemap module to nuxt.config.js

  modules: [
   '@nuxtjs/sitemap'
  ]

notice: If you use other modules (eg. nuxt-i18n), always declare the sitemap module at end of array (eg. modules: ['nuxt-i18n', '@nuxtjs/sitemap'])

  • Add additional options to sitemap section of nuxt.config.js to override defaults
  sitemap: {
    path: '/sitemap.xml',
    hostname: 'https://example.com',
    cacheTime: 1000 * 60 * 15,
    gzip: true,
    exclude: [
      '/secret',
      '/admin/**'
    ],
    routes: [
      '/page/1',
      {
        url: '/page/2',
        changefreq: 'daily',
        priority: 1,
        lastmodISO: '2017-06-30T13:30:00.000Z'
      }
    ]
  }

Options

exclude

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

routes

The routes parameter follows the same way than the generate configuration.

See as well the routes examples below.

path

  • Default: /sitemap.xml

Where serve/generate sitemap file

hostname

  • Default:
    • hostname() for generate mode
    • Dynamically based on request url for middleware mode

This values is mandatory for generation sitemap file, and you should explicitly provide it for generate mode.

cacheTime

  • Default: 1000 * 60 * 15 (15 Minutes)

Defines how frequently should sitemap routes being updated. Please note that after each invalidation, routes will be evaluated again. (See routes section)

filter

  • Default: undefined

If filter option is set as a function, all routes will be filtered through it.

Examples:

nuxt.config.js

// filter routes by language

module.exports = {
  sitemap: {
    filter ({ routes, options }) {
      if (options.hostname === 'example.com') {
        return routes.filter(route => route.locale === 'en')
      }
      return routes.filter(route => route.locale === 'fr')
    }
  }
}
// add a trailing slash to each route

module.exports = {
  sitemap: {
    filter ({ routes }) {
      return routes.map(route => route.url = `${route.url}/`)
    }
  }
}

gzip

  • Default: false

Enable the creation of the .xml.gz sitemap compressed with gzip.

Routes

Dynamic routes are ignored by the sitemap module.

Example:

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

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 () {
      return axios.get('https://jsonplaceholder.typicode.com/users')
      .then(res => res.data.map(user =>  '/users/' + user.username))
    }
  }
}

Function with a callback

This feature is deprecated. Use a promise-based approach instead.

nuxt.config.js

const axios = require('axios')

module.exports = {
  sitemap: {
    routes (callback) {
      axios.get('https://jsonplaceholder.typicode.com/users')
      .then(res => {
        let routes = res.data.map(user => '/users/' + user.username)
        callback(null, routes)
      })
      .catch(callback)
    }
  }
}

License

MIT License

Contributors

sitemap-module's People

Contributors

adam-lynch avatar ansidev avatar atinux avatar delucis avatar dohomi avatar mannil avatar nicopennec avatar renovate[bot] avatar ricardogobbosouza avatar trurlmcbyte avatar

Watchers

 avatar  avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.