PhoenixKit.Modules.Sitemap.Generator (phoenix_kit v1.7.63)

Copy Markdown View Source

Main sitemap generator for PhoenixKit.

Generates a <sitemapindex> at /sitemap.xml referencing per-module sitemap files at /sitemaps/sitemap-{source}.xml.

Architecture

  • /sitemap.xml - Always a <sitemapindex> referencing per-module files
  • /sitemaps/sitemap-static.xml - Static pages
  • /sitemaps/sitemap-routes.xml - Router discovery
  • /sitemaps/sitemap-publishing.xml - Publishing posts (or per-blog files)
  • /sitemaps/sitemap-shop.xml - Shop products (auto-split at 50k)
  • /sitemaps/sitemap-entities.xml - Entities (or per-type files)

HTML sitemaps are rendered by PhoenixKit.Modules.Sitemap.HtmlGenerator.

Usage

# Generate all sitemaps (index + per-module files)
{:ok, result} = Generator.generate_all(base_url: "https://example.com")

# Generate HTML sitemap (collects from all sources)
{:ok, html} = Generator.generate_html(base_url: "https://example.com")

# Backward compatible: generate_xml returns the sitemapindex
{:ok, xml} = Generator.generate_xml(base_url: "https://example.com")

Summary

Functions

Collects URL entries from all enabled sources.

Generates all sitemaps: per-module files and the sitemapindex.

Generates HTML sitemap from all enabled sources.

Builds <sitemapindex> XML from a list of %SitemapFile{} structs.

Generates sitemap file(s) for a single source module.

Generates XML sitemap. Returns the sitemapindex XML.

Gets a specific sitemap part by index (1-based).

Invalidates cache AND triggers async regeneration.

Invalidates all cached sitemaps.

Functions

collect_all_entries(opts \\ [], sources \\ get_sources())

@spec collect_all_entries(
  keyword(),
  [module()]
) :: [PhoenixKit.Modules.Sitemap.UrlEntry.t()]

Collects URL entries from all enabled sources.

When the Languages module is enabled, automatically collects entries for all enabled languages and adds hreflang alternate links.

generate_all(opts \\ [])

@spec generate_all(keyword()) :: {:ok, map()} | {:error, any()}

Generates all sitemaps: per-module files and the sitemapindex.

Options

  • :base_url - Base URL for building full URLs (required)
  • :xsl_style - XSL stylesheet style: "table" or "minimal" (default: "table")
  • :xsl_enabled - Enable XSL stylesheet reference (default: true)

Returns

{:ok, %{
  index_xml: "<?xml ...sitemapindex...",
  modules: [
    %{filename: "sitemap-static", url_count: 3, lastmod: ~U[...]}
  ],
  total_urls: 150
}}

generate_html(opts \\ [])

@spec generate_html(keyword()) :: {:ok, String.t()} | {:error, any()}

Generates HTML sitemap from all enabled sources.

Delegates to PhoenixKit.Modules.Sitemap.HtmlGenerator.

Options

  • :base_url - Base URL for building full URLs (required)
  • :style - Display style: "hierarchical", "grouped", or "flat" (default: "hierarchical")
  • :cache - Enable/disable caching (default: true)
  • :title - Page title (default: "Sitemap")

generate_index(module_infos, base_url, xsl_style \\ "table", xsl_enabled \\ true)

@spec generate_index(
  [PhoenixKit.Modules.Sitemap.SitemapFile.t()],
  String.t(),
  String.t(),
  boolean()
) ::
  String.t()

Builds <sitemapindex> XML from a list of %SitemapFile{} structs.

generate_module(source_module, opts \\ [])

@spec generate_module(
  module(),
  keyword()
) :: [PhoenixKit.Modules.Sitemap.SitemapFile.t()]

Generates sitemap file(s) for a single source module.

Returns a list of %SitemapFile{} structs (one per file generated). Empty sources produce no files and return [].

generate_xml(opts \\ [])

@spec generate_xml(keyword()) ::
  {:ok, String.t()} | {:ok, String.t(), [map()]} | {:error, any()}

Generates XML sitemap. Returns the sitemapindex XML.

Delegates to generate_all/1 and returns the index XML for backward compatibility.

get_sitemap_part(index)

@spec get_sitemap_part(integer()) :: {:ok, String.t()} | {:error, :not_found}

Gets a specific sitemap part by index (1-based).

Legacy function for backward compatibility with old numbered sitemap parts.

invalidate_and_regenerate()

@spec invalidate_and_regenerate() :: {:ok, Oban.Job.t()} | {:error, term()}

Invalidates cache AND triggers async regeneration.

invalidate_cache()

@spec invalidate_cache() :: :ok

Invalidates all cached sitemaps.