Hotwire Discussion

Updating multiple elements in Turbo stream response - a good practice?

I am doing an “infinite scroll” sort of page where the user can load more elements into a list. On the page, there is an element that holds the last item ID on list so that when an update is request, Rails can determine which item will be the next one in the sequence that should be returned. Of course, when the new data is appended to the list, that last item ID needs to be updated for the next possible request.

I was looking into whether I can send back two pieces of data for every Turbo stream request:

  1. The Turbo stream append operation to add the new items to the list
  2. A Turbo stream replace operation to update the placeholder for the last item on the list

I found the following Github post where DHH seems to suggest doing multiple updates via Turbo stream is a discouraged practice. However, I was unclear whether he was referring to the OP’s syntax suggestion or the general concept of performing multiple Turbo stream operations in a single response.

Could you please provide better guidance on the best practice where you might want to do more than one thing in a Turbo response? Like deleting some items while adding others? Or, is this a case where the aid of Stimulus is the best option?

1 Like

The original post is about “Syntax for returning multiple turbo streams” and recommending the syntax be placed in the controller vs erb. They wrote.

you could also do it through a .turbo_stream.erb file, but I like having this in the controller

To me it appears that dhh is responding to this particular controller vs erb comment, but I could be wrong.

1 Like

Is there a best practice on how to solve this?

Hi,

I don’t know if it’s a good or a best practice but it is possible :slight_smile:

" I found the following Github post where DHH seems to suggest doing multiple updates via Turbo stream is a discouraged practice. However, I was unclear whether he was referring to the OP’s syntax suggestion or the general concept of performing multiple Turbo stream operations in a single response."

→ “this is much worse than using a template”

I think, he mean it’s recommended to use a template for this and not to overload the controller.

1 Like