improve iOS Safari LiveView reliability

increase longPollFallbackMs from 2500 to 5000 to prevent premature
fallback on mobile networks, and add visibilitychange handler to
reconnect WebSocket when returning from a backgrounded tab
This commit is contained in:
Graham McIntire 2026-03-11 13:23:26 -05:00
parent 61368e5c5e
commit 7a83b13faa
No known key found for this signature in database

View file

@ -1205,7 +1205,7 @@ const StatusTitle = {
}
const liveSocket = new LiveSocket("/live", Socket, {
longPollFallbackMs: 2500,
longPollFallbackMs: 5000,
params: { _csrf_token: csrfToken, timezone: userTimezone },
hooks: { ...colocatedHooks, SensorChart, CopyToClipboard, ScrollToTop, AutoDismissFlash, BetaBannerDismiss, NetworkMap, SitesMap, LeafletMap, DeviceListReorder, MikrotikPortSync, GlobalSearch, GlobalSearchTrigger, DynamicFavicon, StatusTitle },
})
@ -1266,6 +1266,15 @@ window.addEventListener("phx:download", (event: any) => {
window.URL.revokeObjectURL(url)
})
// Reconnect WebSocket when returning from background (iOS Safari optimization)
document.addEventListener("visibilitychange", () => {
if (document.visibilityState === "visible") {
if (liveSocket.socket.connectionState() !== "open") {
liveSocket.socket.disconnect(() => liveSocket.socket.connect())
}
}
})
// connect if there are any LiveViews on the page
liveSocket.connect()