PhoenixKit.Modules.Sitemap.FileStorage (phoenix_kit v1.7.33)

Copy Markdown View Source

File-based storage for sitemap XML.

Sitemaps are saved to priv/static/sitemap.xml for direct nginx/CDN serving. This is the primary storage - no ETS caching layer needed.

Key Features

  • Direct nginx serving - Files in priv/static/ can be served without Phoenix
  • ETag from mtime - Use get_file_stat/0 for cache validation
  • On-demand generation - First request generates if file missing

Usage

# Save sitemap XML
FileStorage.save(xml_content)

# Load sitemap XML
case FileStorage.load() do
  {:ok, xml} -> send_resp(conn, 200, xml)
  :error -> generate_and_serve(conn)
end

# Check if file exists
FileStorage.exists?()

# Get file stats for ETag
{:ok, mtime, size} = FileStorage.get_file_stat()

# Delete to force regeneration
FileStorage.delete()

Summary

Functions

Clears the sitemap file.

Deletes the sitemap file to force regeneration.

Checks if the sitemap file exists.

Returns the file path for the sitemap.

Returns file stats for ETag generation.

Loads XML content from the sitemap file.

Saves XML content to the sitemap file.

Returns the storage directory path.

Functions

clear_all()

@spec clear_all() :: :ok

Clears the sitemap file.

Alias for delete/0 for API consistency.

delete()

@spec delete() :: :ok

Deletes the sitemap file to force regeneration.

Next request will trigger fresh generation.

Examples

iex> FileStorage.delete()
:ok

exists?()

@spec exists?() :: boolean()

Checks if the sitemap file exists.

Examples

iex> FileStorage.exists?()
true

file_path()

@spec file_path() :: String.t()

Returns the file path for the sitemap.

Path: priv/static/sitemap.xml (allows nginx direct serving)

get_file_stat()

@spec get_file_stat() :: {:ok, tuple(), non_neg_integer()} | :error

Returns file stats for ETag generation.

Uses mtime and size for cache validation - more reliable than database-based config values.

Examples

iex> FileStorage.get_file_stat()
{:ok, {{2025, 1, 15}, {10, 30, 0}}, 12345}

iex> FileStorage.get_file_stat()  # file doesn't exist
:error

load()

@spec load() :: {:ok, String.t()} | :error

Loads XML content from the sitemap file.

Examples

iex> FileStorage.load()
{:ok, "<?xml ..."}

iex> FileStorage.load()  # when file doesn't exist
:error

save(xml_content)

@spec save(String.t()) :: :ok | {:error, term()}

Saves XML content to the sitemap file.

Creates the storage directory if it doesn't exist.

Examples

iex> FileStorage.save("<?xml ...")
:ok

storage_dir()

@spec storage_dir() :: String.t()

Returns the storage directory path.

Uses priv/static for nginx/CDN compatibility.