PhoenixPageMeta.Site behaviour (PhoenixPageMeta v0.1.0)

Copy Markdown View Source

Behaviour for site-wide PageMeta callbacks.

Implemented by the project's PageMeta module (e.g. MyAppWeb.PageMeta). Both callbacks have sensible defaults injected by use PhoenixPageMeta, so you only implement them if your project deviates from the convention.

Default implementations (via use PhoenixPageMeta)

  • base_url/0 — returns the configured :base_url option (string or function-of-arity-0). When omitted, guesses the Endpoint from the module's namespace (e.g. MyAppWeb.PageMetaMyAppWeb.Endpoint.url()).
  • lang_path/2 — locale-prefix swap on :path (e.g. /en/foo/de/foo).

Both are defoverridable — provide your own implementation if needed:

defmodule MyAppWeb.PageMeta do
  use PhoenixPageMeta

  @enforce_keys [:title, :path]
  defstruct [...]

  @impl PhoenixPageMeta.Site
  def lang_path(%__MODULE__{} = page_meta, locale) do
    # Custom routing scheme — e.g. query string instead of path prefix
    page_meta.path <> "?lang=" <> to_string(locale)
  end
end

Summary

Callbacks

Returns the absolute base URL of the site (e.g. "https://example.com"). Used to build canonical and og:url tags.

Returns the localized path for a page in a given locale. Used to render hreflang alternate links. Only called when a page has :supported_locales set.

Callbacks

base_url()

@callback base_url() :: String.t()

Returns the absolute base URL of the site (e.g. "https://example.com"). Used to build canonical and og:url tags.

lang_path(page_meta, locale)

@callback lang_path(page_meta :: struct(), locale :: atom()) :: String.t()

Returns the localized path for a page in a given locale. Used to render hreflang alternate links. Only called when a page has :supported_locales set.