Skip to content

Commit b59436a

Browse files
SteffenDEjosevalim
andauthored
add debug_attributes and set LiveView PID on view element (#3898)
* add debug_pids * add comment * debug_tags_location -> debug_attributes * Update lib/phoenix_component.ex Co-authored-by: José Valim <[email protected]> --------- Co-authored-by: José Valim <[email protected]>
1 parent 37ac183 commit b59436a

File tree

6 files changed

+29
-8
lines changed

6 files changed

+29
-8
lines changed

assets/js/phoenix_live_view/constants.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ export const PHX_PORTAL = "data-phx-portal";
7979
export const PHX_TELEPORTED_REF = "data-phx-teleported";
8080
export const PHX_TELEPORTED_SRC = "data-phx-teleported-src";
8181
export const PHX_RUNTIME_HOOK = "data-phx-runtime-hook";
82+
export const PHX_LV_PID = "data-phx-pid";
8283
export const PHX_KEY = "key";
8384
export const PHX_PRIVATE = "phxPrivate";
8485
export const PHX_AUTO_RECOVER = "auto-recover";

assets/js/phoenix_live_view/view.js

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ import {
3636
PHX_VIEWPORT_TOP,
3737
PHX_VIEWPORT_BOTTOM,
3838
MAX_CHILD_JOIN_ATTEMPTS,
39+
PHX_LV_PID,
3940
} from "./constants";
4041

4142
import {
@@ -392,7 +393,7 @@ export default class View {
392393
}
393394

394395
onJoin(resp) {
395-
const { rendered, container, liveview_version } = resp;
396+
const { rendered, container, liveview_version, pid } = resp;
396397
if (container) {
397398
const [tag, attrs] = container;
398399
this.el = DOM.replaceRootContainer(this.el, tag, attrs);
@@ -418,6 +419,15 @@ export default class View {
418419
);
419420
}
420421

422+
// The pid is only sent if
423+
//
424+
// config :phoenix_live_view, :debug_attributes
425+
//
426+
// if set to true. It is to help debugging in development.
427+
if (pid) {
428+
this.el.setAttribute(PHX_LV_PID, pid);
429+
}
430+
421431
Browser.dropLocal(
422432
this.liveSocket.localStorage,
423433
window.location.pathname,

lib/phoenix_component.ex

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -535,8 +535,9 @@ defmodule Phoenix.Component do
535535
<!-- </AppWeb.CoreComponents.header> -->
536536
```
537537
538-
Similarly, you can also turn on `:debug_tags_location`, which adds a
539-
`data-phx-loc` attribute with the line of where each HTML tag is defined:
538+
Similarly, you can also turn on `:debug_attributes`, which adds a
539+
`data-phx-loc` attribute with the line of where each HTML tag is defined
540+
(as well as `data-phx-pid` to the LiveView container):
540541
541542
```html
542543
<header data-phx-loc="125" class="p-5">
@@ -549,7 +550,7 @@ defmodule Phoenix.Component do
549550
550551
config :phoenix_live_view,
551552
debug_heex_annotations: true,
552-
debug_tags_location: true
553+
debug_attributes: true
553554
554555
Changing this configuration will require `mix clean` and a full recompile.
555556

lib/phoenix_live_view/channel.ex

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1398,7 +1398,8 @@ defmodule Phoenix.LiveView.Channel do
13981398

13991399
case result do
14001400
{:ok, diff, :mount, new_state} ->
1401-
reply = put_container(session, route, %{rendered: diff, liveview_version: lv_vsn})
1401+
diff = maybe_put_debug_pid(%{rendered: diff, liveview_version: lv_vsn})
1402+
reply = put_container(session, route, diff)
14021403
GenServer.reply(from, {:ok, reply})
14031404
{:noreply, post_verified_mount(new_state)}
14041405

@@ -1423,6 +1424,14 @@ defmodule Phoenix.LiveView.Channel do
14231424
end
14241425
end
14251426

1427+
defp maybe_put_debug_pid(diff) do
1428+
if Application.get_env(:phoenix_live_view, :debug_attributes, false) do
1429+
Map.put(diff, :pid, inspect(self()))
1430+
else
1431+
diff
1432+
end
1433+
end
1434+
14261435
defp build_state(%Socket{} = lv_socket, %Phoenix.Socket{} = phx_socket) do
14271436
%{
14281437
join_ref: phx_socket.join_ref,

lib/phoenix_live_view/tag_engine.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1054,7 +1054,7 @@ defmodule Phoenix.LiveView.TagEngine do
10541054

10551055
defp handle_tag_and_attrs(state, name, attrs, suffix, meta) do
10561056
text =
1057-
if Application.get_env(:phoenix_live_view, :debug_tags_location, false) do
1057+
if Application.get_env(:phoenix_live_view, :debug_attributes, false) do
10581058
"<#{name} data-phx-loc=\"#{meta[:line]}\""
10591059
else
10601060
"<#{name}"

test/test_helper.exs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
Application.put_env(:phoenix_live_view, :debug_heex_annotations, true)
2-
Application.put_env(:phoenix_live_view, :debug_tags_location, true)
2+
Application.put_env(:phoenix_live_view, :debug_attributes, true)
33
Code.require_file("test/support/live_views/debug_anno.exs")
4-
Application.put_env(:phoenix_live_view, :debug_tags_location, false)
4+
Application.put_env(:phoenix_live_view, :debug_attributes, false)
55
Application.put_env(:phoenix_live_view, :debug_heex_annotations, false)
66

77
{:ok, _} = Phoenix.LiveViewTest.Support.Endpoint.start_link()

0 commit comments

Comments
 (0)