Layouts specified in `live_session` (LiveView 0.18.3, Phoenix 1.7.0-rc.0) and first renders

I’ve been working on a project that started as 1.16 as a REST API. We’re in the process of adding UIs and with all the changes to in 1.7, we opted to use the release candidate since we can take advantage of things like verified routes now. (I suspect it’ll be production ready before we are, lol.)

1.7 changes things about layouts and I’m trying to make sure I both understand things and want to clear up my own confusion about others.

So far as I can tell, in the “live” portion of a LiveView, layout precedence happens as follows:

  1. An option in on_mount. So, {:ok, socket, layout: ...}
  2. An option in the router with live_session :default, layout: ...
  3. An option in use Phoenix.LiveView, layout: ...

With the design we’re going with, I would actually like to specify the layouts in the router within the context of a live_session. I can reuse LiveViews in different contexts with different layouts. This is actually really, really slick for how we’re building some things.

The trouble I’m running into at the moment, and I’m not sure if this is a bug or my own misunderstanding about live_session, is this:

On the first render, a layout specified in live_session is not used. It will fall back to whatever is specified in use Phoenix.LiveView, layout: ... or the option set in on_mount. Is the the intended behavior or do I need to post an issue?

2 Likes

I was also finding the same. I would get a temporary flash of the wrong layout layout as specified in use Phoenix.LiveView, layout: ... via the MyAppWeb.live_view/0 function. Then it the layout specified in the router with live_session :default, layout: ... would kick in.

Where did you get the information about the precedence, though? I figured I was just doing it wrong. I agree that properly being able to specify it in the router would be awesome.

1 Like

I got the precedence info by experimentation, lol.

I’m going to probably file a bug report tomorrow. This seems like a bug.

1 Like

Report here: Layouts with live_session don't work on initial mount · Issue #5182 · phoenixframework/phoenix · GitHub

1 Like