Rails: turbo-frame with src requested interpreted as format.html

2.9k views Asked by At

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

2

There are 2 answers

1
Nikita Fedyashev On

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.
0
Aiden Cullo On

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