Search code examples
deploymentservercorsnetlify

How to enable CORS on a Netlify deployment?


I'm using Netlify to serve some static .json files. They load fine in the browser but when I try to fetch them via javascript I get the following error in the console:

Access to fetch at (redirected from ) from origin 'http://localhost:3000' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource. If an opaque response serves your needs, set the request's mode to 'no-cors' to fetch the resource with CORS disabled.

I checked all the options in my site's settings in the Netlify dashboard and don't see any options to enable CORS, how can this be done?


Solution

  • Add a file called _headers next to your index.html with the following content:

    /*
      Access-Control-Allow-Origin: *
    

    It's better to change it to your actual origin instead of * in production.