hotwired / turbo-rails

Use Turbo in your Ruby on Rails app
https://turbo.hotwired.dev
MIT License
2.07k stars 320 forks source link

`render` with non-200 status causing no layout and double loading of JS #348

Closed kleinjm closed 2 years ago

kleinjm commented 2 years ago

Context

Rails 7.0.2.4 Ruby 3.1.2 turbo-rails 1.1.1

I have a forbidden controller action that renders a page with a 403 response code.

  def forbidden(exception)
    @error_presenter = ErrorPresenter.new(status_code: :forbidden)

    # ...

    respond_to do |format|
      format.html { render(:base_error, status: :forbidden) } # this is the line in question
      format.all { head(:forbidden) }
    end
  end

Issue

When navigating from another page in my app to this forbidden page, the page renders without a layout and there are a series of JS errors related to double loading.

image (no formatting or layout)

image

Workarounds

There is no issue when,

  1. Removing import "@hotwired/turbo-rails"; in my JS pack file
  2. Removing status: :forbidden from the render call

Neither of these workarounds are acceptable long term.

dhh commented 2 years ago

Fixed in Turbo head via https://github.com/hotwired/turbo/pull/483.

kleinjm commented 2 years ago

I'm still seeing the issue after installing from that specific sha but will wait until the next official release and reopen if necessary. Thanks!