I have tried several examples for implementing an ‘infinite scrolling feed’ using liveview, but none of them seem to work if a user is expected to navigate away from the feed at some point, and then come back to it again through the browser’s back button.
In all of the examples I have seen, the feed relies on temporary assigns
, and a phx-update="append"
flag on the feed. But if the user ever navigates ‘back’ to the feed, after scrolling down and tapping on something, they are dropped back to the very beginning of the feed, losing all of their scroll history.
This is due to the way the state is reset when mount is called after the websocket reconnection, e.g. from this example`:
def mount(_params, _session, socket) do
{:ok,
socket
|> assign(page: 1, per_page: 10)
|> fetch(), temporary_assigns: [users: []]}
end
The user drops back to “page 1” whenever mount
is called, even if that is just when the websocket reconnects after a ‘back’ navigation.
I tried moving the state (the page
variable) to the client, so that the socket’s assigns
doesn’t have to worry about it, but unfortunately LiveView still clears the container that’s previously been populated from the temporary_assigns
variable, after the websocket reconnect.
Is that a bug, or is there some other way to tell LiveView to leave that HTML section in-place so that it can be appended to later once the user scrolls further?