rails / request.js

MIT License
389 stars 28 forks source link

Disabling Turbo? #42

Closed archonic closed 2 years ago

archonic commented 2 years ago

When request.js detects window.Turbo, I'm wondering how we can disable Turbo when calling something like patch? I would just call submit on a form with data-turbo: "false" but I have to manipulate form data with an image cropper.

marcelolx commented 2 years ago

Can you give a more detailed explanation of what you're trying to achieve? request.js will only call Turbo to process the response of your request if the response is a turbo-stream which means that in your request you would be making a request like this with responseKind: 'turbo-stream'

patch('/endpoint', {
  body: {},
  responseKind: 'turbo-stream'
})

Do you want Turbo not to process your response but still receive a turbo-stream as a response?

archonic commented 2 years ago

That's not what I was seeing. I had this:

request = patch( path, {
  body: formData
})

And that was taking over the request and response of a redirect. I'll see if I can create a minimal reproduction soon.

marcelolx commented 2 years ago

Ok so after making the request request.js was following the redirect do you mean? This is not something related to Turbo, it is the default config that request.js assumes see https://github.com/rails/request.js/blob/main/src/fetch_request.js#L129 which is the default of the Fetch API https://developer.mozilla.org/en-US/docs/Web/API/Request/redirect

archonic commented 2 years ago

The problem is that it's not rendering the redirect and that's Turbo behaviour. I didn't think Turbo would interfere unless I had specified the responseKind but I noticed that it was following the redirect but not rendering it. I added a turbo:before-visit event listener and it is being triggered for the patch request I mentioned above.

marcelolx commented 2 years ago

Yeah it is following the redirect because that's something Fetch API does, it doesn't render because it isn't a functionality of request.js

About turbo:before-visit being triggered I'm not sure about, not sure about the internals of Turbo work around this.

The only thing that Turbo does when you specify turbo-stream as the responseKind is process turbo streams that that given response contains.

Probably you don't want to let Fetch API follow the redirect and do it your own?

marcelolx commented 2 years ago

Closing this since it does not seem a problem with Request.JS