Hotwire Discussion

Turbo_stream renders when I'm not expecting it

I have a landing_pages controller with index action:

  def index
    respond_to do |format|
      format.html {}
      format.turbo_stream {}
    end
  end

When I click on the link to the landing_page index

= link_to "Landing Pages", admin_landing_pages_path

It first renders the index page (what I am expecting) and then it will also render the turbo_steam template.

This is strange to me as I’m checking the Mime Type in respond_to do |format| and it’s empty:

#<ActionController::MimeResponds::Collector:0x00007fdc3285e830 @responses={}, @variant=[]>

I have to add that this happens only when coming from a particular page which has a turbo_stream element on it.

EDIT:
After adding turbo: false to the link it stopped firing the turbo_stream. Is this expected? Very confusing for me.

= link_to "Landing Pages", admin_landing_pages_path, data: { turbo: false }
1 Like

Yes. When clicking a turbo-enabled link/form/element. If the controller action serves both html and turbo_stream formats. Because when the link/form/element is clicked, it fires a turbo_stream request. The controller action will prefer to respond with turbo_steam format.

So, for it to navigate and perfer the html response type, you need to pass the turbo: false to the element. Or, you can also have an optional query param present if you don’t like the browser reload thingi.

 def index
    respond_to do |format|
      format.html {}
      format.turbo_stream {} if params[:some_query_param].present?
    end
  end

Hmm, I’m not sure @rockwell. The controller should only ever send back a single response, either HTML or stream.

@HalfBloody it would be helpful to see a screenshot of the network tab of your dev tools while replicating the issue. That would make it more clear what’s going on.

But it can have both as well. I imagine something like index.html for the html view, and index.turbo_stream used for pagination.

Oh yes but that requires 2 separate requests.

1 Like