Optimize Phoenix Component caching

This commit is contained in:
Max Veytsman
2022-09-02 21:01:48 -04:00
parent 1f8318db65
commit 0726bf6781
2 changed files with 43 additions and 49 deletions

View File

@ -71,13 +71,33 @@ defmodule Heroicons.Generator do
attrs = @assigns_to_attrs_mod.assigns_to_attributes(assigns)
assigns = @assign_mod.assign(assigns, :attrs, attrs)
{component, _binding} =
Code.eval_quoted(
Heroicons.Cache.fetch_component(path),
assigns: assigns
)
dynamic = fn track_changes? ->
changed =
case assigns do
%{__changed__: changed} when track_changes? -> changed
_ -> nil
end
component
attrs =
case Phoenix.LiveView.Engine.changed_assign?(changed, :attrs) do
true -> elem(Phoenix.HTML.attributes_escape(assigns.attrs), 1)
false -> nil
end
[attrs]
end
{icon_body, fingerprint} = Heroicons.Cache.fetch_icon(path)
%Phoenix.LiveView.Rendered{
static: [
"<svg",
icon_body
],
dynamic: dynamic,
fingerprint: fingerprint,
root: true
}
end
@doc false
@ -92,12 +112,13 @@ defmodule Heroicons.Generator do
{:safe, [?\s, safe_k, ?=, ?", safe_v, ?"]}
end
{icon_body, _fingerprint} = Heroicons.Cache.fetch_icon(path)
{:safe,
[
"<svg",
Phoenix.HTML.Safe.to_iodata(attrs),
" ",
Heroicons.Cache.fetch_body(path)
icon_body
]}
end
end