Introduction

Nuxt Sitemap comes with an integration for Nuxt Content that allows you to configure your sitemap entry straight from your markdown directly.

Setup Nuxt Content v3

In Nuxt Content v3 we need to use the asSitemapCollection() function to augment any collections to be able to use the sitemap frontmatter key.

content.config.ts
import { defineCollection, defineContentConfig } from '@nuxt/content'
import { asSitemapCollection } from '@nuxtjs/sitemap/content'

export default defineContentConfig({
  collections: {
    content: defineCollection(
      // adds the robots frontmatter key to the collection
      asSitemapCollection({
        type: 'page',
        source: '**/*.md',
      }),
    ),
  },
})

Due to current Nuxt Content v3 limitations, you must load the sitemap module before the content module.

export default defineNuxtConfig({
  modules: [
    '@nuxtjs/sitemap',
    '@nuxt/content' // <-- Must be after @nuxtjs/sitemap
  ]
})

Setup Nuxt Content v2

In Nuxt Content v2 markdown files require either Document Driven Mode, a path key to be set in the frontmatter or the strictNuxtContentPaths option to be enabled.

nuxt.config.ts
export default defineNuxtConfig({
  // things just work!
  content: {
    documentDriven: true
  }
})

If you're not using documentDriven mode and your content paths are the same as their real paths, you can enable strictNuxtContentPaths to get the same behaviour.

nuxt.config.ts
export default defineNuxtConfig({
  sitemap: {
    strictNuxtContentPaths: true
  }
})

Advanced: Nuxt Content App Source

If you'd like to set up a more automated Nuxt Content integration and you're not using Document Driven mode, you can add content to the sitemap as you would with Dynamic URLs.

An example of what this might look like is below, customize to your own needs.

server/api/__sitemap__/urls.ts
import { defineEventHandler } from 'h3'
import type { ParsedContent } from '@nuxt/content/dist/runtime/types'
import { serverQueryContent } from '#content/server'
import { asSitemapUrl, defineSitemapEventHandler } from '#imports'

export default defineSitemapEventHandler(async (e) => {
  const contentList = (await serverQueryContent(e).find()) as ParsedContent[]
  return contentList
    .filter(c => c._path.startsWith('_articles'))
    .map((c) => {
      return asSitemapUrl({
        loc: `/blog/${c._path.replace('_articles', '')}`,
        lastmod: updatedAt
      })
    })
})
export default defineNuxtConfig({
  sitemap: {
    sources: [
      '/api/__sitemap__/urls'
    ]
  }
})

Usage

Frontmatter sitemap

Use the sitemap key in your frontmatter to add a page to your sitemap.

You can provide any data that you would normally provide in the sitemap configuration.

---
sitemap:
  loc: /my-page
  lastmod: 2021-01-01
  changefreq: monthly
  priority: 0.8
---

# My Page

Exclude from Sitemap

If you'd like to exclude a page from the sitemap, you can set sitemap: false in the frontmatter or robots: false if you'd like to exclude it from search engines.

---
sitemap: false
robots: false
---
Did this page help you?