Phoenix_live_view: Phoenix_flash passed from conn session no longer persists in LV assigns

Created on 25 Nov 2019  路  2Comments  路  Source: phoenixframework/phoenix_live_view

Environment

  • Elixir version (elixir -v): v1.9.4 Erlang/OTP 22
  • Phoenix version (mix deps): v1.4.11
  • Phoenix LiveView version (mix deps): ref 08bf171
  • NodeJS version (node -v): v12.13.0
  • NPM version (npm -v): v6.12.0
  • Operating system: MacOS Catalina 10.15.1

I'm having trouble getting phoenix_flash from my conn session into LiveView assigns after the recent commit that automatically loads session and csrf tokens.

Previously to commit 08bf17 it was working great like this:

# router.ex
live "/", IndexLive, session: [:current_user, :phoenix_flash]
# index_live.ex
def mount(session, socket) do
  {:ok, assign(socket, current_user: session[:current_user], phoenix_flash: session[:phoenix_flash])}
end

Now the conn session is automatically loaded so the code looks like this:

# router.ex
live "/", IndexLive
# index_live.ex
def mount(session, socket) do
  new_socket =
    socket
    |> assign(:current_user, session["current_user"])
    |> assign_new(:phoenix_flash, fn -> session["phoenix_flash"] end) 

  {:ok, new_socket}
end

On disconnected mount the flash message is assigned and properly rendered for a split second. Unfortunately, after the LV connects and mount is called again the "phoenix_flash" is already cleared and no longer appears in the session which causes it to disappear from the render.

I thought using assign_new would work since once the assign is set (on disconnected render) it would not be overwritten on the live connect but it appears the socket assigns do not persist between renders... I guess because the true "socket" doesn't exist at that point.

I know the usual pattern is rendering flash in your layout (which would work) but I can't do that because I need the message rendered in the middle of a LV not above it.

Most helpful comment

To be tackled on #570.

All 2 comments

This is what is happening:

  1. we render the page and we show the flash message, the use of the flash message makes it be removed from the session

  2. then we connect to LiveView with the new session, where flash_message was already discarded

I am not sure what is the best fix here. But we will need to eventually solve it because we are planning to move layouts to LiveViews too. Maybe we will make any current flash part of the live view session and handle it automatically for you.

And you are correct in regards to assign_new. It is not shared between connections (the disconnected and connected render). It is mostly used to pass information from parent to child.

To be tackled on #570.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

LightningK0ala picture LightningK0ala  路  5Comments

josevalim picture josevalim  路  3Comments

josevalim picture josevalim  路  3Comments

drapergeek picture drapergeek  路  5Comments

lukaszsamson picture lukaszsamson  路  5Comments