LiveView 0.9.0 :flash is a reserved assign and cannot be set directly

Dear Phoenix users,

I recently upgraded from Phoenix LiveView 0.8.0 to 0.9.0. In the enhancements section of the changelog, it is written “Raise if :flash is given as an assign” which is the error I have with my live view : “:flash is a reserved assign by LiveView and it cannot be set directly. Use the appropriate flash functions instead.”

If I dump the assigns I receive inside the function handle_info/2, I can see that indeed there is a :flash assign : ..., flash: %{}, ....

I add other assigns inside the function before doing assign(socket, assigns).

The problem is that I am not able to identify the place where this assign is made. I don’t think I make it explicitly somewhere. Do I have to remove the explicit :flash key inside of the assigns even if I didn’t put it there ? It would look like a dirty hack to me.

In my router, I have :

pipeline :browser do
  plug :accepts, ["html"]
  plug :fetch_session
  plug :fetch_live_flash
  plug :protect_from_forgery
  plug :put_secure_browser_headers
  plug HydroWeb.AuthPlug
  plug :put_live_layout, {HydroWeb.LayoutView, "app.html"}
end

I removed my old “plug :fetch_flash” (should have been removed sooner) and I added the “plug :put_live_layout” to conform to the backwards incompatible change.

If I search for “flash” inside my Elixir source code, I find this the result below.

hydro_web/lib/hydro_web.ex
37:      import Phoenix.Controller, only: [get_flash: 1, get_flash: 2, view_module: 1]

hydro_web/lib/hydro_web/router.ex
8:    plug :fetch_live_flash

hydro_web/lib/hydro_web/controllers/page_controller.ex
89:      |> put_flash(:error, "You must be logged in to access that page")

hydro_web/lib/hydro_web/controllers/session_controller.ex
22:        |> put_flash(:error, "Invalid username/password combination")

hydro_web/lib/hydro_web/plugs/ensure_role_plug.ex
41:    |> Controller.put_flash(:error, "Unauthorized access")

hydro_web/lib/hydro_web/templates/session/new.html.eex
2:  <p class="alert alert-info" role="alert"><%= get_flash(@conn, :info) %></p>
3:  <p class="alert alert-danger" role="alert"><%= get_flash(@conn, :error) %></p>

hydro_web/lib/hydro_web/templates/layout/app.html.eex
23:      <p class="alert alert-info" role="alert"><%= get_flash(@conn, :info) %></p>
24:      <p class="alert alert-danger" role="alert"><%= get_flash(@conn, :error)   %></p>

Any idea is welcome. TIA

You are probably re-assigning the assigns from the socket (which would have the :flash in it).

I would search for occurrences of this: assign(socket, assigns).

5 Likes