Skip to content
Meeko
Type to search the guides and the API reference. Open it from any page with / or Ctrl K; move with the arrow keys and open a result with Enter.
Get started

Contents

On this page

Not translated yet

This page has not been translated into English yet, so it is shown in Spanish.

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.

ts
import { sitemapXml } from "@meekojs/meeko/framework";

Source code src/framework/sitemap-xml.ts:59

Firma #

ts
function sitemapXml(origin: string, urls: readonly (string | SitemapResolvedUrl)[]): string

Parameters #

origin string

Origen público, como "https://example.com".

urls readonly (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 hreflang inválido o un pathname que no empieza por /.

Example #

ts
// 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" } });
});

See also #

Was this page helpful?

Add a comment

It is sent when you choose “Yes” or “No”. Only the page, your answer and the comment are stored: no cookies and no personal data.

0 of 2000 characters

Edit on GitHub: packages/meeko/src/framework/sitemap-xml.ts