Dynamic LiveView Routing

I have a situation in which users can create different content types (that need different LiveViews) and customize the content’s url path. So the paths “/path1” and “/path2” might be different types of content that need different templates. I need to look up the slug in the database to see what type of content it is.

One solution I thought of was to route all paths to one “dummy” LiveView and put the following code in an on_mount function for it:

    replacement_live_view = NewLiveViewModuleBasedOnContentType

    socket = %{
      socket |
       view: replacement_live_view,
       private: %{socket.private | root_view: replacement_live_view}
    }

Basically, the idea is to just update the socket’s view and private.root_view . This seems to work but I’m worried about the side effects of doing something like this – especially making changes to something in the private key. Also, for the initial HTTP request, I would have a plug that replaces the conn.private.phoenix_live_view:

    {live_view_module, param2, param3} = conn.private.phoenix_live_view
    replacement_live_view = NewLiveViewModuleBasedOnContentType

    %{
      conn
      | private: %{conn.private | phoenix_live_view: {replacement_live_view, param2, param3}}
    }

Are there any reasons to avoid “redirecting” the LiveView this way? Just worried about the side effects of updating “private” keys. Thanks!

2 Likes