🔮 Sanity Create is here. Writing is reinvented. Try now, no developer setup

Issue with CORS error when fetching data from Sanity Studio in React App

11 replies
Last updated: Sep 1, 2021
Hello everyone,I have deployed my first React App on Netlify, and everything apart from the sanity blog I’ve created works.
When I check the console I get the following error message, what have I missed?
Aug 30, 2021, 2:12 PM
Hey User! Are you still having this issue? If so, is this error occurring in your studio or your frontend?
Aug 30, 2021, 9:04 PM
Yeah, unfortunately.I get this error in the front-end on my live published website.

I have added my url to the CORS origins, but still nothing
😞
Aug 31, 2021, 7:28 AM
When trying this in developer mode, everything works like a charm, when deployed the .env variables works, bit I can’t seem to find anything about
networkError
, so I wonder if Sanity Studio block the URL somewhere.
Have you heard about this before or do you have any clue how this can be resolved?
Aug 31, 2021, 3:14 PM
When trying this in developer mode, everything works like a charm, when deployed the .env variables works, bit I can’t seem to find anything about
networkError
, so I wonder if Sanity Studio block the URL somewhere.
Have you heard about this before or do you have any clue how this can be resolved?
Aug 31, 2021, 3:14 PM
Aug 31, 2021, 4:18 PM
Which browser are you using and do you have any enhanced privacy settings?
Aug 31, 2021, 6:08 PM
Google Chrome, but I can re-create this in Firefox
Aug 31, 2021, 7:53 PM
And also Safari
Aug 31, 2021, 7:53 PM
So for some reason it won’t fetch the data, caused of this “Network Error” 😞
Aug 31, 2021, 7:54 PM
I just realized that the firefox browser shows additional info:
Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at https://{my project id is here}.<http://api.sanity.io/v1/data/query/production|api.sanity.io/v1/data/query/production>?....

I did some research and found this “Basically this errors are due to restriction a browser applies while loading the resources from other site. Let says your site X is trying to load resources like js/css from Site Y, A policy on browser gets triggered and blocks the resources if site Y doesn’t allow other site to request content. This action causes above error message to show up in the console window.
To fix this issue, Site Y has to send Access-Control-Allow-Origin: * response header to allow other site to load resources. In case you want to allow only site X, then site Y should send Access-Control-Allow-Origin: http://&lt;domain-of-site-x&gt;&gt; to browswer.” (from
https://community.akamai.com/customers/s/article/Cross-Origin-Request-Blocked-The-Same-Origin-Policy-disallows-reading-the-remote-resource-at-URL?language=en_US#:~:text=You%20may%20have%20observed%20%22Cross,the%20resources%20from%20other%20site . ), but I feel a bit confused where this needs to be allowed.
I might be on the wrong track, but I’d rather share then not.
Aug 31, 2021, 8:13 PM
Sorry for spamming, but I found this:
https://www.sanity.io/docs/cors
And I tried around some stuff with the
CORS origins, and I came to the conclusion it works when I added my url like
<https://www.domain.com>
instead of
<https://domain.com>
it worked.


I did the mistake of thinking that would be added in the background but I got it working! 😄
Sep 1, 2021, 6:30 AM

Sanity– build remarkable experiences at scale

Sanity is a modern headless CMS that treats content as data to power your digital business. Free to get started, and pay-as-you-go on all plans.

Was this answer helpful?