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

Changing XHR to Fetch API #1845

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

hmdros
Copy link

@hmdros hmdros commented Oct 19, 2022

@arielj, @toppa and I are submitting this PR for this issue: #1193

We had a few differences between XHR and Fetch, so we couldn't match functionality 100%.
But we think it's fine from what we saw on the code.

  • Now we abort the request with AbortController
  • Fetch API doesn't have a onProgress callback or equivalent
  • Fetch API doesn't support sync request

We couldn't find usages of the xhr argument on callback methods. We weren't sure about the arguments.

Captura de Tela 2022-10-19 às 17 56 42

@hmdros hmdros requested a review from a team as a code owner October 19, 2022 20:57
Copy link
Contributor

@simon04 simon04 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for your work. I appreciate the modernisation. For this core functionality, we should take extra care to maintain compatibility and not introduce any bugs. Please see my comments.

Comment on lines +85 to +86
onError = (type, error, options) ->
options.error?.call options.context, type, error, options
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here are some usages for the error handler and progress callback:

Comment on lines -105 to -109
abort = (xhr) ->
clearTimeout(xhr.timer)
xhr.onreadystatechange = null
xhr.abort()
return
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here's one usage of abort:

Comment on lines +22 to +25
if options.dataType == 'json'
response.json()
else
response.text()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think response.ok and response.status (200 <= xhr.status < 300) need to be checked.

@arielj
Copy link

arielj commented Oct 24, 2022

hey @simon04 we were reviewing your comments and the code, and we have some concerns if this move from xhr to fetch is actually beneficial or not. I can describe a bit of what we found:

  • fetch has no equivalent of a progress callback, the workarounds involve adding a lot of code wrapping the fetch requests into another object like https://dev.to/tqbit/how-to-monitor-the-progress-of-a-javascript-fetch-request-and-cancel-it-on-demand-107f or https://github.com/prioe/node-fetch-progress, but we are not sure if that's the direction you want to go with this code, it will be way more complex
  • the way the abort works with fetch requests requires a lot of changes in logic because fetch does not return a request object that can be aborted, it returns a promise, so the abort callback would have to be passed as an option from the caller (it requires changing entry_page.coffee, entry.coffee, db.coffee, and ajax.coffee just to pass around an abort function)

Also, we noticed one thing that seems to be a bug in the current code, this error callback

error: (_, xhr) => @onUpdateReady() if xhr.status is 404
is expecting the xhr as the second argument, but that's actually passed as the third argument here
options.error?.call options.context, type, xhr, options

We want to raise these concerns before continuing with this change, we are not sure if it's really worth it (the current code works just fine and some of the code around it was designed for how xhr works)

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 this pull request may close these issues.

None yet

3 participants