Skip to content

Commit 4d3fc56

Browse files
committed
Use Phoenix assets directly
1 parent 48116dd commit 4d3fc56

File tree

6 files changed

+27
-37
lines changed

6 files changed

+27
-37
lines changed

assets/bun.lockb

-40.6 KB
Binary file not shown.

assets/js/app.js

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
1-
// Establish Phoenix Socket and LiveView configuration.
2-
import { Socket, LongPoll } from "phoenix";
3-
import { LiveSocket } from "phoenix_live_view";
1+
// Phoenix assets are imported from dependencies.
42
import topbar from "topbar";
53

64
let csrfToken = document.querySelector("meta[name='csrf-token']").getAttribute("content");
7-
let socketPath = document.querySelector("meta[name='socket-path']").getAttribute("content");
8-
let socketTransport = document.querySelector("meta[name='socket-transport']").getAttribute("content");
9-
let normalizedTransport = (socketTransport == "longpoll") ? LongPoll : WebSocket;
5+
let livePath = document.querySelector("meta[name='live-path']").getAttribute("content");
6+
let liveTransport = document .querySelector("meta[name='live-transport']") .getAttribute("content");
107

11-
let liveSocket = new LiveSocket(socketPath, Socket, { transport: normalizedTransport, params: { _csrf_token: csrfToken }});
8+
let liveSocket = new LiveView.LiveSocket(livePath, Phoenix.Socket, {
9+
transport: liveTransport === "longpoll" ? Phoenix.LongPoll : WebSocket,
10+
params: { _csrf_token: csrfToken },
11+
});
1212

1313
// Show progress bar on live navigation and form submits
1414
topbar.config({ barColors: { 0: "#29d" }, shadowColor: "rgba(0, 0, 0, .3)" });

lib/error_tracker/web.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ defmodule ErrorTracker.Web do
6262
```elixir
6363
config :error_tracker,
6464
socket: [
65-
path: "/my-custom-socket-path"
65+
path: "/my-custom-live-path"
6666
transport: :longpoll # (accepted values are :longpoll or :websocket)
6767
]
6868
```

lib/error_tracker/web/components/layouts.ex

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,25 @@ defmodule ErrorTracker.Web.Layouts do
22
@moduledoc false
33
use ErrorTracker.Web, :html
44

5-
@default_socket_config %{path: "/live", transport: :websocket}
5+
phoenix_js_paths =
6+
for app <- ~w[phoenix phoenix_html phoenix_live_view]a do
7+
path = Application.app_dir(app, ["priv", "static", "#{app}.js"])
8+
Module.put_attribute(__MODULE__, :external_resource, path)
9+
path
10+
end
11+
12+
@static_path Application.app_dir(:error_tracker, ["priv", "static"])
13+
@external_resource css_path = Path.join(@static_path, "app.css")
14+
@external_resource js_path = Path.join(@static_path, "app.js")
615

7-
@css_path Application.app_dir(:error_tracker, ["priv", "static", "app.css"])
8-
@js_path Application.app_dir(:error_tracker, ["priv", "static", "app.js"])
16+
@css File.read!(css_path)
917

10-
@external_resource @css_path
11-
@external_resource @js_path
18+
@js """
19+
#{for path <- phoenix_js_paths, do: path |> File.read!() |> String.replace("//# sourceMappingURL=", "// ")}
20+
#{File.read!(js_path)}
21+
"""
1222

13-
@css File.read!(@css_path)
14-
@js File.read!(@js_path)
23+
@default_socket_config %{path: "/live", transport: :websocket}
1524

1625
embed_templates "layouts/*"
1726

lib/error_tracker/web/components/layouts/root.html.heex

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
66
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
77
<meta name="csrf-token" content={get_csrf_token()} />
8-
<meta name="socket-path" content={get_socket_config(:path)} />
9-
<meta name="socket-transport" content={get_socket_config(:transport)} />
8+
<meta name="live-path" content={get_socket_config(:path)} />
9+
<meta name="live-transport" content={get_socket_config(:transport)} />
1010

1111
<title>{assigns[:page_title] || "🐛 ErrorTracker"}</title>
1212

priv/static/app.js

Lines changed: 1 addition & 20 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)