Bug in navigating from a controller to a LiveView page (put_flash message will not be deleted)

Hi folks, I think I found a bug in LiveView which was reported in GitHub by me (link: put_flash message exists whole project pages when even user navigates).
In this bug you can’t clean Flash message when you navigate from a controller to a Phoenix LiveView.

For example

you want to create a login form, after user sends his email and password you should send request to a controller (post method) and if this information is right, Phoenix controller refresh the page and create a session for LiveView.

If you send a put_flash form controller to a LiveView page, This message will not be hidden even you navigate to another page until refresh the page, but if you create a put_flash In Live View after navigating it is concealed and has no problem.

On the post which is reported, I captured some videos and I show my problems, but I create a simple video again to show this problem.


{:phoenix, "~> 1.6.0"},
{:phoenix_live_view, "~> 0.16.4"} and test master

It should be noted, I had no problem with phoenix LiveView 0.15.7.

Please help me to fix it, Thanks

Can somebody suggest me how can I fix this problem? Or suggest a way around this problem. Thanks!

Temporary patch

you can put_session instead of put_flash, create a tuple like this {:successful_login, created time} and send it to LiveView, after sending you can check the time with current time if their compare are bigger than 5 second you can skip it if not you can show this.
I didn’t test it, but I think it works.

Maybe emit the clear_flash event when a user clicks the link?

clear_flash restarts the live page, the main problem is why user should see the flash message when he navigates to another menu or page if I use clear_flash in mount it will be restarted and doesn’t show flash message which is bad.

Maybe user doesn’t want to click on flash message to start event to clear it and if he uses it, it is restarted and this is a bad experience.

It shouldn’t restart the page… All it does is update the assigns:

# phoenix_live_view/lib/phoenix_live_view/utils.ex
def clear_flash(%Socket{} = socket) do
  assign(socket, :flash, %{})

Maybe there is something else in your logic that is crashing the LV?

Excuses me on mount it restates the live page. I tested it on my project 1mint ago. If user click on flash message like this:

<p id="<%= Ecto.UUID.generate %>" class="col titile-of-blog-posts alert alert-info" role="alert" phx-click="clear_flash" phx-value-key="info" phx-target="<%= @myself %>">

def handle_event("clear_flash", _params, socket) do
    {:noreply, socket |> clear_flash()}

flash message is deleted but if he navigates to another live page with live redirect, he sees the flash message again

I test it on normal phoenix without any extra code

I think clear_flash just clears the flash message which is created by LiveView not controller put_flash. @josevalim

By the way, :flash is a reserved assign by LiveView, and it cannot be set directly. Use the appropriate flash functions instead.

Controller example:

def index(conn, _params) do
    |> put_flash(:info, "after redirecting from a controller to a LiveView:::This put_flash is not going to be hiddend when user navigates with live_redirect")
    |> redirect(to: "#{Router.live_path(conn, TestPutFlashWeb.HomeLive)}")

Oh, I see now. You’re putting the flash in the conn, but then once you start the LV, you don’t have access to the conn anymore. Perhaps instead of putting the flash there, you could add a URL param (e.g. ?show_flash=true) to the live redirect, and then match on that in handle_params to add the flash message to the socket instead.

This is a second solution that I have already thought about!, but my users can put it on my project URL and show this (flash) every time they want, I know it has no problem for me as security problem.

After updating to phoenix 1.6 and LiveView 0.16.1, when my controller sends user to live side of my project with put_flash and redirect, the flash message is not cleared when user navigate to another pages. But this problem does not exist in the previous version.

It should be noted, I have no problem before 0.16.0. I think it is better to be fixed in Phoenix LiveView lib.

By the way, Phoenix LiveView puts the conn flash into assgns

assigns: %{__changed__: %{}, flash: %{}, live_action: nil},

And we can not replace directly:

`:flash` is a reserved assign by LiveView, and it cannot be set directly. Use the appropriate flash functions instead.

You don’t need to replace the flash directly - you can use put_flash and clear_flash for that.

New response of @chrismccord

Yes if I create flash message with handle_params not controller

@shahryarjb please realize in response to me asking to not ping the maintainers, you came here and… pinged the maintainers! (me) :upside_down_face:, which sends an email to my inbox. We are tracking it. The best you can do is standby or submit a PR if you’re wanting to expedite the process :slight_smile:

Please avoid pinging the issues for updates since it adds noise to our maintainer workload. I understand you may be encountering issues, but rest assured if it’s here and open, it’s on our list to review/assess/fix. Thanks!

Yes, I am very sorry sir :pensive:, but I didn’t know how you can track the issues, after days I think you forgot this issue and I don’t want to ping you I just need a help or get explanation of you even less, maybe I can fix it and send a PR.

This is a great pride for me to send a PR if I can to a project helped me to learn programming.

Please do not be upset, and thank you for all works you did

Yeah I get, especially if you’re hitting an issue that you need fixed to accomplish your goals. Just keep in mind, repeatedly pinging maintainers in all available avenues (twitter, forums, GH) is inversely correlated with us prioritizing an issue :slight_smile:

It sounds like a bug but I have not had the time to reproduce on my side. Also the comments about it “resetting” the LiveView makes it appear you also have some crashes on your end. We’ve been hard at work prepping some big features for 0.17, so once that and ElixirConf settles, I should have bandwidth to take a look at other issues.

1 Like

Thank you, I think it is fixed.