The problem
I tested the LLM from Infomaniak the other day and I struggled with the CORS (Cross-Origin Resource Sharing) issue again.
It is a recurring issue when you host your app on the https://my-app.com and it requests an api on https://api.example.com.
The browser will prevent you from doing so unless the backend allows the frontend explicitly with the Access-Control-Allow-Origin header, hopefully not *.
MDN describes in detail the concept on their website.
The Solution
If you use Netlify, you can use two methods:
netlify.toml file
As a Netlify forum thread states, you need to define a rewrite rule:
|
|
The :splat placeholder represents everything after https://api.example.com/.
A _redirects File
This option is the same as above, but you write it differently:
|
|
Also, be sure to name the file _redirects and place it in the public directory.
Follow me
Thanks for reading this article. Make sure to follow me on X, subscribe to my Substack publication and bookmark my blog to read more in the future.
Credits: Photo by Markus Spiske on Unsplash.