3

I've noticed this strange behavior of turbo frames loading lazily via src attribute setting:

nav
  = turbo_frame_tag 'primary_menu', src: primary_menu_path
  : def primary_menu
  :   respond_to do |format|
  :     format.turbo_stream
> :     format.html { binding.pry ; render plain: 'should not reach this block' }
  :   end
  : end

)> turbo_frame_request?
=> true

For some reason, it is interpreted as regular requested html request, although turbo_frame_request? returns => true. What's causing it and how to fix it?

P.S. primary_menu.turbo_stream.slim exists for that controller action.

Local versions:

hotwire-rails 0.1.3 @hotwired/turbo-rails 7.0.0-beta.5

BinaryButterfly
  • 18,137
  • 13
  • 50
  • 91

2 Answers2

2

Before someone with better knowledge of turbo internals explains it properly, below is a method that worked for me:

<%= turbo_frame_tag 'primary_menu', src: primary_menu_path %>

Would be interpreted as HTML request:

def primary_menu
  # load your thing

  respond_to do |format|
    format.html
  end
end

in your primary_menu.html.erb make sure you wrap your content with <%= turbo_frame_tag 'primary_menu' do %> block.

Keep in mind that:

  • turbo_frame_tag names have to match here and its original definition/require.
  • any additional turbo_frame_tag wrapped blocks in that response would be silently ignored.
BinaryButterfly
  • 18,137
  • 13
  • 50
  • 91
  • I had the same problem with you, but I'm using link_to. I was expecting a request with format TURBO_STREAM, but it was sending with format HTML. I think this is the normal comportment for turbo when is a GET request. – Pedro Gabriel Lima Jan 06 '22 at 13:28
0

This is expected behavior, as stated in the Turbo docs

By default, Turbo doesn’t add the text/vnd.turbo-stream.html MIME type when submitting links, or forms with a method type of GET

To override this behavior:

To use Turbo Streams responses with GET requests in an application you can instruct Turbo to include the MIME type by adding a data-turbo-stream attribute to a link or form.

It is also answered here

The reason for this is that for GET requests, it makes more sense to use a Turbo frame not stream

Aiden Cullo
  • 312
  • 3
  • 15