diff --git a/assets/js/liveview/live_socket.js b/assets/js/liveview/live_socket.js index bbf1ea747..04e018a44 100644 --- a/assets/js/liveview/live_socket.js +++ b/assets/js/liveview/live_socket.js @@ -3,9 +3,12 @@ import { Socket } from "phoenix" import { LiveSocket } from "phoenix_live_view" let csrfToken = document.querySelector("meta[name='csrf-token']") -if (csrfToken) { +let websocketUrl = document.querySelector("meta[name='websocket-url']") +if (csrfToken && websocketUrl) { let token = csrfToken.getAttribute("content") - let liveSocket = new LiveSocket("/live", Socket, { + let url = websocketUrl.getAttribute("content") + let liveUrl = (url === "") ? "/live" : new URL("/live", url).href; + let liveSocket = new LiveSocket(liveUrl, Socket, { params: { _csrf_token: token }, hooks: {}, dom: { // for alpinejs integration onBeforeElUpdated(from, to) { diff --git a/config/runtime.exs b/config/runtime.exs index 0d309932c..e6837454d 100644 --- a/config/runtime.exs +++ b/config/runtime.exs @@ -225,6 +225,8 @@ log_failed_login_attempts = |> get_var_from_path_or_env("LOG_FAILED_LOGIN_ATTEMPTS", "false") |> String.to_existing_atom() +websocket_url = get_var_from_path_or_env(config_dir, "WEBSOCKET_URL", "") + config :plausible, environment: env, mailer_email: mailer_email, @@ -247,7 +249,8 @@ config :plausible, PlausibleWeb.Endpoint, transport_options: [max_connections: :infinity], protocol_options: [max_request_line_length: 8192, max_header_value_length: 8192] ], - secret_key_base: secret_key_base + secret_key_base: secret_key_base, + websocket_url: websocket_url maybe_ipv6 = if System.get_env("ECTO_IPV6"), do: [:inet6], else: [] diff --git a/lib/plausible_web/endpoint.ex b/lib/plausible_web/endpoint.ex index 8bfba7f05..bd6148b05 100644 --- a/lib/plausible_web/endpoint.ex +++ b/lib/plausible_web/endpoint.ex @@ -58,4 +58,10 @@ defmodule PlausibleWeb.Endpoint do plug CORSPlug plug PlausibleWeb.Router + + def websocket_url() do + :plausible + |> Application.fetch_env!(__MODULE__) + |> Keyword.fetch!(:websocket_url) + end end diff --git a/lib/plausible_web/templates/layout/app.html.eex b/lib/plausible_web/templates/layout/app.html.eex index ff0873031..8df312db7 100644 --- a/lib/plausible_web/templates/layout/app.html.eex +++ b/lib/plausible_web/templates/layout/app.html.eex @@ -6,6 +6,7 @@ /> + /> "> <%= assigns[:title] || "Plausible ยท Simple, privacy-friendly alternative to Google Analytics" %> diff --git a/lib/plausible_web/views/layout_view.ex b/lib/plausible_web/views/layout_view.ex index e81c35d29..bb2ee9217 100644 --- a/lib/plausible_web/views/layout_view.ex +++ b/lib/plausible_web/views/layout_view.ex @@ -9,6 +9,10 @@ defmodule PlausibleWeb.LayoutView do PlausibleWeb.Endpoint.url() end + def websocket_url() do + PlausibleWeb.Endpoint.websocket_url() + end + def home_dest(conn) do if conn.assigns[:current_user] do "/sites"