Dynamic Live Routes

Hi guys,
I’d like to talk about my attempt to build a plugin mechanism for LiveComponents. My goal is to build a WebApp that goes through a LiveView and consists of several LiveComponents.

The LiveComponents are rendered in dependence of live_actions. Since the routes for the live_actions are defined at runtime, the router has to be recompiled as soon as a new plugin is added or triggered. Currently I use a /plugin folder within the project. To save the compiled modules I use an ETS cache. The plugins implement a behaviour so that the system can be completely agnostic and only renders what corresponds to the behaviour.

The router looks like this:

  scope "/", TestWeb do
    pipe_through :browser

    live "/", TestLive, :index
    live "/error", TestLive, :error

    for plugin <- TestWeb.PluginController.get_actions() do
      live "/#{plugin}", TestLive, plugin

    get "/recompile", PluginController, :recompile

What is currently still a stumbling block is the source code in the /plugins folder. This should not be left lying around forever, but should be compiled once and then load the beam files as needed (restart).

How do you find this method? Since Elixir is a language that has to be compiled first, this approach seems very clumsy to me. Maybe it could be done better. Criticism and suggestions for improvement are welcome :slight_smile: