Guides

Data Sources

Last updated by
Harlan Wilton
in feat: runtime sources (#450).

Every URL within your sitemap will belong to a source. Sources determine where your sitemap URLs come from and how they're managed.

Sources are categorized into two types:

  • Application Sources: Automatically generated from your application
  • User Sources: Manually configured by you

Application Sources

Application sources are automatically generated from your Nuxt application. They provide convenience by automatically discovering URLs from your app's structure, but can be disabled if they don't match your needs.

  • nuxt:pages - Statically analysed pages of your application
  • nuxt:prerender - URLs that were prerendered
  • nuxt:route-rules - URLs from your route rules
  • @nuxtjs/i18n:pages - When using the pages config with Nuxt I18n. See Nuxt I18n for more details.
  • @nuxt/content:document-driven - When using Document Driven mode. See Nuxt Content for more details.

Disabling Application Sources

You can disable application sources individually or all at once using the excludeAppSources config option.

export default defineNuxtConfig({
  sitemap: {
    // exclude all app sources
    excludeAppSources: true,
  }
})

User Sources

User sources allow you to manually configure where your sitemap URLs come from. These are especially useful for dynamic routes that aren't using prerendering discovery.

You have several options for providing user sources:

1. Build-time Sources with urls Function

For sitemap data that only needs to be updated at build time, the urls function is the simplest solution. This function runs once during sitemap generation.

nuxt.config.ts
export default defineNuxtConfig({
  sitemap: {
    urls: async () => {
      // fetch your URLs from a database or other source
      const urls = await fetch('https://example.com/api/urls')
      return urls
    }
  }
})

2. Runtime Sources with sources Array

For sitemap data that must always be up-to-date at runtime, use the sources array. Each source is a URL that gets fetched and should return either:

  • JSON array of sitemap URL entries
  • XML sitemap document
export default defineNuxtConfig({
  sitemap: {
    sources: [
      // create our own API endpoints
      '/api/__sitemap__/urls',
      // use a static remote file
      'https://cdn.example.com/my-urls.json',
      // hit a remote API with credentials
      ['https://api.example.com/pages/urls', { headers: { Authorization: 'Bearer <token>' } }]
    ]
  }
})

You can provide multiple sources, but consider implementing your own caching strategy for performance.

Learn more about working with dynamic data in the Dynamic URLs guide.

3. Dynamic Sources Using Nitro Hooks

For advanced use cases, you can dynamically add or modify sources at runtime using the sitemap:sources Nitro hook. This is useful for:

  • Adding sources based on request context
  • Forwarding authentication headers
  • Modifying source configurations on the fly
server/plugins/sitemap.ts
import { defineNitroPlugin } from 'nitropack/runtime'
import { getHeader } from 'h3'

export default defineNitroPlugin((nitroApp) => {
  nitroApp.hooks.hook('sitemap:sources', async (ctx) => {
    // Add a new source dynamically
    ctx.sources.push('/api/runtime-urls')
    
    // Modify existing sources to add headers
    ctx.sources = ctx.sources.map(source => {
      if (typeof source === 'object' && source.fetch) {
        const [url, options = {}] = Array.isArray(source.fetch) ? source.fetch : [source.fetch, {}]
        
        // Forward authorization header from original request
        const authHeader = getHeader(ctx.event, 'authorization')
        if (authHeader) {
          options.headers = options.headers || {}
          options.headers['Authorization'] = authHeader
        }
        
        source.fetch = [url, options]
      }
      return source
    })
  })
})

Learn more about the sitemap hooks in the Nitro Hooks documentation.

Did this page help you?