-
Notifications
You must be signed in to change notification settings - Fork 37
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
Convert $.ajax calls to use fetch #45
Comments
Just as a headsup, fetch seems simpler but also needs more scaffolding to make it actually work properly in regards to error handling. See this. I also remember reading it doesn't work as nicely with cookies and sessisons as xhr does. Anyway, if we can replace the jquery ajax calls with it then it is absolutely fine. Alternatively we could use axios which is promise based and a solid framework. |
That article seems to make things a bit more complicated than it has to in regards to error handling. Since all our requests are happening from one bit of code, it's easy enough to add Cookie support seems easy enough too, we just need to add the All good things to keep in mind when we implement it though. This change will definitely need a fair amount of testing before it's good to go. |
Stickied this one as we are currently running into some issues it seems with ajax jquery, specifically redirects when reddit thinks you should log in. ajax can't pick up on them and then we get served an html pages. So it might be beneficial to explore this a bit further soonish. After recently having played around with fetch it seems indeed that the scaffolding surrounding it is doable. For example this is what I did in another project modified to work on reddit. function handleEndpoint ({endpoint, method}) {
return new Promise((resolve, reject) => {
// If we don't have a method we can't continue
if (!method) {
method = 'GET'
}
// Set up the initial request params.
const requestInit = {
method,
credentials: 'include',
};
let requestURL = `https://old.reddit.com${endpoint}`;
const apiRequest = new Request(requestURL, requestInit);
fetch(apiRequest)
.then(response => {
if (response.ok) {
return response;
}
return reject({
status: response.status,
statusText: response.statusText,
});
})
.then(response => response.json())
.then(response => {
resolve(response);
});
});
} Called through handleEndpoint ({
endpoint: '/subreddits/mine/moderator.json',
})
.then(response => {
console.log(response)
}).catch(error => {
console.error(error);
}); Which as you pointed already out can likely be simplified (and isn't complete either) :P Just pasting it in here as reference. |
Oh what I wanted to actually type is that we need to make sure we can handle redirects with fetch properly 😁 I also tested the above code (and modified it from the initial code I pasted) and it seems to work fairly well in the background. edit: https://developer.mozilla.org/en-US/docs/Web/API/Response/redirected Seems like if we add |
I'm gonna look into implementing this soonish. Do we generally want to disallow redirects or follow them? |
Error on redirect I believe. |
As I got reminded by the other jquery related issue we tackled the other day, any progress here? There is no rush as using jquery for now is fine. |
Still on my radar for when I'm done with (or get bored of) #245 |
Related to #44
fetch
is supported on all relevant browser versions and has a much nicer API than$.ajax
does.Because
$.ajax
passes three parameters to its callbacks, it's more difficult to have it play well with promises, which can only represent a single value (we get around this now by promising an object with the callback values as properties, but this isn't ideal).fetch
, on the other hand, promises a singleResponse
object.Additionally,
Response
is (relatively) easily serializable to/from JSON - e.g. we can get the response from the background page, convert it to JSON, and then letTBUtils.sendRequest
convert the JSON it received from the background page back into aResponse
object with its constructor. For example:The text was updated successfully, but these errors were encountered: