Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Phoenix_flash passed from conn session no longer persists in LV assigns #502

Closed
tmepple opened this issue Nov 25, 2019 · 2 comments
Closed

Comments

@tmepple
Copy link
Contributor

tmepple commented Nov 25, 2019

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.

@josevalim
Copy link
Member

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.

@josevalim
Copy link
Member

To be tackled on #570.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants