How to disable WebSocket in LiveView to test static rendering?

I want to test how my statically rendered site looks like before a WebSocket connection is established. Is there an easy way to disable the “hydration” part of the LiveView life-cycle? That is, prevent LiveView from establishing a WebSocket connection (or at least delay it by a lot?).

I tried the following so far:

  • Add window.WebSocket = undefined before the
    let liveSocket = new LiveSocket(...) call in app.js
    - This prevents LiveView from using WebSockets, but causes it to fall back to longpolling. If longpolling isn’t implemented, the site refreshes every second and retries the longpolling.
  • Add a timeout when the socket is connected:
def handle_params(_params, _url, socket) do
  if connected?(socket), do: :timer.sleep(10_000)

  {:noreply, socket}
end

This actually works quite well, but I wondered whether there’s a setting to disable the WebSocket connection entirely?

1 Like

How about not starting the LiveSocket?

2 Likes

D’oh! That was too obvious for me to see :smiley:

I commented out the

liveSocket.connect()

part in app.js and indeed, now it’s not establishing a WebSocket connection. So simple!

Thanks for pointing this obvious solution out to me :heart:

1 Like