k.utils.sitemap
Build XML Sitemap
| Execution Context | Prerequisites | Typical Use |
|---|---|---|
KScript API (typically returning application/xml) | None | Generating 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 / Property | Description | Returns |
|---|---|---|
create() | Create a new chainable Sitemap builder instance | Sitemap |
sitemap.append(loc[, options]) | Append a URL entry with optional metadata | Sitemap |
sitemap.build() | Serialize the accumulated entries as XML | string |
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.
| Parameter | Type | Required | Description |
|---|---|---|---|
loc | string | Yes | Page absolute URL |
options | AppendOptions | No | Sitemap entry options |
AppendOptions
| Property | Type | Description |
|---|---|---|
lastmod | string | Last modified date |
changefreq | Changefreq | Update frequency: always, hourly, daily, weekly, monthly, yearly, never |
priority | number | Priority, from 0.0 to 1.0 |
alternates | Record<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';