Skip to content

k.utils.sitemap ​

Build XML Sitemap

Execution ContextPrerequisitesTypical Use
KScript API (typically returning application/xml)NoneGenerating dynamic sitemap.xml feeds with multilingual alternates

k.utils.sitemap chains calls to build sitemaps.org compliant XML.

Quick Start ​

js
k.api.get("sitemap", () => {
  const xml = k.utils.sitemap.create()
    .append("https://www.example.com/", { changefreq: "daily", priority: 1.0 })
    .append("https://www.example.com/about", { lastmod: "2024-01-01", priority: 0.7 })
    .build()
  k.response.setHeader("Content-Type", "application/xml; charset=utf-8")
  k.response.write(xml)
})

Quick Reference ​

Method / PropertyDescriptionReturns
create()Create a new chainable Sitemap builder instanceSitemap
sitemap.append(loc[, options])Append a URL entry with optional metadataSitemap
sitemap.build()Serialize the accumulated entries as XMLstring

create() ​

Returns a Sitemap builder supporting chained append() and build() calls.

Parameters: None.

Returns: Sitemap builder.

js
const xml = k.utils.sitemap.create()
  .append("https://www.example.com/page1", {
    lastmod: "2024-01-01",
    changefreq: "weekly",
    priority: 0.8
  })
  .append("https://www.example.com/page2", {
    alternates: { en: "https://www.example.com/en", zh: "https://www.example.com/zh" }
  })
  .build()

append() ​

Append a URL entry to the sitemap builder.

ParameterTypeRequiredDescription
locstringYesPage absolute URL
optionsAppendOptionsNoSitemap entry options

AppendOptions ​

PropertyTypeDescription
lastmodstringLast modified date
changefreqChangefreqUpdate frequency: always, hourly, daily, weekly, monthly, yearly, never
prioritynumberPriority, from 0.0 to 1.0
alternatesRecord<string, string>Mapping from language code to URL

Returns: Sitemap, allowing chained calls.

js
k.api.get("sitemap-append", () => {
  const sitemap = k.utils.sitemap.create()
  sitemap.append("https://www.example.com/blog/hello", {
    lastmod: "2024-01-01",
    changefreq: "weekly",
    priority: 0.7
  })

  const xml = sitemap.build()
  k.response.setHeader("Content-Type", "application/xml")
  k.response.write(xml)
})

build() ​

Output the serialized XML Sitemap string. In k.api.* handlers, write raw XML with k.response.write(xml) (returning xml directly causes it to be JSON-encoded).

Parameters: None.

Returns: string.

js
k.api.get("sitemap-xml", () => {
  const xml = k.utils.sitemap.create()
    .append("https://www.example.com/", { priority: 1 })
    .build()

  k.response.setHeader("Content-Type", "application/xml")
  k.response.write(xml)
})

TypeScript Definitions ​

ts
interface KSitemap {
  create(): Sitemap;
}

interface Sitemap {
  append(loc: string, options?: AppendOptions): Sitemap;
  build(): string;
}

interface AppendOptions {
  lastmod: string;
  changefreq: Changefreq;
  priority: number;
  alternates: Record<string, string>;
}

type Changefreq = 'always' | 'hourly' | 'daily' | 'weekly' | 'monthly' | 'yearly' | 'never';