simple-sitemap

Go to documentation

Simple, minimal sitemap.xml generation.

nuxt-simple-sitemap

NPM versionNPM DownloadsGitHub stars

A simple sitemap.xml module for Nuxt 3.


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

ℹ️ Looking for a complete SEO solution? Check out Nuxt SEO Kit.

Features

  • 🪝 Minimal config, powerful API
  • 🔄 Route config using route rules
  • 🏞️ Handle trailing slashes
  • 📦 Uses sitemap.js

Install

npm install --save-dev nuxt-simple-sitemap# Using yarnyarn add --dev nuxt-simple-sitemap

Setup

nuxt.config.ts

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

Set host

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

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

Usage

Handling dynamic routes

By default, all static routes are included within the sitemap.xml

To enable dynamic routes to be included, you can either manually provide them via the urls config or enable the Nitro crawler.

If your dynamic links are linked on your site, you can enable the Nitro crawler to automatically include them.

This is recommended as having internal links for all your pages is a good practice for SEO.

export default defineNuxtConfig({  nitro: {    prerender: {      crawlLinks: true,      routes: [        '/',      ]    }  }})

Manual dynamic URLs

export default defineNuxtConfig({  sitemap: {    // provide dynamic URLs to be included     urls: async () => {      const blogPages = await getBlogPages()      return blogPages.map((page) => ({          url: `/blog/${page.slug}`,          lastmod: page.updatedAt,          changefreq: 'daily',          priority: 0.8,      }))    },  },})

Configure sitemap.xml entries

To change the behavior of sitemap.xml entries, you can use Nitro route rules.

nuxt.config.ts

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

See sitemaps.org for all available options.

Previewing sitemap

In development, you can visit /sitemap.preview.xml.

If you're using the Nitro crawler, this sitemap.xml will only be a preview, as the dynamic URLs won't be resolved.

Module Config

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

hostname

  • Type: string
  • Default: undefined
  • Required: true

The host of your site. This is required to generate the sitemap.xml. Example: https://example.com

trailingSlash

  • Type: boolean
  • Default: false

Whether to add a trailing slash to the URLs in the sitemap.xml.

enabled

  • Type: boolean
  • Default: true

Whether to generate the sitemap.xml.

defaults

  • Type: object
  • Default: {}

Default values for the sitemap.xml entries. See sitemaps.org for all available options.

urls

  • Type: () => MaybePromise<SitemapEntry[]> | MaybePromise<SitemapEntry[]>
  • Default: []

Provide custom URLs to be included in the sitemap.xml.

include

  • Type: string[]
  • Default: ['/**']

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.

devPreview

  • Type: boolean
  • Default: true

Whether to generate the sitemap.xml preview in development. It can be useful to disable if you have fetch requests to external APIs.

inferStaticPagesAsRoutes

  • Type: boolean
  • Default: true

Will generate routes from your static page files. Useful to disable if you're using the i18n module with custom routes.

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