Skip to content

Commit cf2f12d

Browse files
smltrossjrw
authored andcommitted
Make requests to Crom API over GET
Currently, making POST requests [with a JSON content type](https://developer.mozilla.org/en-US/docs/Glossary/CORS-safelisted_request_header) means that the browser needs to make a [preflight request](https://developer.mozilla.org/en-US/docs/Glossary/Preflight_request) first, and that adds an additional request overhead. The preflight request itself doesn't add much load to the API server, and it probably doesn't add a _huge_ overhead to the client either, but with the server running out of a single geographic region, switching to a "CORS-safe" GET request would spare a good chunk of a second of latency for the end-user, depending on their location. In the long term, this would also open up the avenue for basic HTTP-layer caching. Implementation-wise, GraphQL doesn't exactly have an official spec for interfacing with HTTP, but the Crom API's custom implementation follows [the guidelines](https://graphql.org/learn/serving-over-http), which includes a recommendation for making requests over GET. The accept header is there so that the browser doesn't serve the playground HTML instead (which it would if the accept included text/html or \*/\*). Couldn't use URLSearchParams with the ES5 limitation, but the alternative isn't so bad. Also, I drafted this in the GitHub editor, so I haven't actually tested this change.
1 parent 5a4bafb commit cf2f12d

File tree

1 file changed

+8
-3
lines changed

1 file changed

+8
-3
lines changed

js/lookup/crom.js

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -137,8 +137,13 @@ function parseTranslations(response, currentBranch, branches, addLink) {
137137
*/
138138
function executeQuery(url, callback) {
139139
var request = new XMLHttpRequest();
140-
request.open("POST", "https://api.crom.avn.sh/graphql", true);
141-
request.setRequestHeader("Content-Type", "application/json");
140+
var requestUrl =
141+
"https://api.crom.avn.sh/graphql?query=" +
142+
encodeURIComponent(query) +
143+
"&variables=" +
144+
encodeURIComponent(JSON.stringify({ url: url }));
145+
request.open("GET", requestUrl, true);
146+
request.setRequestHeader("Accept", "application/json");
142147
request.addEventListener("readystatechange", function () {
143148
if (request.readyState === XMLHttpRequest.DONE) {
144149
try {
@@ -157,5 +162,5 @@ function executeQuery(url, callback) {
157162
}
158163
}
159164
});
160-
request.send(JSON.stringify({ query: query, variables: { url: url } }));
165+
request.send();
161166
}

0 commit comments

Comments
 (0)