sitemapXml
función Since 1.8.0
Escribe un sitemap (protocolo sitemaps.org 0.9) con URL absolutas: origin más cada ruta, con
<lastmod>, <changefreq> y <priority> cuando la URL los tiene y un
<xhtml:link rel="alternate" hreflang> por cada una de sus alternates, con el espacio de nombres
xhtml sólo si alguna los usa. Escapa el XML y no ordena las URL. Es la misma función con la que
meeko build escribe sitemap.xml; úsala cuando el sitemap se sirve desde una base de datos
(sitemap: false en la configuración), sin escribir el XML a mano.
import { sitemapXml } from "@meekojs/meeko/framework";Source code src/framework/sitemap-xml.ts:59
Firma #
function sitemapXml(origin: string, urls: readonly (string | SitemapResolvedUrl)[]): stringParameters #
-
originstring Origen público, como
"https://example.com".-
urlsreadonly (string | SitemapResolvedUrl)[] Rutas (
pathname) o URL con sus campos y versiones en otros idiomas.
Returns #
-
string El documento XML.
Throws #
-
Error Si una alternativa tiene un
hreflanginválido o unpathnameque no empieza por/.
Example #
// server/middleware/sitemap.ts
import { sitemapXml } from "@meekojs/meeko/framework";
import { defineServerMiddleware } from "@meekojs/meeko/server";
export default defineServerMiddleware(async (request, next) => {
if (new URL(request.url).pathname !== "/sitemap.xml") return next(request);
const pages = await db.pages.list();
const xml = sitemapXml("https://example.com", pages.map((page) => ({
pathname: page.path,
lastmod: page.updatedAt,
alternates: [
{ hreflang: "es", pathname: page.path },
{ hreflang: "en", pathname: `/en${page.path}` },
{ hreflang: "x-default", pathname: page.path },
],
})));
return new Response(xml, { headers: { "content-type": "application/xml; charset=utf-8" } });
});