Arrow keys: ← previous · → next 27 of 35
@nodedk/sitemap
0.3.3 stable

sitemap#

Generate an XML sitemap as a Node.js readable stream.

Install#

npm i @nodedk/sitemap

Usage#

var sitemap = require('@nodedk/sitemap')
var { text } = require('node:stream/consumers')

async function main() {
  var stream = sitemap({
    hostname: 'https://example.com',
    urls: [
      { url: '/' },
      {
        url: '/about',
        lastmod: new Date('2026-08-26'),
        priority: 0.8
      }
    ]
  })

  var xml = await text(stream)
  console.log(xml)
}

main().catch(console.error)

API#

sitemap({ hostname, urls = [] })#

Returns a Node.js readable byte stream synchronously. The options object is required; absolute entry URLs can be used without a hostname. Options:

  • hostname: Base URL used to create absolute sitemap URLs.
  • urls: URL strings or sitemap entry objects. Defaults to an empty array.

Each entry accepts:

  • url: URL to include in the sitemap.
  • lastmod: Optional Date or parseable date string. Invalid dates are omitted.
  • priority: Optional priority value.

Localized pages need no special configuration. Include every localized page as an entry in the urls array.

String entries are converted to { url: entry }. Entries without a usable URL are ignored.

Examples#

Pipe the stream to a file:

var sitemap = require('@nodedk/sitemap')
var { createWriteStream } = require('node:fs')
var { mkdir } = require('node:fs/promises')
var { pipeline } = require('node:stream/promises')

async function main() {
  var urls = ['/', '/about']
  await mkdir('dist', { recursive: true })
  await pipeline(
    sitemap({ hostname: 'https://example.com', urls }),
    createWriteStream('dist/sitemap.xml')
  )
}

main().catch(console.error)

The sitemap protocol limits one sitemap to 50,000 URLs. This library does not enforce that limit or create sitemap indexes.

Created by Vidar Eldøy