LiveView: Feature to "cancel" second render

One thing I find is that I typically “start” with a LiveView even for static pages (in cases where I know I’m likely to add interactivity). I think it would be really awesome if we could do something like this:

def mount(...) do
  socket
  |> assign(...)
  |> cancel_second_render()
end

This would essentially say “this is a static page. Don’t connect to the web socket, just render the HTML and be done”.

This means that you can have the benefit of a static html page very easily, without redesigning everything as a “dead view”. This is especially useful when you have that feeling “I know some day later I’ll probably want this to be a liveview”.

13 Likes

I‘ve thought about this as well in the past, but with live navigation I’m wondering if not starting the websocket is actually useful. I agree that being able to skip the second render would be nice though, given it‘s mostly needed for change tracking and asserting confidence over the match between server side state and local state.

2 Likes

Curious how live navigation works in between the first and second render. Or just while the ws is not connected. Do they in some way fall back to “regular” links?

1 Like

If you simply don’t include the LiveView JS in the HTML of a page it won’t connect to websocket and won’t re-render.

1 Like

Ya I think the easy way to roll this is put a data element on the body tag or something that uses a “@nolive” assign you set on the static render. Then in the JS code you check for that to be true and if it is, just don’t run the LiveView JS code.

6 Likes

They fall back to normal links doing navigation through the browser and plain http. But if you can you don‘t want this to happen, but rather opt for what live navigation does, which only happens via the websocket connection, skipping the http level additional render. Switching constantly between live and non-live pages kindoff denies making use of that optimization.

1 Like

I wish we had separate load/3 and mount/3 functions. That plus being able to return json from render/1 would allow me to never use controllers again.

Yeah, thats an interesting idea :thinking: Next time I have a case for it I may try that out, because that could at least provide the concept. I think having it in core sends a signal that it is okay to actually do that kind of thing (i.e you won’t break anything by not running the second render), but it makes sense to try the manual approach first.

Along these lines I would love it if event handlers could somehow be routeable. This would allow for truly progressive web apps (if you need it). I know there would be issues with this—for example and code that uses send won’t work anymore and it will probably become a come repeated question on this forum—but it could allow using most features for static HTML + POSTs.

Something like:

# router

  live "/foos", FooLive do
    post "/", event: "save"
  end

# heex
  ~H"""
  <.form for={@form} phx-submit="save" action={~p"/foos"} method="post">
  """

Very much spitballing here though have been thinking about it a little bit.

2 Likes

You could check for the transport_id in the socket and pattern match on it.
If it is nil you are not yet connected.

Isn’t Phoenix.LiveView — Phoenix LiveView v1.0.0-rc.7 meant for this?

Yes I know, that’s where I got it from. But with checking this you could differentiate your mount/3 between the first unconnected render and the next.

1 Like

We’re talking about how to cancel the second render, not how to detect if we’re in it.

Sorry, I was replying to his separate load and mount functions.

Ohhhhh right my bad.

My point is that if the module doesn’t have a mount function it simply wouldn’t attempt to run code a second time anyway. There would be nothing to cancel @zachdaniel

"This means that you can have the benefit of a static html page very easily, without redesigning everything as a “dead view”. This is especially useful when you have that feeling “I know some day later I’ll probably want this to be a liveview”.

If this was implemented and make LV easily handle static pages then LV could be used as the default choice or would there stil be benefits to not use LV for something like a marketing/landing page?

I can’t think of a reason to use anything other than LV at that point :slight_smile:

3 Likes

If that’s the case then I’m surprised this suggestion & thread has not gotten more attention as it could have quite significant impact on both how LV itself is used and its role within phoenix?

I quite often see statments such as “LV is not good for this or that”, especially for static marketing or landing pages and this, if implemented, seems to then invalidate such arguments if I understand the situation correctly.

4 Likes

This is what I’d so as well. A dead render of a LiveView really is just a rendered HTML page. Nothing forces you to actually connect.

diff --git a/assets/js/app.js b/assets/js/app.js
index cfa128d..5b85399 100644
--- a/assets/js/app.js
+++ b/assets/js/app.js
@@ -33,8 +33,10 @@ topbar.config({barColors: {0: "#29d"}, shadowColor: "rgba(0, 0, 0, .3)"})
 window.addEventListener("phx:page-loading-start", _info => topbar.show(300))
 window.addEventListener("phx:page-loading-stop", _info => topbar.hide())
 
-// connect if there are any LiveViews on the page
-liveSocket.connect()
+// connect if there are any LiveViews on the page and no meta[name='no-live']
+if (!document.querySelector("meta[name='no-live']")) {
+  liveSocket.connect()
+}
 
 // expose liveSocket on window for web console debug logs and latency simulation:
 // >> liveSocket.enableDebug()
diff --git a/lib/demo_app_web/components/layouts/root.html.heex b/lib/demo_app_web/components/layouts/root.html.heex
index 436ea06..5990b0b 100644
--- a/lib/demo_app_web/components/layouts/root.html.heex
+++ b/lib/demo_app_web/components/layouts/root.html.heex
@@ -4,6 +4,7 @@
     <meta charset="utf-8" />
     <meta name="viewport" content="width=device-width, initial-scale=1" />
     <meta name="csrf-token" content={get_csrf_token()} />
+    <meta :if={assigns[:no_live]} name="no-live" content="true" />
     <.live_title default="DemoApp" suffix=" · Phoenix Framework">
       {assigns[:page_title]}
     </.live_title>

And then just assign(socket, :no_live, true) and you’re good to go. I don’t think this needs a more “official” way to do.

4 Likes