Turbo-stream inside turbo-frame response. Is this a legit pattern?

I saw somewhere that someone was putting a tubro_stream inside a turbo_frame response for a standard http GET request. I don’t think it was a form submission, just a GET.

Is this valid? The docs make it sound like turbo-stream are only for broadcasting and form submissions, not standard turbo_frame requests.

It’s legit because even the server-broadcasted format.turbo_stream use the javascript magic that adding a turbo_stream inside a turbo_frame does.

Both end up having a <turbo-stream><template></template></turbo-stream> element being added to the page, getting picked up by the JavaScript magic (a MutationObserver) that then either appends or replaces or updates elements on the page with the same id or classes.

So it’s legit, and sometimes it allows you to package both the updated HTML inside the turbo_frame itself and updating a different part of the page (e.g. adding a flash message somewhere else on the page) in the same response.

1 Like

Interesting. So that makes me wonder why we respond to form submission differently (in a Rails application) then other requests.

For example, for a form submission we do

render turbo_stream: render turbo_stream: turbo_stream.replace(...)

But we simply render a template for turbo_frame requests.

My understanding is that these methods are not interchangeable, but your answer makes me wonder why not, given they are essentially the same in the end result.