question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Drive is including `Accept: text/vnd.turbo-stream.html` on visits that are not inside of a Frame

See original GitHub issue

We are working on transitioning a JS-heavy filtered list to Turbo Frames + Streams, and something we’ve run into seems like it might be a bug in Turbo.

We have a DealsController#index action that is one of the primary parts of our application, with a link in our main navigation to it like this:

<a href="/deals">Deals</a>

We’ve got Turbo Drive set up for the application, so with no introduction of Frames/Streams, this works perfectly, it updates the <body> through Drive as expected. Then we started introducing <turbo-frame>s to support things like infinite scrolling of “deals”, with our index.html.erb looking something like this:

<%= turbo_frame_tag 'deals', target: '_top' do %>
  <%= render @deals %>
<% end %>

<%= turbo_frame_tag 'deals-next-page', src: url_for(page: @page + 1), loading: 'lazy' %>

And an index.turbo_stream.erb that looks something like this:

<%= turbo_stream.append 'deals' do %>
  <%= render partial: @deals, formats: %i[html] %>
<% end %>

<%= turbo_stream.replace 'deals-next-page' do %>
  <%= turbo_frame_tag 'deals-next-page', src: url_for(page: @page + 1), loading: 'lazy' %>
<% end %>

However, when we introduce index.turbo_stream.erb, our navigation link (which is outside of any <turbo-frame> and should behave just as a simple Drive request) no longer works as expected. Instead of getting the full HTML response from the server and replacing the <body>, it receives index.turbo_stream.erb, which makes for all sorts of weird issues when clicking that navigation link from other pages (i.e. from our home page, where there is no <turbo-frame id: 'deals'> element on the page).

After looking into this a bit more, I’m pretty sure it’s because Turbo Drive is including the Accept: text/vnd.turbo-stream.html header with all of its requests, which is causing my controller to respond with that format if it’s available.

When clicking a link (or submitting a form) inside of a <turbo-frame> (without a target='_top'), that makes perfect sense to me, but I’m surprised it does this when clicking a link anywhere else on the page. In that case, I’d expect Turbo Drive to do its thing, and simply fetch the standard HTML response from the server, without the text/vnd.turbo-stream.html content type included in the Accept header.

Am I misunderstanding something basic about Turbo, or is this something that should be fixed?

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:7 (5 by maintainers)

github_iconTop GitHub Comments

2reactions
spinosacommented, Feb 19, 2021

@agrobbin after the change introduced in #52 , does your infinite scroll approach not break?

The initial page load would render index.html.erb with something like:

<%= turbo_frame_tag 'deals', target: '_top' do %>
  <%= render @deals %>
<% end %>

<%= turbo_frame_tag 'deals-next-page', src: url_for(page: @page + 1), loading: 'lazy' %>

just fine.

But the Fetch initiated by <%= turbo_frame_tag 'deals-next-page', src: url_for(page: @page + 1), loading: 'lazy' %> no longer has the Accept: text/vnd.turbo-stream.html header, and so loads index.html.erb instead of the desired index.turbo_frame.erb. Dose it not?

0reactions
spinosacommented, Feb 22, 2021

@agrobbin I ended up with a similar refactoring.

The paged index route implemented with Turbo Streams and overloaded views is elegant; 🙏 #146

Read more comments on GitHub >

github_iconTop Results From Across the Web

Turbo new install - forms do not redirect · Issue #122 - GitHub
I'm able to interact with Turbo as expected - ie, I have a frame ... meaning the Accept header contains the text/vnd.turbo-stream.html media ......
Read more >
Navigate with Turbo Drive - Hotwire Turbo
Turbo Drive models page navigation as a visit to a location (URL) with an action. Visits represent the entire navigation lifecycle from click...
Read more >
Dynamic forms with Turbo - Thoughtbot
During a frame's navigation, it issues an HTTP GET request based on the path or URL declared by its [src] attribute. The request...
Read more >
Turbo stream partial rendering as plain text in Rails 7
turbo_stream renders a partial with a <turbo-stream> tag (no layout, no javascript) and a header Content-Type: text/vnd.turbo-stream.html; .
Read more >
Almost JSless Frontend on Rails | Railsware Blog
1 Rails UJS. 2 Turbolinks. 3 What is not enough for us. 4 New tools from the Rails team. 4.1 Turbo. 4.1.1 Turbo...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found