Skip to content

add auto_connect option to LiveView mount options #3704

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

SteffenDE
Copy link
Collaborator

Add auto_connect option to LiveView mount options to specify that a LiveView should not automatically connect on dead render. When navigated to and there's already a connection established, this option has no effect.

See https://elixirforum.com/t/liveview-feature-to-cancel-second-render/67770

@SteffenDE SteffenDE force-pushed the sd-phx-autoconnect branch from 107cb2f to 3464573 Compare March 5, 2025 10:37
This is useful when you have a static page that does not require
any connected functionality, but should render over the existing
connection when navigating from an already connected LiveView.
Defaults to `true`.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And what happens if auto_connect is false and I go to a page also with auto_connect=false, trying to use patch or navigate? I assume it does a full page navigation anyway? We should document this.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes. Each link will perform a full page reload.

Copy link
Member

@josevalim josevalim left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nits in the docs but, assuming it doesn't connect between auto_connect=false, it LGTM.

Copy link
Contributor

@rhcarvalho rhcarvalho left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am afraid this adds one more variant to a LiveView which makes it harder to reason about a LiveView's lifecycle.

In my mind a "live view" is meant to be connected.

to not automatically connect to the server on dead render.
This is useful when you have a static page that does not require
any connected functionality, but should render over the existing
connection when navigating from an already connected LiveView.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A LiveView with auto_connect: false is special in that its reactivity would depend whether you arrived over a regular HTTP request or live navigation.

What would happen if the socket disconnects for some reason (e.g. user drives through a tunnel or server is redeployed)?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the LiveView is connected once and disconnects, it will reconnect as usual. This only affects the very first page load.

@SteffenDE SteffenDE force-pushed the sd-phx-autoconnect branch from 2ca45d7 to 9d684d4 Compare April 16, 2025 16:55
@SteffenDE SteffenDE requested a review from chrismccord April 16, 2025 16:55
@SteffenDE SteffenDE force-pushed the sd-phx-autoconnect branch 2 times, most recently from 1552fa0 to e1e94dd Compare June 10, 2025 11:45
SteffenDE and others added 3 commits June 10, 2025 13:48
to specify that a LiveView should not automatically connect
on dead render. When navigated to and there's already a connection
established, this option has no effect.

See https://elixirforum.com/t/liveview-feature-to-cancel-second-render/67770
@SteffenDE SteffenDE force-pushed the sd-phx-autoconnect branch from e1e94dd to a8756c0 Compare June 10, 2025 11:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants