From edf8a802936961c5397c3405b67a76314084d602 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Valim?= Date: Fri, 22 Nov 2024 23:25:58 +0100 Subject: [PATCH] Log when navigate fails --- lib/phoenix_live_view.ex | 5 +++-- lib/phoenix_live_view/channel.ex | 17 +++++++++++++++-- lib/phoenix_live_view/session.ex | 2 +- 3 files changed, 19 insertions(+), 5 deletions(-) diff --git a/lib/phoenix_live_view.ex b/lib/phoenix_live_view.ex index 380a2856d8..975471a190 100644 --- a/lib/phoenix_live_view.ex +++ b/lib/phoenix_live_view.ex @@ -1031,7 +1031,8 @@ defmodule Phoenix.LiveView do immediately invoked to handle the change of params and URL state. Then the new state is pushed to the client, without reloading the whole page while also maintaining the current scroll position. - For live navigation to another LiveView, use `push_navigate/2`. + For live navigation to another LiveView in the same `live_session`, + use `push_navigate/2`. Otherwise, use `redirect/2`. ## Options @@ -1051,7 +1052,7 @@ defmodule Phoenix.LiveView do end @doc """ - Annotates the socket for navigation to another LiveView. + Annotates the socket for navigation to another LiveView in the same `live_session`. The current LiveView will be shutdown and a new one will be mounted in its place, without reloading the whole page. This can diff --git a/lib/phoenix_live_view/channel.ex b/lib/phoenix_live_view/channel.ex index d4f1f7ad5b..26b1dbeb35 100644 --- a/lib/phoenix_live_view/channel.ex +++ b/lib/phoenix_live_view/channel.ex @@ -1532,10 +1532,23 @@ defmodule Phoenix.LiveView.Channel do defp authorize_session(%Session{} = session, endpoint, %{"redirect" => url}) do if redir_route = session_route(session, endpoint, url) do case Session.authorize_root_redirect(session, redir_route) do - {:ok, %Session{} = new_session} -> {:ok, new_session, redir_route, url} - {:error, :unauthorized} = err -> err + {:ok, %Session{} = new_session} -> + {:ok, new_session, redir_route, url} + + :error -> + Logger.warning( + "navigate event to #{inspect(url)} failed because you are redirecting across live_sessions. " <> + "A full page reload will be performed instead" + ) + + {:error, :unauthorized} end else + Logger.warning( + "navigate event to #{inspect(url)} failed because the URL does not point to a LiveView. " <> + "A full page reload will be performed instead" + ) + {:error, :unauthorized} end end diff --git a/lib/phoenix_live_view/session.ex b/lib/phoenix_live_view/session.ex index 9298315173..bae1e45c3f 100644 --- a/lib/phoenix_live_view/session.ex +++ b/lib/phoenix_live_view/session.ex @@ -25,7 +25,7 @@ defmodule Phoenix.LiveView.Session do {:ok, replace_root(session, route.view, self())} true -> - {:error, :unauthorized} + :error end end