I was embedding some large images with react query to provide a loading effect on slow connections when I encountered this CORS issue I did not understand.
I'm not getting why it's no problem for the browser to import resources from external servers when the request is performed in the HTML.
For example, this is a valid image fetch: <img src={"https://stackoverflow.design/assets/img/logos/so/logo-stackoverflow.svg"}/>
it gets rendered with no problem.
When performing the same fetch from javascript, you get CORS to block your response.
For example: fetch("https://stackoverflow.design/assets/img/logos/so/logo-stackoverflow.svg");
will be blocked.
They are both GET requests, and inspecting the response headers on the network tab they look the exact same.
Is it a desired browser behavior? If so, how is it a safety feature?
If not, how to get around this, in order to fetch simple requests?