I have an app where the user can navigate from the Main Index to a Board via live_patch passing the board ID:
<%= for board <- @board_list do %>
<div class="flex">
<div class="py-4 px-4">
<div>
<%= live_patch board.name,
to: Routes.live_path(@socket, __MODULE__, board: board.id),
class: "text-purple-700 hover:underline" %>
</div>
Then handle_params picks up the change, adjusts the view, and changes the :page_title
def handle_params(params, _url, socket) do
socket =
case params do
%{"board" => board_id} ->
socket
|> assign(nav: :board)
|> ...
|> assign(page_title: Boards.get_name(board_id))
%{} ->
...
end
{:noreply, socket}
end
I expected this to create a proper browser history to track the user’s navigation, and to an extent, it does: When the user clicks the back button, it returns them to the appropriate page. However, the page titles in the history are wrong.
If the user went from
Main Index → Board 1 → Main Index → Board 2
Then the browser history will show
Board 1 → Main Index → Board 2
And the current page title will be “Board 2”.
My hypothesis is that the browser sees the updated page title BEFORE the “navigation” happens, so the back button correctly leads to the previous parameters, but shows the current page title.
Is there a workaround for this where the browser will not see the new page title until AFTER live_patch has sent the params and “navigation” is finished?
Thanks in advance!