Redirecting causing the request to fire twice

hey :wave:t2:

I have a language selector like this:

<% I18n.available_locales.each do |locale| %>
  <% if I18n.locale != locale %>
    <li>
      <%= button_to locale_flag(locale), locale_settings_path(locale: locale), method: :put, form: { data: { turbo_confirm: t('application.confirm'), class: 'btn btn-square glass text-2xl' } %>
    </li>
  <% end %>
<% end %>

and the corresponding controller does this:

class SettingsController < ApplicationController
  def locale
    cookies[:loc] = params[:locale] if I18n.locale_available?(params[:locale])

    redirect_to :root, status: :see_other
  end
end

I noticed that for some reason, the root is rendered twice:

14:23:05 web.1  | Started PUT "/settings/locale?locale=pt-BR" for 127.0.0.1 at 2023-01-15 14:23:05 -0300
14:23:05 web.1  | Processing by SettingsController#locale as TURBO_STREAM
14:23:05 web.1  |   Parameters: {"authenticity_token"=>"[FILTERED]", "locale"=>"pt-BR"}
14:23:05 web.1  | Redirected to http://127.0.0.1:3000/
14:23:05 web.1  | Completed 303 See Other in 1ms (Allocations: 382)
14:23:05 web.1  |
14:23:05 web.1  |
14:23:05 web.1  | Started GET "/" for 127.0.0.1 at 2023-01-15 14:23:05 -0300
14:23:05 web.1  | Processing by MessagesController#index as TURBO_STREAM
14:23:05 web.1  |   Rendering layout layouts/application.html.erb
14:23:05 web.1  |   Rendering messages/index.html.erb within layouts/application
14:23:05 web.1  |   Rendered loader/_component.html.erb (Duration: 0.6ms | Allocations: 324)
14:23:05 web.1  |   Rendered turnstile/_component.html.erb (Duration: 0.4ms | Allocations: 319)
14:23:05 web.1  |   Rendered messages/index.html.erb within layouts/application (Duration: 3.0ms | Allocations: 1883)
14:23:05 web.1  |   Rendered locale/_component.html.erb (Duration: 0.7ms | Allocations: 561)
14:23:05 web.1  |   Rendered layouts/_nav.html.erb (Duration: 1.2ms | Allocations: 834)
14:23:05 web.1  |   Rendered layouts/_footer.html.erb (Duration: 0.6ms | Allocations: 194)
14:23:05 web.1  |   Rendered layout layouts/application.html.erb (Duration: 9.3ms | Allocations: 6760)
14:23:05 web.1  | Completed 200 OK in 11ms (Views: 10.3ms | Allocations: 7480)
14:23:05 web.1  |
14:23:05 web.1  |
14:23:05 web.1  | Started GET "/" for 127.0.0.1 at 2023-01-15 14:23:05 -0300
14:23:05 web.1  | Processing by MessagesController#index as HTML
14:23:05 web.1  |   Rendering layout layouts/application.html.erb
14:23:05 web.1  |   Rendering messages/index.html.erb within layouts/application
14:23:05 web.1  |   Rendered loader/_component.html.erb (Duration: 0.1ms | Allocations: 144)
14:23:05 web.1  |   Rendered turnstile/_component.html.erb (Duration: 0.1ms | Allocations: 141)
14:23:05 web.1  |   Rendered messages/index.html.erb within layouts/application (Duration: 0.8ms | Allocations: 937)
14:23:05 web.1  |   Rendered locale/_component.html.erb (Duration: 0.2ms | Allocations: 334)
14:23:05 web.1  |   Rendered layouts/_nav.html.erb (Duration: 0.3ms | Allocations: 438)
14:23:05 web.1  |   Rendered layouts/_footer.html.erb (Duration: 0.2ms | Allocations: 67)
14:23:05 web.1  |   Rendered layout layouts/application.html.erb (Duration: 4.2ms | Allocations: 4793)
14:23:05 web.1  | Completed 200 OK in 5ms (Views: 4.4ms | ActiveRecord: 0.0ms | Allocations: 5079)

I’m not really sure what is causing this but setting data: { turbo: false } fix it.

wonder if anyone has seen something like this before

just found the “problem”: I have data-turbo-track="reload" in one of my scripts (Cloudflare’s Turnstile). It triggers a full page reload because the script address changes when the language changes. I guess I can live with it since this index page does nothing other than just rendering a simple form