Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Awkward behavior around redirects & error codes #34

Closed
bhaibel opened this issue Dec 24, 2020 · 6 comments
Closed

Awkward behavior around redirects & error codes #34

bhaibel opened this issue Dec 24, 2020 · 6 comments

Comments

@bhaibel
Copy link

bhaibel commented Dec 24, 2020

I'm trying to Turboify an app which uses Devise.

When I try to sign out, I expect the following to happen.

  1. Turbo sends an xhr request to the appropriate Devise controller.
  2. Devise does its post-logout redirect.
  3. Eventually, the redirect chain resolves to HTML containing the signin page.
  4. That HTML gets returned in the xhr response.
  5. Turbo renders the signin page.

Instead, the following happens:

  1. Turbo sends an xhr request to the appropriate Devise controller.
  2. Devise does its post-logout redirect.
  3. Eventually, the redirect chain resolves to a bare 401, because something is autodetecting the XHR and deciding not to return HTML.
  4. No content gets returned to the XHR response, just an error code.
  5. Turbo doesn't render anything, because it got no HTML.
  6. I click on literally any other link, or reload, or the like.
  7. I am immediately redirected to the sign in page with an error.

For my use case, I can probably fix it by overriding the Devise redirect chain -- it's annoying, but not unsolvable.

However, I can see this kind of interaction being really confusing to a naive user. It doesn't really signal what the issue is, it's just a silent failure.

@dhh
Copy link
Member

dhh commented Dec 25, 2020

Would if Devise has something in place to detect all requests that come from fetch as XHR somehow, and then not do the HTML rendering if then? Maybe dive into that Devise chain and lets see if its something we have to fix there or if there's anything we can do on the turbo side.

seanpdoyle added a commit to hotwired/turbo that referenced this issue Dec 26, 2020
Related to hotwired/turbo-rails#12
Related to hotwired/turbo-rails#34

Typically, Turbo expects each FormSubmission request to result in a
redirect to a new Location.

When a FormSubmission request fails with an [unprocessable entity][422]
response, render the response HTML. This commit brings the same behavior
to `<turbo-frame>` elements.

[422]: https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/422
seanpdoyle added a commit to hotwired/turbo that referenced this issue Dec 26, 2020
Related to hotwired/turbo-rails#12
Related to hotwired/turbo-rails#34

Typically, Turbo expects each FormSubmission request to result in a
redirect to a new Location.

When a FormSubmission request fails with an [unprocessable entity][422]
response, render the response HTML. This commit brings the same behavior
to `<turbo-frame>` elements.

[422]: https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/422
@woto
Copy link

woto commented Dec 26, 2020

I remember that old turbolinks/turbolinks-classic had special code for Devise:

By default, Turbolinks includes itself in ActionController::Base. To opt out of the Turbolinks features in certain controllers (redirect_to behavior, request_method cookie, X-XHR-Referer referrer check, etc.), set config.turbolinks.auto_include to false in application.rb and include Turbolinks::Controller in the controllers where you use Turbolinks.

And Devise even has some hint here on how to use it

Also temporary stucked with Devise redirects :)

miharekar added a commit to miharekar/visualizer that referenced this issue Dec 29, 2020
seanpdoyle added a commit to hotwired/turbo that referenced this issue Dec 30, 2020
Related to hotwired/turbo-rails#12
Related to hotwired/turbo-rails#34

Typically, Turbo expects each FormSubmission request to result in a
redirect to a new Location.

When a FormSubmission request fails with an HTTP Status code between
400-499 or 500-599 (e.g. [unprocessable entity][422]), render the
response HTML. This commit brings the same behavior to `<turbo-frame>`
elements.

[422]: https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/422
seanpdoyle added a commit to hotwired/turbo that referenced this issue Dec 30, 2020
Related to hotwired/turbo-rails#12
Related to hotwired/turbo-rails#34

Typically, Turbo expects each FormSubmission request to result in a
redirect to a new Location.

When a FormSubmission request fails with an HTTP Status code between
400-499 or 500-599 (e.g. [unprocessable entity][422]), render the
response HTML. This commit brings the same behavior to `<turbo-frame>`
elements.

[422]: https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/422
domchristie pushed a commit to domchristie/turbo that referenced this issue Jan 3, 2021
Related to hotwired/turbo-rails#12
Related to hotwired/turbo-rails#34

Typically, Turbo expects each FormSubmission request to result in a
redirect to a new Location.

When a FormSubmission request fails with an HTTP Status code between
400-499 or 500-599 (e.g. [unprocessable entity][422]), render the
response HTML. This commit brings the same behavior to `<turbo-frame>`
elements.

[422]: https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/422
@dhh
Copy link
Member

dhh commented Jan 5, 2021

You can now render on POST using either 4xx or 5xx status codes. In Rails, you'd do:

    respond_to do |format|
      if @room.save
        format.html { redirect_to @room, notice: 'Room was successfully created.' }
        format.json { render :show, status: :created, location: @room }
      else
        format.html { render :new, status: :unprocessable_entity }
        format.json { render json: @room.errors, status: :unprocessable_entity }
      end
    end

Part of 0.5.3.

@dhh dhh closed this as completed Jan 5, 2021
@dhh dhh reopened this Jan 5, 2021
@dhh
Copy link
Member

dhh commented Jan 5, 2021

Wups, posted on the wrong issue. But you can now opt out of Turbo processing using the data-turbo=false attribute. I believe that should probably solve this? Might want to add a doc note about it.

@miharekar
Copy link

In case someone lands here trying to get Devise working with Turbo - there's been several workarounds/improvements suggested on Hotwire Discussion.

There's even a GoRails video with instructions.

@davegudge
Copy link
Contributor

The responders used by Devise have been updated heartcombo/responders#223 (pending a new release). Once released, the open PR for Devise can be finalised heartcombo/devise#5340

@dhh dhh closed this as completed Apr 8, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

5 participants