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

change to using fetch for ajax instead of jquery function #764

Merged
merged 12 commits into from
Feb 12, 2022

Conversation

70ray
Copy link
Collaborator

@70ray 70ray commented Jan 31, 2022

javascript native fetch uses the promise mechanism. This should make the code less incomprehensible than using two different methods for asynchronous operations.
There should not be any difference in function.

Sandbox at: https://www.pgdp.org/~rp31/c.branch/api_fetch

@cpeel
Copy link
Member

cpeel commented Jan 31, 2022

While they probably do, we should double-check that all of our supported browsers support these JS features: https://caniuse.com/?search=promise

scripts/api.js Show resolved Hide resolved
}
return new Promise(function(resolve, reject) {
fetch(url, options)
.then(function(response) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

not that it can happen often, but the fetch promise could need a catch if network interrupted or something so may want to catch the fetch promise too:
https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API/Using_Fetch

Copy link
Collaborator Author

@70ray 70ray Feb 1, 2022

Choose a reason for hiding this comment

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

I put in a catch and tried issuing a request with the network disconnected but nothing ever happens. We could perhaps put in a timer to detect network disconnection.
If the url has a spurious character inserted at the beginning the error message that appears is "TypeError: NetworkError when attempting to fetch resource." so it seems to be working.
If you edit the tail of the url for example api -> apix then then an "incorrect response type" message appears which is correct because the html page for "file not found" is sent.

scripts/page_browse.js Outdated Show resolved Hide resolved
scripts/page_browse.js Show resolved Hide resolved
scripts/page_browse.js Show resolved Hide resolved
scripts/api.js Outdated Show resolved Hide resolved
@chrismiceli
Copy link
Collaborator

I love the switch to fetch!

@70ray
Copy link
Collaborator Author

70ray commented Feb 1, 2022

While they probably do, we should double-check that all of our supported browsers support these JS features: https://caniuse.com/?search=promise

Promise and fetch are ok for Chrome 54 Firefox 52 Microsoft Edge Opera 41 Safari 11

scripts/api.js Outdated Show resolved Hide resolved
scripts/api.js Outdated Show resolved Hide resolved
scripts/api.js Outdated Show resolved Hide resolved
scripts/api.js Outdated
"headers": {
"X-API-KEY": "SESSION"
}
function ajax(method, apiUrl, queryParams = {}, data = {}) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

I think it'd be nice if we could write a few unit tests for this. Obviously we'll need to mock fetch, so maybe a 5th param that we can set in unit tests to a Promise mock that we implement. Like:

function ajax(method, apiUrl, queryParams = {}, data = {}, fetchPromise = fetch) {
...
}

And then in the unit tests pass a mock like:

ajax('myMethod', 'myapiUrl', {}, {}, Promise.resolve({ok: true, json: () => Promise.resolve({data: {}}));

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Yes, I'll look at doing this.

@chrismiceli
Copy link
Collaborator

Looks good to me now. I look forward to the unit tests though if you intend to do them.

@70ray
Copy link
Collaborator Author

70ray commented Feb 3, 2022

Looks good to me now. I look forward to the unit tests though if you intend to do them.

I started working on this but ran into a problem with an interaction with another test, getting the message "validCharacterPattern is not defined". The ajax test works if the characterValidtion test and the file it uses are removed from the unit tests. I'll look into fixing this in another branch since it is not directly related to this MR.

@70ray
Copy link
Collaborator Author

70ray commented Feb 5, 2022

Looks good to me now. I look forward to the unit tests though if you intend to do them.

I started working on this but ran into a problem with an interaction with another test, getting the message "validCharacterPattern is not defined". The ajax test works if the characterValidtion test and the file it uses are removed from the unit tests. I'll look into fixing this in another branch since it is not directly related to this MR.

@70ray
Copy link
Collaborator Author

70ray commented Feb 5, 2022

Added some tests following Chris's example now that the character test issue has been resolved and updated the sandbox.

@srjfoo
Copy link
Member

srjfoo commented Feb 6, 2022

Browserstack testing:

High Sierra:

  • Safari 11 looks okay
  • Firefox 52-53 throw an "Invalid API path" exception. FF 54 works okay.
  • Chrome 54,-60 throw an "Invalid API path" exception. Chrome 61, 62 okay
  • Opera 41-47 throw the exception, Opera 48 seems to be okay.

Windows 7

  • FF -- verified, same results as macOS High Sierra

Windows 10
*FF -- verified, same results as macOS High Sierra

For the initial testing, I verified scrolling through pages and resizing a bit, but even so, didn't do exhaustive functional testing; mostly verified that it looked reasonable (i.e. toolbars in place, could scroll pages, could resize window without losing vertical toolbar options.

After the more extensive testing on High Sierra, I mostly verified that the max versions that threw an exception on macOS also did so on Windows, and that the min version that didn't also matched. I also verified that the oldest Edge worked okay, and totally ignored IE 😁 .

I went looking for version information; I didn't look at opera, but found this for Chrome and FF:

Chrome 54: 2016-10-12
Chrome 56: 2017-01-25
Chrome 60: 2017-07-25
Chrome 61: 2017-09-05

Firefox 52: 2017-03-07
Firefox 54: 2017-06-13
Firefox 56: 2017-09-28

Opera 41: 2016-10-25
Opera 43: 2017-02-07
Opera 48: 2017-09-27

So, essentially, functionality for this was added in September of 2017 for all three major cross-platform browsers (Safari may be a major browser for Mac, iPhone and iPad users, but is not cross-platform).

@cpeel
Copy link
Member

cpeel commented Feb 6, 2022

Thanks Sharon!

To reframe what Sharon discovered, our currently supported browsers are:

  • Chrome 54
  • Firefox 52
  • Microsoft Edge
  • Opera 41
  • Safari 11

And this would change it to:

  • Chrome 61 <-- changed
  • Firefox 54 <-- changed
  • Microsoft Edge
  • Opera 48 <-- changed
  • Safari 11

We should see what JS feature we are using that doesn't work. The "Invalid API path" error sounds like the URL isn't being constructed correctly. According to caniuse.org template strings are supported in all of those versions though which was my first guess.

@srjfoo
Copy link
Member

srjfoo commented Feb 6, 2022

Thanks Sharon!

To reframe what Sharon discovered, our currently supported browsers are:

* Chrome 54

* Firefox 52

* Microsoft Edge

* Opera 41

* Safari 11

And this would change it to:

* Chrome 61 <-- changed

* Firefox 54 <-- changed

* Microsoft Edge

* Opera 48 <-- changed

* Safari 11

We should see what JS feature we are using that doesn't work. The "Invalid API path" error sounds like the URL isn't being constructed correctly. According to caniuse.org template strings are supported in all of those versions though which was my first guess.

Interesting, though, that browsers that were about 6 months newer than our currently supported set do work with the code as-is?

@cpeel
Copy link
Member

cpeel commented Feb 6, 2022

Interesting, though, that browsers that were about 6 months newer than our currently supported set do work with the code as-is?

That doesn't surprise me. This code is using some JS features that we haven't used before. I can't find anything obvious at caniuse.com that jumps out as the problem though.

@70ray
Copy link
Collaborator Author

70ray commented Feb 6, 2022

  • Invalid API path

Looks like it is the URLSearchParams constructor.

@70ray
Copy link
Collaborator Author

70ray commented Feb 6, 2022

The URLSearchParams constructor could not accept a "record" but it's possible to append key,value pairs one at a time since FF29, chrome49, opera36. safari10.1, Edge17. Revised code and updated the sandbox.

@srjfoo
Copy link
Member

srjfoo commented Feb 6, 2022

The URLSearchParams constructor could not accept a "record" but it's possible to append key,value pairs one at a time since FF29, chrome49, opera36. safari10.1, Edge17. Revised code and updated the sandbox.

The previously erroring browser versions work with the new code. I didn't do as much testing, just mostly confirmed that the affected high and low end of the previously not working versions work now on one OS or another, which is hopefully good enough.

Copy link
Member

@cpeel cpeel left a comment

Choose a reason for hiding this comment

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

Thanks Ray!

Given the prod deployment tomorrow morning and the code release aimed for the end of the week, I'm going to hold off merging this in until after we cut the release. I'd prefer this change to bake on test before rolling it out and a bit of bake time on prod before including it in a release.

@70ray
Copy link
Collaborator Author

70ray commented Feb 7, 2022

The URLSearchParams constructor could not accept a "record" but it's possible to append key,value pairs one at a time since FF29, chrome49, opera36. safari10.1, Edge17. Revised code and updated the sandbox.

The previously erroring browser versions work with the new code. I didn't do as much testing, just mostly confirmed that the affected high and low end of the previously not working versions work now on one OS or another, which is hopefully good enough.

Thanks Sharon, I should have read the doc. https://developer.mozilla.org/en-US/docs/Web/API/URLSearchParams/URLSearchParams more carefully instead of just looking at the top line of Browser compatibility.

@cpeel cpeel merged commit 443817f into DistributedProofreaders:master Feb 12, 2022
@70ray 70ray deleted the api_fetch branch February 14, 2022 11:47
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

4 participants