grych / drab

Remote controlled frontend framework for Phoenix.

Home Page:https://tg.pl/drab

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Support for hot code upgrade in production

guidotripaldi opened this issue · comments

If an app in production is hot upgraded to a new version, Drab rises an alert with
"An error occured. Please contact the System Administrator" error message each time the user will try to interact with a Drab powered template.

This because in Drab.Config.drab_extension() the function Application.fetch_env(:phoenix, :compiled_template_engines) is used to retrieve the Drab custom engine, but this function returns nil after a hot code upgrade, so Drab will fail.

  def drab_extension() do
    {drab_ext, Drab.Live.Engine} =
      :phoenix
      |> Application.get_env(:compiled_template_engines)
      |> Enum.find(fn {_, v} -> v == Drab.Live.Engine end)

    "." <> to_string(drab_ext)
  end

To safely get the list of compiled engines, the officially available API Phoenix.Template.engines/0 should be used instead. Internally, it dynamically rebuilds the list when necessary, and makes it statically available to the subsequent calls through Application.fetch_env(:phoenix, :compiled_template_engines).

  def drab_extension() do
    {drab_ext, Drab.Live.Engine} =
     Phoenix.Template.engines()
     |> Enum.find(fn {_, v} -> v == Drab.Live.Engine end)

    "." <> to_string(drab_ext)
  end

PR #190 apply the suggested patch.

Looks great and simple, #190 closes this. :-)