i-n-g-m-a-r

i-n-g-m-a-r

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?

Showing Posts 1 to 6

josevalim

josevalim

Creator of Elixir

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
i-n-g-m-a-r

i-n-g-m-a-r OP

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

generates:

  • 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)`
  end
end
StormByte

StormByte

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?

StormByte

StormByte

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.

LostKobrakai

LostKobrakai

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.

— All posts loaded —

Where Next? Top

Trending in Questions Top

katta
I having some trouble figuring out if I have set myself too strict of standards for my production server. Currently I can handle 75% of r...
New
achenet
Hello, I’m trying to build a basic Phoenix web-app, and I’d like to use Tailwind. However, when I launch mix phx.server, I get an error...
New
kpanic
Hi everyone, I am toying with the idea of building a “match maker” for giving personal help to people that wants to start coding. I sta...
New
Cxx-mlr
I’m working on a small exercise involving update_in/3, and I came up with this solution: data = %{ name: "Periodic Table", category:...
New
ChrisAmelia
I’ve got trouble wrapping my head around the order in which functions are called in this snippet (from Phoenix’s authentication): toke...
New
dillonoconnor
Is there any way to avoid the Hologram compiler running when using iex? It seems like the front-end code could potentially be disregarded...
New
thiagogsr
** (ArgumentError) expected :max_attempts to be a positive integer, got: {:@, [line: 10, column: 19], [{:max_attempts, [line: 10, column:...
New

Other Trending Topics Top

GenericJam
Edit: 2026 May 15 - This post is archived. Mob is alive!! Main docs: mob v0.7.11 — Documentation A bit of explanation for the slightly c...
New
mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
garrison
Hobbes is a low-level distributed database for the Elixir programming language. Hobbes provides a simple, safe, and scalable storage lay...
New
budgie
A little off-topic, but I feel like people here have a good head on their shoulders. I used to be quite good at making software. Was luc...
New
KristerV
Hey. Is there anyone here who creates agents in their apps? Not talking about using agents, but creating them. I’m finding it pretty diff...
New
mcass19
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews