Add assigns option in live route to be passed to the LiveView

Currently I’m not finding a good way to pass data from the defined live route to the mounted LiveView.

The 2 approaches currently available are:

  • the session option in the live_session. Cons here is that is per live route
  • the metadata option that we can access in the handle_params/3 callback. Cons here is that if we need some logic using the metadata data, we can do it in the mount/3 callback, only on handle_params/3

My proposal is to add an assigns option to the live/4 route, very similar to the option that the “normal” Phoenix route methods (get, post, match, etc.) have.

Something like:

live_session :admin do
  live "/users", UsersLive, assigns: %{route_name: :users, allow: :admin}
  live "/posts", PostLive, assigns: %{route_name: :posts, allow: :user}
end

This has the advantage of not requiring extra logic of extracting the metadata from the route in handle_params/3 and be immediately available on mount/3 (and even on the on_mount/4 hook).

This is specially useful for libraries too, like LiveDashboard. In there the list of pages are saved in the session, but the assigns could be used for it to avoid serializing the list to the client (not sure if this is desired there but it’s just an example).