Code Monkey home page Code Monkey logo

nuxt-simple-sitemap's Introduction

nuxt-simple-sitemap

NPM version NPM Downloads GitHub 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 yarn
yarn 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.

Automatic dynamic URLs - Recommended

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

nuxt-simple-sitemap's People

Contributors

harlan-zw avatar damevin 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.