Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: allow prefixing the link paths #418

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
6 changes: 4 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,9 @@ 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()
# broken due to redirecting in a loop
prefix = socket.router.__live_dashboard_path_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