Skip to content

k.utils.sitemap ​

构建 XML Sitemap

概述 ​

k.utils.sitemap 链式生成 sitemaps.org 格式 XML。

create() ​

返回可 append 的构建器。

参数: 无。

返回: Sitemap 构建器。

ts
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() ​

参数类型必填说明
locstring是页面绝对 URL
optionsAppendOptions否Sitemap 条目配置

AppendOptions ​

属性类型说明
lastmodstring最后修改日期
changefreqstring更新频率:always、hourly、daily、weekly、monthly、yearly、never
prioritynumber优先级,范围 0.0 到 1.0
alternatesRecord<string, string>语言到 URL 的映射

返回: Sitemap,可继续链式调用。

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

return sitemap.build()

build() ​

输出 XML 字符串。

参数: 无。

返回: string。

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

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

相关文档 ​