Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Beacon.Plug to mix beacon.install #686

Merged
merged 4 commits into from
Dec 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
- `Beacon.Config` option `:page_warming` can change the number of pages to warm, specify which pages, or disable warming per site
- Only start reachable sites on boot to save resources
- Warn when a site defined in the router is not reachable

- Add `Beacon.Plug` for consistent rendering when using Page Variants
- `mix beacon.install` now adds `Beacon.Plug` to host app Router

### Fixes
- Remove self dependency on ErrorPage module
Expand Down
23 changes: 23 additions & 0 deletions guides/upgrading/v0.3.0.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Upgrading to v0.3.0

## Add `Beacon.Plug` to your Router

In your application's `router.ex`, first find the pipeline used for your beacon_site. It is usually `:browser` but you can check:

```elixir
scope "/" do
pipe_through :browser
beacon_site "/", site: :my_site
end
```

In the above case we can see the pipeline is `:browser`. Still in the router, look for that pipeline and add `plug Beacon.Plug`:

```elixir
pipeline :browser do
...
plug Beacon.Plug
end
```

Now the `Beacon.Plug` will ensure consistent rendering, particularly important when Page Variants are used.
17 changes: 14 additions & 3 deletions lib/mix/tasks/beacon.install.ex
Original file line number Diff line number Diff line change
Expand Up @@ -49,20 +49,22 @@ defmodule Mix.Tasks.Beacon.Install do
argv = igniter.args.argv
options = igniter.args.options

{igniter, router} = Igniter.Libs.Phoenix.select_router(igniter)

igniter
|> add_beacon_plugin_formatter()
|> replace_error_html()
|> replace_error_html(router)
|> add_plug_to_router_pipeline(router)
|> maybe_gen_site(options, argv)
end

defp add_beacon_plugin_formatter(igniter) do
Igniter.Project.Formatter.import_dep(igniter, :beacon)
end

defp replace_error_html(igniter) do
defp replace_error_html(igniter, router) do
app_name = Igniter.Project.Application.app_name(igniter)

{igniter, router} = Igniter.Libs.Phoenix.select_router(igniter)
{igniter, [endpoint]} = Igniter.Libs.Phoenix.endpoints_for_router(igniter, router)

Igniter.Project.Config.configure(
Expand All @@ -74,6 +76,15 @@ defmodule Mix.Tasks.Beacon.Install do
)
end

defp add_plug_to_router_pipeline(igniter, router) do
Igniter.Libs.Phoenix.append_to_pipeline(
igniter,
:browser,
"plug Beacon.Plug",
router: router
)
end

defp maybe_gen_site(igniter, options, argv) do
if options[:site] do
Igniter.compose_task(igniter, "beacon.gen.site", argv)
Expand Down
6 changes: 3 additions & 3 deletions test/mix/tasks/gen_site_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ defmodule Mix.Tasks.Beacon.GenSiteTest do
project
|> Igniter.compose_task("beacon.gen.site", @opts_my_site)
|> assert_has_patch("lib/test_web/router.ex", """
23 + | beacon_site "/", site: :my_site
24 + | beacon_site "/", site: :my_site
""")
end

Expand All @@ -82,8 +82,8 @@ defmodule Mix.Tasks.Beacon.GenSiteTest do
|> apply_igniter!()
|> Igniter.compose_task("beacon.gen.site", @opts_other_site)
|> assert_has_patch("lib/test_web/router.ex", """
23 23 | beacon_site "/", site: :my_site
24 + | beacon_site "/other", site: :other
24 24 | beacon_site "/", site: :my_site
25 + | beacon_site "/other", site: :other
""")
end
end
Expand Down
10 changes: 9 additions & 1 deletion test/mix/tasks/install_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,19 @@ defmodule Mix.Tasks.Beacon.InstallTest do
""")
end

test "add Beacon.Plug to router pipeline", %{project: project} do
project
|> Igniter.compose_task("beacon.install")
|> assert_has_patch("lib/test_web/router.ex", """
11 + | plug Beacon.Plug
""")
end

test "optionally generates new site", %{project: project} do
project
|> Igniter.compose_task("beacon.install", ~w(--site my_site --path /my_site))
|> assert_has_patch("lib/test_web/router.ex", """
23 + | beacon_site "/my_site", site: :my_site
24 + | beacon_site "/my_site", site: :my_site
""")
end
end
Loading