mcrumm/phoenix_profiler

PhoenixProfiler seems to break `attach_hook`

miguel-s opened this issue · 4 comments

Hi @mcrumm!

Thanks for publishing this awesome profiler!

Issue

After following the setup instructions for the Phoenix Profiler my app started raising this exception:

** (exit) an exception was raised:
** (RuntimeError) cannot attach hook with id :active_tab on :handle_params because the view was not mounted at the router with the live/3 macro

I have a simple on_mount hook in MyAppWeb.Nav that handles setting a few assigns:

def on_mount(:default, _params, _session, socket) do
  {:cont,
   socket
   |> attach_hook(:active_tab, :handle_params, &handle_active_tab_params/3)
   |> attach_hook(:active_date, :handle_params, &handle_active_date_params/3)}
end

Which is used in the router:

live_session :authenticated,
      on_mount: [{MyAppWeb.UserAuth, :ensure_authenticated}, MyAppWeb.Nav] do
  # routes...
end

Removing the Phoenix Profiler dev config or removing the calls to attach_hook will get rid of the error, but obviously either the profiler or the assigns will be removed as well.

Hi @miguel-s thanks for the report. That's a know issue that is related to phoenix_live_view, which still needs some work.

In the mean time you can try to match and ignore that event by adding the following line to MyAppWeb.Nav (and others if necessary):

def on_mount(_, :not_mounted_at_router, _, socket), do: {:cont, socket}

Thanks @leandrocp your workaround worked!

That's a know issue that is related to phoenix_live_view

Just curious -- Is there a related discussion regarding this anywhere?

@iautom8things Leandro and I had a realtime discussion about whether or not to propose a way to make lifecycle hooks conditional on LiveView modules not mounted at the router. It may be possible to pass an option to live_render/3 to ignore the hooks, but I am not sure.