[]previous and next page

sitemapXml

The XML of one sitemap file, written as Next writes it: one element per line, the namespaces in Next's order, a final newline.

The XML of one sitemap file, written as Next writes it: one element per line, the namespaces in Next's order, a final newline. Every URL is absolute (relative values resolve against base); dates, priorities and change frequencies are checked; the file is refused past 50 000 URLs or 50 MiB. The children of a url follow the sitemaps.org order (loc, lastmod, changefreq, priority), then the extensions.

With localize (opt-in), the entries are rewritten first: localizedSitemap(i18n.routing) from the @shadow-js/dusk/i18n subpath lists each app path once per locale, every copy with its xhtml:link rel="alternate" hreflang links. What it returns is checked like any entry, and counts toward the limits.

Import#

ts
import { sitemapXml } from "@shadow-js/dusk/server";

Signature#

ts
function sitemapXml(
  entries: readonly SitemapEntry[],
  options: {
    readonly base?: URL | undefined;
    /** The app's trailing-slash policy: entries on the base's origin follow it (`preserve` when omitted). */
    readonly trailingSlash?: TrailingSlashPolicy | undefined;
    /** Largest file in UTF-8 bytes. Default {@link MAX_SITEMAP_BYTES}; a lower value is for a smaller budget or a test. */
    readonly maxBytes?: number | undefined;
    /**
     * Rewrites the entries before they are written (opt-in), with the base and the trailing-slash policy:
     * `localizedSitemap(i18n.routing)` lists each app path once per locale with its `hreflang` alternates. Absent: the
     * entries are written as given.
     */
    readonly localize?:
      | ((
          entries: readonly SitemapEntry[],
          context: { readonly base: URL | undefined; readonly trailingSlash: TrailingSlashPolicy },
        ) => readonly SitemapEntry[])
      | undefined;
  } = {},
): string

Parameters#

Name Description
entries The URLs.
options base: the origin relative URLs resolve against. localize: rewrites the entries (locale copies).

Returns#

The XML document.

Throws#

  • TypeError, RangeError for a value the protocol does not accept, or a file over its limits.

Examples#

ts
sitemapXml([{ url: "/", changeFrequency: "daily", priority: 1 }], { base: new URL("https://example.com") });
sitemapXml([{ url: "about" }], { base: new URL("https://example.com"), localize: localizedSitemap(i18n.routing) });