PhoenixDatastar.Router (PhoenixDatastar v0.1.14)

Copy Markdown View Source

Router macros for PhoenixDatastar.

Usage

import PhoenixDatastar.Router

scope "/", MyAppWeb do
  pipe_through :browser

  datastar_session :default do
    datastar "/counter", CounterStar
  end
end

datastar/3 generates per-view routes:

For live views, a global SSE stream endpoint (PhoenixDatastar.StreamPlug) and a navigation endpoint (PhoenixDatastar.NavPlug) must also be added to the router. See the README for the full setup.

Summary

Functions

Defines routes for a PhoenixDatastar view.

Groups datastar routes under shared stream/navigation behavior.

Functions

datastar(path, view, opts \\ [])

(macro)

Defines routes for a PhoenixDatastar view.

The macro auto-detects whether the view is stateless (use PhoenixDatastar) or live (use PhoenixDatastar, :live) and generates appropriate routes.

Options

  • :html_module - The Phoenix HTML module to use for rendering the mount template. Defaults to Application.get_env(:phoenix_datastar, :html_module).

datastar_session(name, opts \\ [], list)

(macro)

Groups datastar routes under shared stream/navigation behavior.

Routes within a datastar_session block are registered in the PhoenixDatastar.RouteRegistry for soft navigation matching. When a user navigates between views in the same session, the existing SSE connection is reused and the view is swapped in-place without a full page reload.

Soft navigation only works between live views (use PhoenixDatastar, :live) within the same session. Stateless views always trigger a full page reload.

Options

  • :root_selector - CSS selector for the container element patched during soft navigation. Defaults to "#app".
  • :stream_guard - MFA tuple {Module, :function} stored as route metadata. Reserved for future use — not currently enforced by StreamPlug or NavPlug. Use pipeline plugs for authorization instead.
  • :nav_guard - MFA tuple {Module, :function} stored as route metadata. Defaults to :stream_guard. Reserved for future use — not currently enforced.