Empty socket and mount on live_patch?

So, I’m trying to arrange my live routes in approximately this fashion:

live_session :default do
  live "/feed", FeedLive
  live "/status", StatusLive
end

I have a live component navigation menu in both FeedLive and StatusLive, but when I try to navigate from FeedLive to StatusLive with a live_patch (or the other way around), mount gets called and the socket given to mount doesn’t have assigns. I was under the impression it would be possible to live_patch within the same live_session and only handle_params/3 gets called. Am I mistaken or is there some bug in my code?

Also, is there some definitive guide on routing for LiveView? PragmaticStudio videos make use of URL query parameters to navigate, but it’s messy in my opinion. I’d prefer to define live routes in router.ex, but this live_patching issue is preventing it. So, if there’s some best practice on the issue, I’d appreciate any info.

I think you’re getting the use cases of live_redirect/2 and live_patch/2 mixed up :slight_smile:

In order to navigate between live views, regardless of whether they are in the same live_session or not, you have to use live_redirect/2 and not live_patch/2. You use live_patch/2 when your link goes into the current live view (you’re “patching” the view by passing it new parameters).

As for a good overview of navigation in LiveView, I’ve found this section in the docs to be quite good: Live navigation — Phoenix LiveView v0.17.5

1 Like

Yes, looks like it. It’s a bit disappointing you can’t patch between live modules. User and user-related info has to be reloaded from the DB at every redirect, which is a waste of bandwidth.

You could resort to other strategies in order not to reload everything from the database. For example you could store user-related information in the session.