Repository navigation
Defer phx-disconnected while the page is hidden - #4505
Open
BryanJBryce wants to merge 1 commit into
Open
BryanJBryce wants to merge 1 commit into
BryanJBryce wants to merge 1 commit into
Conversation
When the connection drops in a background tab, phx-disconnected commands ran after 500ms even though nobody could see them, and phoenix.js only reconnects once the page is visible again. Users returning to the tab were greeted with an error toast while the socket reconnected. phx-disconnected commands now wait until the page is visible and then for the new disconnectedResumeTimeout option (2000ms by default), so the immediate reconnect can finish first. The error classes are still applied right away. Also mark join timeouts and transport failures as phx-client-error instead of phx-server-error, matching the network attribution already used for diagnostics. Co-Authored-By: Claude Opus 5.5 <[email protected]>
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
Coming back to a LiveView tab that has been in the background often shows the
generated "Something went wrong! Attempting to reconnect" toast, even though
the connection recovers a moment later. It makes a healthy app look broken.
This happens because:
View.delayedDisconnectedrunsphx-disconnectedcommandsdisconnectedTimeout(500ms) after a connection error, regardless of pagevisibility.
(
reconnectWhenHiddendefaults to false), and reconnects immediately onvisibilitychange.So for any drop that happens while the tab is hidden (laptop sleep, network
change, a server deploy, the browser freezing the tab), the disconnected UI is
always on screen when the user returns, and they watch the full reconnect,
join and render with an error toast up.
Separately,
onJoinErrorsetsphx-server-errorfor join timeouts andtransport failures, even though it already attributes them to the network for
diagnostics. With the generated layout this shows "Something went wrong!" when
the server could not be reached at all.
Changes
phx-disconnectedcommands never run while the page is hidden. When the pagebecomes visible again, they wait for the new
disconnectedResumeTimeoutLiveSocket option (2000ms by default), giving the immediate reconnect time to
finish. If the page is hidden again before the timer fires, they are deferred
again. Reconnecting (
hideLoader) or destroying the view cancels them.phx-loading,phx-errorand error-kind classes and thephx:page-loading-startevent are still applied immediately, so CSS and thegenerated top bar can still signal that the page is not live.
onJoinErrorusesphx-client-erroranderrorKind: "client"when the jointimed out or failed in the transport.
Listening for
resumein addition tovisibilitychangemirrors the phoenix.jsworkaround for Chrome not reliably firing
visibilitychangewhen unfreezing apage.
Compatibility
Apps that use
phx-disconnectedfor something other than UI will see it runlater for drops that happen in background tabs: on return plus the resume
timeout, instead of 500ms after the drop. Apps that need the old timing can
listen for
phx:page-loading-startwithkind: "error", which still firesimmediately.
Testing
assets/test/view_test.tscover deferral while hidden, theresume timeout, reconnecting before and after the page is visible, hiding
again mid-timer,
resumeevents, destroy, and the join-error classes. Theyfail without the change.
npm run js:test,npm run js:lintandnpm run typecheck:testspass.https://github.com/BryceLabs/liveview-reconnect-ux
Possible follow-up in the Phoenix generator
The generated
flash_groupshows the same red error toast for a shortreconnect and a real outage. The demo app also has a quieter two-stage
indicator (
?ui=pill): a neutral "Reconnecting…" pill that expands with amessage and a Reload button if the connection is still down 5 seconds later.
It only uses the existing bindings, so it could be a separate Phoenix PR.
🤖 Generated with Claude Code