Skip to content

Commit

Permalink
make sure the page is loaded correctly and some cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
leandrocp committed Aug 17, 2023
1 parent c03144b commit 862603f
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 67 deletions.
13 changes: 11 additions & 2 deletions lib/beacon/lifecycle/template.ex
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
defmodule Beacon.Lifecycle.Template do
@moduledoc false

require Logger
alias Beacon.Lifecycle
@behaviour Beacon.Lifecycle

Expand Down Expand Up @@ -92,8 +93,16 @@ defmodule Beacon.Lifecycle.Template do
This stage runs in the render callback of the LiveView responsible for displaying the page.
"""
def render_template(site, template, format, context) do
lifecycle = Lifecycle.execute(__MODULE__, site, :render_template, template, sub_key: format, context: context)
@spec render_template(Beacon.Content.Page.t(), module(), map(), Macro.Env.t()) :: Beacon.Template.t()
def render_template(page, page_module, assigns, env) do
template =
case page_module.render(assigns) do
%Phoenix.LiveView.Rendered{} = rendered -> rendered
:not_loaded -> Beacon.Loader.PageModuleLoader.load_page!(page, page_module, assigns)
end

context = [path: page.path, assigns: assigns, env: env]
lifecycle = Lifecycle.execute(__MODULE__, page.site, :render_template, template, sub_key: page.format, context: context)
lifecycle.output
end
end
10 changes: 10 additions & 0 deletions lib/beacon/loader/page_module_loader.ex
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,16 @@ defmodule Beacon.Loader.PageModuleLoader do
GenServer.call(name(config.site), {:load_page!, page, stage}, 300_000)
end

# TODO: retry
def load_page!(%Content.Page{} = page, page_module, assigns) do
Logger.debug("compiling #{page_module}")

%Content.Page{} = page = Beacon.Content.get_published_page(page.site, page.id)
{:ok, ^page_module, _ast} = load_page!(page, :request)
%Phoenix.LiveView.Rendered{} = rendered = page_module.render(assigns)
rendered
end

defp build(module_name, component_module, functions) do
quote do
defmodule unquote(module_name) do
Expand Down
14 changes: 4 additions & 10 deletions lib/beacon/template.ex
Original file line number Diff line number Diff line change
Expand Up @@ -49,16 +49,10 @@ defmodule Beacon.Template do
# it is NOT supposed to be used to render templates
def __render__(site, path_list) when is_list(path_list) do
case Beacon.Router.lookup_path(site, path_list) do
{{site, path}, {_page_id, _layout_id, format, page_module, _component_module}} ->
assigns = %{__changed__: %{}, __live_path__: [], __beacon_page_module__: nil, __beacon_component_module__: nil}

template = page_module.render(assigns)

Beacon.Lifecycle.Template.render_template(site, template, format,
path: path,
assigns: assigns,
env: BeaconWeb.PageLive.make_env()
)
{{site, path}, {page_id, _layout_id, format, page_module, component_module}} ->
assigns = %{__changed__: %{}, __live_path__: [], __beacon_page_module__: page_module, __beacon_component_module__: component_module}
page = %Beacon.Content.Page{id: page_id, site: site, path: path, format: format}
Beacon.Lifecycle.Template.render_template(page, page_module, assigns, BeaconWeb.PageLive.make_env())

_ ->
raise BeaconWeb.NotFoundError, "page not found: #{inspect(path_list)}"
Expand Down
24 changes: 3 additions & 21 deletions lib/beacon_web/live/page_live.ex
Original file line number Diff line number Diff line change
Expand Up @@ -24,28 +24,10 @@ defmodule BeaconWeb.PageLive do
end

def render(assigns) do
{{site, path}, {page_id, _layout_id, format, page_module, _component_module}} =
lookup_route!(assigns.__site__, assigns.__live_path__)

{{site, path}, {page_id, _layout_id, format, page_module, _component_module}} = lookup_route!(assigns.__site__, assigns.__live_path__)
assigns = Phoenix.Component.assign(assigns, :beacon_path_params, Beacon.Router.path_params(path, assigns.__live_path__))
template = do_render(site, page_id, page_module, assigns)
Lifecycle.Template.render_template(site, template, format, path: path, assigns: assigns, env: __ENV__)
end

# TODO: backpressure and move to a proper module
defp do_render(site, page_id, page_module, assigns) do
require Logger

case page_module.render(assigns) do
:not_loaded ->
Logger.debug("compiling #{page_module}")
page = Beacon.Content.get_published_page(site, page_id)
{:ok, page_module, _ast} = Beacon.Loader.PageModuleLoader.load_page!(page, :request)
page_module.render(assigns)

rendered ->
rendered
end
page = %Beacon.Content.Page{id: page_id, site: site, path: path, format: format}
Lifecycle.Template.render_template(page, page_module, assigns, __ENV__)
end

defp lookup_route!(site, path) do
Expand Down
22 changes: 0 additions & 22 deletions test/beacon/lifecycle/template_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -19,26 +19,4 @@ defmodule Beacon.Lifecycle.TemplateTest do
assert Lifecycle.Template.load_template(page) == "<div>beacon</div>"
end
end

describe "render_template" do
setup do
opts = [
path: "/test/lifecycle",
assigns: %{},
env: __ENV__
]

[opts: opts, format: :markdown, template: "<div>{ title }</div>"]
end

test "template must be valid heex", %{template: template, format: format, opts: opts} do
assert %Phoenix.LiveView.Rendered{static: ["<p>Beacon</p>"]} = Lifecycle.Template.render_template(:lifecycle_test, template, format, opts)
end

test "render must return a Phoenix.LiveView.Rendered struct", %{template: template, format: format, opts: opts} do
assert_raise Beacon.LoaderError, ~r/Return output must be of type Phoenix.LiveView.Rendered.*/, fn ->
Lifecycle.Template.render_template(:lifecycle_test_fail, template, format, opts)
end
end
end
end
15 changes: 3 additions & 12 deletions test/beacon/loader/page_module_loader_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -151,18 +151,9 @@ defmodule Beacon.Loader.PageModuleLoaderTest do
{:ok, module, _ast} = PageModuleLoader.load_page!(page)

assert [
{"primary", nil,
%Phoenix.LiveView.Rendered{
static: ["<main>\n <h1>my_site#home</h1>\n</main>"]
}},
{"variant_a", 1,
%Phoenix.LiveView.Rendered{
static: ["<div>variant_a</div>"]
}},
{"variant_b", 2,
%Phoenix.LiveView.Rendered{
static: ["<div>variant_b</div>"]
}}
%Phoenix.LiveView.Rendered{static: ["<main>\n <h1>my_site#home</h1>\n</main>"]},
{1, %Phoenix.LiveView.Rendered{static: ["<div>variant_a</div>"]}},
{2, %Phoenix.LiveView.Rendered{static: ["<div>variant_b</div>"]}}
] = module.templates(%{})
end
end
Expand Down

0 comments on commit 862603f

Please sign in to comment.