phoenix_playground
phoenix_playground copied to clipboard
Support for `push_patch` and multiple URLs?
Using the example code in https://github.com/phoenix-playground/phoenix_playground/blob/main/examples/demo_router.exs and trying to create multiple URLs in my router and navigate between them, I get the following error:
Router:
defmodule DemoRouter do
use Phoenix.Router
import Phoenix.LiveView.Router
pipeline :browser do
plug :accepts, ["html"]
plug :fetch_session
plug :put_root_layout, html: {PhoenixPlayground.Layouts, :root}
plug :put_secure_browser_headers
end
scope "/" do
pipe_through :browser
live_session :default, layout: {PhoenixPlayground.Layouts, :live} do
live "/", OperationLive
live "/page/:page", OperationLive
end
end
end
PhoenixPlayground.start(plug: DemoRouter)
LiveView:
defmodule OperationLive do
use Phoenix.LiveView
def mount(_params, _session, socket) do
{:ok, assign(socket, element_id: "first") }
end
def handle_params(%{"page" => page}, _uri, socket) do
{:noreply, assign(socket, element_id: page)}
end
def handle_params(_, _, socket), do: {:noreply, socket}
def handle_event("select-page", %{"id" => id}, socket) do
{:noreply, push_patch(socket, to: "/page/#{id}")}
end
def render(assigns) do
~H"""
<div>
<button :for={i <- ["first", "second", "third"]} phx-click="select-page" phx-value-id={i}>
Select <%= i %>
</button>
</div>
"""
end
end
The following error is raised when clicking a button:
11:20:57.930 [error] GenServer #PID<0.3512.0> terminating
** (ArgumentError) cannot invoke handle_params nor live_redirect/live_patch to %URI{scheme: "http", userinfo: nil, host: "localhost", port: 4000, path: "/page/second", query: nil, fragment: nil} because it isn't defined in PhoenixPlayground.Router.LiveRouter
(phoenix_live_view 0.20.17) lib/phoenix_live_view/route.ex:43: Phoenix.LiveView.Route.live_link_info!/3
(phoenix_live_view 0.20.17) lib/phoenix_live_view/channel.ex:882: Phoenix.LiveView.Channel.patch_params_and_action!/2
(phoenix_live_view 0.20.17) lib/phoenix_live_view/channel.ex:853: Phoenix.LiveView.Channel.handle_redirect/5
(stdlib 6.0.1) gen_server.erl:2173: :gen_server.try_handle_info/3
...
I'm not sure why the socket isn't configured to use my actual router instead of the default router, or if that would even solve the problem?
Thank you for the report. This is a playground bug, I'm not yet sure of the root cause but FWIW it works as expected when playground live reload is disabled, i.e.:
PhoenixPlayground.start(plug: DemoRouter, live_reload: false)
Btw, you can also use https://github.com/thmsmlr/livescript which requires playground with live_reload: false as it does its own live reloading and the bug shouldn't be there then.