Where does the metadata map defined as an option in live/4 go?

From the docs about Actions and live navigation:

:metadata - a map to optional feed metadata used on telemetry events and route info for example: %{route_name: :foo, access: :user} .

Where does the metadata map go?
Do I have access?
Can I use it to define for example which LiveComponent to render?

1 Like

No, you cannot. As the docs say, the metadata is only available on telemetry events emitted by the router. If you want to pass data to the underlying LiveView, my recommendation is to either use an action:

live "/foo", MyLive, :foo # Renders FooComponent
live "/bar", MyLive, :bar # Renders BarComponent

Or the session:

live "/foo", MyLive, session: %{"component" => :foo} # Renders FooComponent
live "/bar", MyLive, session: %{"component" => :bar} # Renders BarComponent

Example scenario for anyone who is also wondering what route helpers Phoenix.Router generates for LiveView and how to use them.

scope "/", MyAppWeb do
  pipe_through :browser

  live "/foo", FoobarLive, :qux
  live "/bar", FoobarLive, :baz


  • Routes.foobar_path/2
  • Routes.foobar_path/3
  • Routes.foobar_url/2
  • Routes.foobar_url/3

FoobarLive minus Live converted to snake case followed by _path and _url

Actions :qux and :baz can be used in two ways:

  • to know where you are
  • to know how to get somewhere else
# foobar_live.html.leex
I am here: <%= @live_action %>
Same as:   <%= assigns.live_action %>

  live_component @socket,
    MyAppWeb.Component.Navigation, id: :nav, cur: @live_action

Routes is an imported alias for MyAppWeb.Router.Helpers.
LiveComponent does not import the Routes alias.

defmodule MyAppWeb.Component.Navigation do
  use Phoenix.LiveComponent
  alias MyAppWeb.Router.Helpers, as: Routes

  def update(%{id: :nav, cur: cur} = _assigns, socket) do
    # --> `Routes.foobar_path(socket, :qux)`
    # --> `Routes.foobar_path(socket, :baz)`

Since Phoenix 1.6, the :session part seems removed, so things that worked before like

live "/bar", MyLive, session: %{"component" => :bar}

no longer work :frowning: with the following error:

Supported options include: :container, :as, :metadata, :private.

Is there an equivalent to put session data directly from the route like it was possible before?

That’s the alternative:

1 Like

Thanks for the reply!

I saw already that approach but while it is very convenient for grouping different LVs in my opinion is a little overkill when you just want to pass little data to the LV’s session from start, this is why I asked if there is a simpler way just to pass data like it could be done before.

Generally you want to minimize the data sent via the session anyways. If this is static to just a single route I’d strongly question if it cannot be statically defined in the liveview’s code. live_session is for the cases where things are dynamically added for a section of pages.