Skip to content

Commit

Permalink
fix: allow prefixing the link paths
Browse files Browse the repository at this point in the history
  • Loading branch information
yordis committed May 29, 2023
1 parent 80637ee commit b62ed84
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 3 deletions.
11 changes: 10 additions & 1 deletion dev.exs
Original file line number Diff line number Diff line change
Expand Up @@ -456,6 +456,13 @@ defmodule DemoWeb.Router do
use Phoenix.Router
import Phoenix.LiveDashboard.Router

forward "/admin", DemoWeb.Router.Admin
end

defmodule DemoWeb.Router.Admin do
use Phoenix.Router
import Phoenix.LiveDashboard.Router

pipeline :browser do
plug :fetch_session
plug :protect_from_forgery
Expand All @@ -470,6 +477,8 @@ defmodule DemoWeb.Router do
get "/hello/:name", DemoWeb.PageController, :hello

live_dashboard("/dashboard",
live_socket_path: "/live",
path_prefix: "/admin",
env_keys: ["USER", "ROOTDIR"],
metrics: DemoWeb.Telemetry,
metrics_history: {DemoWeb.History, :data, []},
Expand Down Expand Up @@ -518,7 +527,7 @@ defmodule DemoWeb.Endpoint do
same_site: "Lax"
]

socket "/live", Phoenix.LiveView.Socket, websocket: [connect_info: [session: @session_options]]
socket "/admin/live", Phoenix.LiveView.Socket, websocket: [connect_info: [session: @session_options]]
socket "/phoenix/live_reload/socket", Phoenix.LiveReloader.Socket

plug Phoenix.LiveReloader
Expand Down
4 changes: 2 additions & 2 deletions lib/phoenix/live_dashboard/page_builder.ex
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ defmodule Phoenix.LiveDashboard.PageBuilder do
We currently support `card/1`, `fields_card/1`, `row/1`,
`shared_usage_card/1`, and `usage_card/1`;
and the live components `live_layered_graph/1`, `live_nav_bar/1`,
and the live components `live_layered_graph/1`, `live_nav_bar/1`,
and `live_table/1`.
## Helpers
Expand Down Expand Up @@ -956,7 +956,7 @@ defmodule Phoenix.LiveDashboard.PageBuilder do
def live_dashboard_path(socket, route, node, old_params, new_params) when is_atom(node) do
if function_exported?(socket.router, :__live_dashboard_prefix__, 0) do
new_params = for {key, val} <- new_params, key not in ~w(page node), do: {key, val}
prefix = socket.router.__live_dashboard_prefix__()
prefix = socket.router.__live_dashboard_prefix__() |> dbg()

path =
if node == node() and is_nil(old_params["node"]) do
Expand Down
4 changes: 4 additions & 0 deletions lib/phoenix/live_dashboard/router.ex
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,8 @@ defmodule Phoenix.LiveDashboard.Router do
opts
end

route_path_prefix = Keyword.get(opts, :path_prefix, "") <> path

scope =
quote bind_quoted: binding() do
scope path, alias: false, as: false do
Expand Down Expand Up @@ -126,7 +128,9 @@ defmodule Phoenix.LiveDashboard.Router do

unless Module.get_attribute(__MODULE__, :live_dashboard_prefix) do
@live_dashboard_prefix Phoenix.Router.scoped_path(__MODULE__, path)
@live_dashboard_path_prefix Phoenix.Router.scoped_path(__MODULE__, route_path_prefix)
def __live_dashboard_prefix__, do: @live_dashboard_prefix
def __live_dashboard_path_prefix__, do: @live_dashboard_path_prefix
end
end
else
Expand Down

0 comments on commit b62ed84

Please sign in to comment.