Question regarding code organization/isolation best practices for a "spa-like" series of pages

Have you considered using multiple LiveViews within a live_session? That way each “subview” is just a LiveView with its own handle_params lifecycle callback. And by using live_session, you can also put shared mount logic into a shared on_mount hook for all the LiveViews in that live_session.

    live_session :default, on_mount: [MyAppWeb.SharedMount] do
      live "/", HomeLive, :home
      live "/my-page", PageLive, :my_page 
      live "/authorize", AuthLive, :authorize
    end

Naturally, there are some tradeoffs involved since each navigating within the live_session across its LiveViews will trigger mean another LiveView mount, but the websocket connection itself will be re-used across live navigation events. Plus, you could also check for no established socket via !connected? in the on_mount hook to conditionally run the shared logic that only needs to be run once at the beginning of the live_session.

1 Like