rails / request.js

MIT License
389 stars 28 forks source link

Adding X-Turbo-Request-Id support #73

Closed brendon closed 2 weeks ago

brendon commented 2 weeks ago

Turbo requests now keep track of a set of the last 20 or so requests and submits an ID for each turbo request to the server so that if a response is broadcast back to browsers the initiating browser can ignore the page reload update:

https://github.com/hotwired/turbo/blob/main/src/http/fetch.js

It would be useful to hook into this functionality in request.js. Would you be open to that?

Hopefully it's a case of just optionally importing the augmented fetch from the above file? I'm sure it could be complicated though :D

brendon commented 2 weeks ago

I've reported the spam comment.

In the meantime it might be easiest to allow for providing our own fetch function?

https://github.com/rails/request.js/blob/11a0cd92d69acc8ab3b5c163c7671e2461ca7505/src/fetch_request.js#L22

I can work around this by doing:

      const windowFetch = window.fetch
      window.fetch = fetch

      post(this.unlockUrlValue, { responseKind: 'turbo-stream' })

      window.fetch = windowFetch
marcelolx commented 2 weeks ago

So, could you explain the use case where we need the X-Turbo-Request-Id when making a request with request.js?

brendon commented 2 weeks ago

I'm using sortable.js and after a sort is done I want to submit a turbo request to tell everyone else (except the acting browser) to refresh their page to see the change in order. If the X-Turbo-Request-Id isn't sent along for the ride, the browser doesn't know to ignore the page refresh turbo-stream broadcast.

That's the simplest case that can't easily be done in another way.

brendon commented 2 weeks ago

Plus you provide the ability to send turbo requests, so it makes sense to account for this change in behaviour from the turbo side.

brendon commented 2 weeks ago

I went ahead and did a PR. Hopefully it's low touch enough :)

marcelolx commented 2 weeks ago

I'm using sortable.js and after a sort is done I want to submit a turbo request to tell everyone else (except the acting browser) to refresh their page to see the change in order. If the X-Turbo-Request-Id isn't sent along for the ride, the browser doesn't know to ignore the page refresh turbo-stream broadcast.

That's the simplest case that can't easily be done in another way.

I see, so Turbo will behave this way if the turbo-stream returned is a refresh action, that makes sense 👍

Thanks for bringing this up, I will take a look at the PR, but I believe it should be ok using Turbo's fetch in those scenarios.

brendon commented 2 weeks ago

Thanks @marcelolx :) I appreciate you taking the time to review and merge the PR :)