Skip to content

Commit 6b05085

Browse files
authored
fix: make Router.reachable/2 less strict (#698)
1 parent 7774d76 commit 6b05085

File tree

4 files changed

+47
-6
lines changed

4 files changed

+47
-6
lines changed

lib/beacon/router.ex

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -270,10 +270,24 @@ defmodule Beacon.Router do
270270
# match and invalidate the `beacon_site` mount.
271271
def reachable?(%Beacon.Config{} = config, opts \\ []) do
272272
%{site: site, endpoint: endpoint, router: router} = config
273+
273274
host = Keyword.get_lazy(opts, :host, fn -> endpoint.host() end)
274-
prefix = router.__beacon_scoped_prefix_for_site__(site)
275+
276+
prefix =
277+
Keyword.get_lazy(opts, :prefix, fn ->
278+
router.__beacon_scoped_prefix_for_site__(site)
279+
end)
275280

276281
case Phoenix.Router.route_info(router, "GET", prefix, host) do
282+
# bypass and allow booting beacon sites even though there's a route conflict
283+
# but only for root paths, for example:
284+
# live /
285+
# beacon_site /
286+
# that's because even though they share the same prefix,
287+
# beacon can still serve pages at /:page
288+
%{route: "/"} ->
289+
true
290+
277291
%{phoenix_live_view: {Beacon.Web.PageLive, _, _, %{extra: %{session: %{"beacon_site" => ^site}}}}} ->
278292
true
279293

test/beacon/router_test.exs

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -63,14 +63,28 @@ defmodule Beacon.RouterTest do
6363
end
6464

6565
describe "reachable?" do
66-
test "test" do
66+
setup do
6767
site = :host_test
6868
config = Beacon.Config.fetch!(site)
69-
valid_host = "host.com"
69+
[config: config]
70+
end
7071

72+
test "match existing host", %{config: config} do
73+
valid_host = "host.com"
7174
assert Router.reachable?(config, host: valid_host)
72-
refute Router.reachable?(%{config | site: :my_site}, host: valid_host)
73-
refute Router.reachable?(config, host: "other.com")
75+
end
76+
77+
test "existing nested conflicting route", %{config: config} do
78+
valid_host = "host.com"
79+
refute Router.reachable?(config, host: valid_host, prefix: "/nested/some_page")
80+
end
81+
82+
test "with no specific host", %{config: config} do
83+
assert Router.reachable?(config, host: nil)
84+
end
85+
86+
test "do not match any existing host/path", %{config: config} do
87+
refute Router.reachable?(config, host: nil, prefix: "/nested/invalid")
7488
end
7589
end
7690
end

test/support/live_views.ex

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
defmodule Beacon.BeaconTest.LiveViews.FooBarLive do
2+
use Phoenix.LiveView
3+
def render(assigns), do: ~H""
4+
end

test/support/router.ex

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,17 @@ defmodule Beacon.BeaconTest.Router do
1616
beacon_site "/media", site: :s3_site
1717
end
1818

19-
# test :host
19+
scope "/" do
20+
pipe_through :browser
21+
22+
live_session :default do
23+
live "/", Beacon.BeaconTest.LiveViews.FooBarLive
24+
live "/nested/:page", Beacon.BeaconTest.LiveViews.FooBarLive
25+
end
26+
end
27+
2028
scope path: "/", host: "host.com" do
29+
pipe_through :browser
2130
beacon_site "/", site: :host_test
2231
end
2332

0 commit comments

Comments
 (0)