JSPM

  • Created
  • Published
  • Downloads 5694
  • Score
    100M100P100Q15099F
  • License MIT

Package Exports

  • nuxt-simple-sitemap

Readme

nuxt-simple-sitemap

NPM version NPM Downloads GitHub stars

A simple sitemap module for pre-rendered Nuxt v3 apps.


Status: Early Access
Please report any issues 🐛
Made possible by my Sponsor Program 💖
Follow me @harlan_zw 🐦 • Join Discord for help

Features

  • 🔄 Route config using route rules
  • 🪝 Easily hook into the sitemap generation
  • 📦 Uses sitemap.js

Install

npm install --save-dev nuxt-simple-sitemap

# Using yarn
yarn add --dev nuxt-simple-sitemap

Setup

nuxt.config.ts

export default defineNuxtConfig({
  modules: [
    'nuxt-simple-sitemap',
  ],
})

To have routes included in the sitemap.xml automatically, they need to be pre-rendered by Nitro.

export default defineNuxtConfig({
  nitro: {
    prerender: {
      crawlLinks: true,
      routes: [
        '/',
        // any URLs that can't be discovered by crawler
        '/my-hidden-url'
      ]
    }
  }
})

Note: The sitemap.xml will only be generated once you build your site.

Set host

You'll need to provide the host of your site in order to generate the sitemap.xml.

export default defineNuxtConfig({
  // Recommended 
  runtimeConfig: {
    siteUrl: 'https://example.com',
  },
  // OR 
  sitemap: {
    hostname: 'https://example.com',
  },
})

Route Rules

To change the behavior of the sitemap, you can use route rules. Route rules are provided as Nitro route rules.

nuxt.config.ts

export default defineNuxtConfig({
  routeRules: {
    // Don't add any /secret/** URLs to the sitemap  
    '/secret/**': { index: false },
    // modify the sitemap entry for specific URLs
    '/about': { sitemap: { changefreq: 'daily', priority: 0.3 } }
  }
})

The following options are available for each route rule:

  • index: Whether to include the route in the sitemap.xml. Defaults to true.
  • sitemap.changefreq: The change frequency of the route.
  • sitemap.priority: The priority of the route.

Module Config

If you need further control over the sitemap URLs, you can provide config on the sitemap key.

enabled

  • Type: boolean
  • Default: true

Whether to generate the sitemap.xml.

include

  • Type: string[]
  • Default: undefined

Filter routes that match the given rules.

export default defineNuxtConfig({
  sitemap: {
    include: [
      '/my-hidden-url'
    ]
  }
})

exclude

  • Type: string[]
  • Default: undefined

Filter routes that match the given rules.

export default defineNuxtConfig({
  sitemap: {
    exclude: [
        '/my-secret-section/**'
    ]
  }
})

Additional config extends sitemap.js.

Examples

Add custom routes without pre-rendering

export default defineNuxtConfig({
  hooks: {
      'sitemap:generate': (ctx) => {
          // add custom URLs
          ctx.urls.push({
              url: '/my-custom-url',
              changefreq: 'daily',
              priority: 0.3
          })
      }
  }
})

Sponsors

License

MIT License © 2022-PRESENT Harlan Wilton