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

Adding X-Turbo-Request-Id support #73

Closed
brendon opened this issue Aug 26, 2024 · 7 comments · Fixed by #74
Closed

Adding X-Turbo-Request-Id support #73

brendon opened this issue Aug 26, 2024 · 7 comments · Fixed by #74

Comments

@brendon
Copy link
Contributor

brendon commented Aug 26, 2024

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
Copy link
Contributor Author

brendon commented Aug 26, 2024

I've reported the spam comment.

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

const response = new FetchResponse(await window.fetch(this.url, this.fetchOptions))

I can work around this by doing:

      const windowFetch = window.fetch
      window.fetch = fetch

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

      window.fetch = windowFetch

@rails rails deleted a comment Aug 26, 2024
@marcelolx
Copy link
Collaborator

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

@brendon
Copy link
Contributor Author

brendon commented Aug 26, 2024

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
Copy link
Contributor Author

brendon commented Aug 26, 2024

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
Copy link
Contributor Author

brendon commented Aug 27, 2024

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

@marcelolx
Copy link
Collaborator

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
Copy link
Contributor Author

brendon commented Aug 30, 2024

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants