ivanminutillo

ivanminutillo

Mobile Tab Switching Triggers Full Page Reload in LiveView - Is This Expected Behavior?

Hi everyone,

I’m working on a Phoenix LiveView application and noticed an annoying behavior on mobile devices: whenever users switch tabs and return to the app, the entire page reloads even if it was fully loaded before. This happens consistently on both iOS and Android browsers.

The Issue

  • User loads a LiveView page on mobile
  • User switches to another browser tab or app
  • User returns to the original tab
  • The socket reconnects and triggers a full page reload, losing scroll position and any client-side state

This creates a poor user experience, especially on slower connections where users have to wait for the entire page to reload.

Our implementation uses standard LiveSocket configuration:

  let liveSocket = new LiveSocket("/live", Socket, {
    timeout: 60000,
    params: { _csrf_token: csrfToken },
    // ... standard configuration
  });

We don’t have any visibilitychange or pageshow/pagehide event handlers

Is this the default Phoenix LiveView behavior? Does LiveView intentionally trigger a full remount when reconnecting after tab switches?

Here a potential solution I’m considering

  // Detect tab visibility changes
  document.addEventListener("visibilitychange", () => {
    if (document.hidden) {
      // Gracefully disconnect?
      liveSocket.disconnect();
    } else {
      // Reconnect without full reload?
      liveSocket.connect();
    }
  });

But I’m unsure if this is the right approach or if it might cause other issues…

Most Liked

sabiwara

sabiwara

Elixir Core Team
arcanemachine

arcanemachine

I haven’t noticed this behaviour in my LiveView projects…

Can you try out my toy LiveView project and see if it still happens?

Also, here’s my liveSocket config:

https://github.com/arcanemachine/phoenix-quiz-game/blob/master/assets/js/app.js#L42

steffend

steffend

Phoenix Core Team

I think this is solved by Stop reconnecting when page is hidden by SteffenDE · Pull Request #6534 · phoenixframework/phoenix · GitHub - please try it using the branch from GitHub linked in the PR and hopefully we’ll have this properly fixed in the next release! During testing, I did NOT see the device doing a full page reload though, it only reconnected the socket, so there might be something else in play as well. I think the full reload might happen if there was a deploy in between, then it would be expected iirc.

I’ve been testing this with an Android device + USB debugging to attach to the devtools and inspect what’s happening when the device goes to sleep. Previously, we’d immediately try to connect again. In the WebSocket case the socket would stay in connecting state indefinitely and when unlocking the device the actual connection would take a very long time (for no apparent reason). For LongPoll, the device would backoff the HTTP requests and then also when unlocking the actual connection could take a very long time to actually send data (I ran tcpdump on the server side).

The fix prevents trying to reconnect when the page indicates that it is hidden and only does a new connection attempt as soon as it’s visible again. For both WebSocket and LongPoll, I can’t reproduce the stalling in that case. The page reconnects instantly as it should.

Where Next?

Popular in Questions Top

_russellb
I want to try my hand at web scraping. What tools/libraries do I need to use. I’m hoping to turn this into something professional so don’...
New
beno
I will often find my self writing things similar to: case some_value do nil -> something() "" -> something() _ -> somethi...
New
greenz1
I have a phoenix application from which a user can download multiple(5-6) files of size 1MB. I couldn’t find anything related to sending ...
New
pmjoe
I have a relationship of love and hate with Elixir. Lots of things are just absolutely right, but there are some things that are kind of ...
New
itssasanka
Hi all, Trying to get some more clarity over utc_datetime and naive_datetime for Ecto: The documentation above suggests that while ...
New
ycv005
I have followed this StackOverflow post to install the specific version of Erlang. And When I am running mix ecto.setup then getting fol...
New
freewebwithme
Using vs code and installed ElixirLS: support and debugger. And I got an error popped up on start up says Failed to run ‘elixir’ comma...
New
lucidguppy
I have a super simple question about elixir - how would I take a file like this foo bar baz and output a new file that enumerates th...
New
RisingFromAshes
I’ve read in another post that it may be possible with a router helper - but I couldn’t find an appropriate one, and tbh, I’m still just ...
New
script
If I have a string “1000 cfu/ml” . I want to remove the characters and / and space . So the string is like this "1000" What is the ...
New

Other popular topics Top

TunkShif
This post is an instruction guide to help you setup your Neovim for Elixir development from scratch. It includes general information on h...
274 42158 114
New
sen
Hi All, I set a environment variables in dev.exs , like below code. when i start server, how can i set the ${enable} value? thanks. d...
New
chrismccord
As promised, the first release candidate of Phoenix 1.3.0 is out! This release focuses on code generators with improved project structure...
New
grych
Hi folks, Few months ago I have announced the proof-of-concept of the library to manipulate the browsers DOM objects directly from Elixi...
639 52774 488
New
sergio_101
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
New
komlanvi
Hi everyone, I was playing with phoenix liveView but I run into an issue. I have a form and want to validate each input text when the te...
New
joaquinalcerro
Hi there, I am working with Ecto-Postgresql and I need to call all of the records from a specific table but the table has 40,000 records...
New
vegabook
I’m brand new to Phoenix and I have stripped one of the demo applications to the bone. I just want to get an svg up on the screen. Here i...
New
svb
Hi! Currently I want to submit a form by pressing the Enter key. However, since my input field is of type “textarea” this is just adds a...
New
vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New

We're in Beta

About us Mission Statement