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

CORS error when using from frontend #108

Open
makmonty opened this issue Oct 12, 2024 · 3 comments
Open

CORS error when using from frontend #108

makmonty opened this issue Oct 12, 2024 · 3 comments

Comments

@makmonty
Copy link

makmonty commented Oct 12, 2024

I get a CORS error (preflight OPTIONS request fails) when I try to use the library from a frontend app.

I created a minimal setup project here https://github.com/makmonty/ra-bug-min-rep

Note: I don't get the error if I do the same requests with my own implementation as such:

const result = await fetch('https://retroachievements.org/API/API_GetGame.php?y=<webApiKey>&z=<username>&i=<gameId>')
const data = await result.json()

So it's not a configuration issue at the API side.

My environment is:

Browser: Firefox 131.0
SO: Arch Linux

@wescopeland
Copy link
Member

wescopeland commented Oct 12, 2024

Hi @makmonty!

Thanks for reporting this issue. There must be something strange going on. I just created a new React app as a test, and I am indeed able to make API calls through the library with client-side JS:

Screenshot 2024-10-12 at 11 31 20 AM

Here's my very minimal source code:

import { buildAuthorization, getUserProfile } from "./index";

function App() {
  const username = "WCopeland";
  const webApiKey = "redacted";

  const authorization = buildAuthorization({ username, webApiKey });

  const handleButtonClick = async () => {
    const userProfile = await getUserProfile(authorization, {
      username: "xelnia",
    });

    console.log(userProfile);
  };

  return <button onClick={handleButtonClick}>make api call</button>;
}

export default App;

However, it's worth noting that I'm not seeing a preflight OPTIONS call in my network tab at all. A simple GET like this with no custom headers shouldn't be triggering a preflight request.

Is it possible your Vue app is injecting anything into the HTTP call? Custom headers, etc? Any sort of "interference" like this will likely trigger a preflight request.

@makmonty
Copy link
Author

I tried your code and it didn't work for me. BUT, I tried to run both your code and mine on Chrome and both worked fine. As you said, on Chrome the preflight call doesn't happen, but it does on Firefox.
Another thing I noted is that the preflight request doesn't happen in my own implementation of the API. It's only made when I use the @retroachievements/api package on Firefox 🤷

@stphn
Copy link

stphn commented Jan 2, 2025

I ran into the same issue with Firefox and Safari. The API requests fail due to CORS errors, but everything works fine in Chrome. This issue occurs in both localhost and live preview environments.

FIREFOX: OPTIONS /API/API_GetUserProfile.php?z=User-Agent: Mozilla/5.0
SAFARI: GET /API/API_GetUserProfile.php User-Agent: RetroAchievements-api-js

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

No branches or pull requests

3 participants