Skip to content

Commit 9340edb

Browse files
authored
fix: boot beacon even without defined beacon_site routes in host app … (#699)
* fix: boot beacon even without defined beacon_site routes in host app router * clean up * docs
1 parent 6b05085 commit 9340edb

File tree

6 files changed

+26
-2
lines changed

6 files changed

+26
-2
lines changed

guides/general/troubleshooting.md

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,14 @@ a site prefix can never match and it will never receive requests.
2929

3030
That's is not necessarily an error if you have multiple sites in the same project
3131
and each scope is filtering requests on the `:host` option.
32+
But it may indicate:
3233

33-
But it may indicate an invalid configuration, as a preceding route matching the prefix
34+
1. An invalid configuration, as a preceding route matching the prefix
3435
that was supposed to be handled by this site, or an invalid `:host` value.
3536

37+
2. Missing `use Beacon.Router` and/or missing `beacon_site` in your
38+
app's router file.
39+
3640
Note that if you're using `:host` on the scope and running in `localhost`,
3741
consider adding `"localhost"` to the list of allowed hosts.
3842

@@ -41,4 +45,4 @@ Also check the [Beacon.Router](https://hexdocs.pm/beacon/Beacon.Router.html) for
4145
## RuntimeError - could not find persistent term for endpoint
4246

4347
`Beacon` should be started after your host's `Endpoint`, please review the application children
44-
and make sure is declared after the endpoint.
48+
and make sure is declared after the endpoint.

lib/beacon/router.ex

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -270,7 +270,10 @@ 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+
function_exported?(router, :__beacon_scoped_prefix_for_site__, 1) && reachable?(site, endpoint, router, opts)
274+
end
273275

276+
defp reachable?(site, endpoint, router, opts) do
274277
host = Keyword.get_lazy(opts, :host, fn -> endpoint.host() end)
275278

276279
prefix =

test/beacon/registry_test.exs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ defmodule Beacon.RegistryTest do
1212
:lifecycle_test,
1313
:lifecycle_test_fail,
1414
:my_site,
15+
:no_routes,
1516
:not_booted,
1617
:raw_schema_test,
1718
:s3_site

test/beacon/router_test.exs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,5 +86,10 @@ defmodule Beacon.RouterTest do
8686
test "do not match any existing host/path", %{config: config} do
8787
refute Router.reachable?(config, host: nil, prefix: "/nested/invalid")
8888
end
89+
90+
test "router without beacon routes" do
91+
config = Beacon.Config.fetch!(:no_routes)
92+
refute Router.reachable?(config)
93+
end
8994
end
9095
end

test/support/router.ex renamed to test/support/routers.ex

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
defmodule Beacon.BeaconTest.NoRoutesRouter do
2+
use Beacon.BeaconTest.Web, :router
3+
end
4+
15
defmodule Beacon.BeaconTest.Router do
26
use Beacon.BeaconTest.Web, :router
37
use Beacon.Router

test/test_helper.exs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,13 @@ Supervisor.start_link(
5050
router: Beacon.BeaconTest.Router,
5151
repo: Beacon.BeaconTest.Repo
5252
],
53+
[
54+
site: :no_routes,
55+
mode: :testing,
56+
endpoint: Beacon.BeaconTest.Endpoint,
57+
router: Beacon.BeaconTest.NoRoutesRouter,
58+
repo: Beacon.BeaconTest.Repo
59+
],
5360
[
5461
site: :not_booted,
5562
mode: :testing,

0 commit comments

Comments
 (0)