Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Nextjs SSR fetch origin

Using the Nextjs getServerSideProps function I make a fetch request to my API. My API checks origin headers for CORS but gets the origin header as undefined. Why is this happening and is there a way around this?

I get origin headers while making fetch requests normally from the browser in Next. This issue only occurs when the request is made to the API via the server from Nextjs getServerSideProps.

like image 254
Ziyak Avatar asked Dec 14 '25 20:12

Ziyak


1 Answers

What I did was adding the Origin parameter to the header object in the fetch request and adding the url from the .env file to easily change it for production.

  //Create fetch's options
  const options = {
     mode: 'cors',
     method: 'GET',
     credentials: 'include',
     headers: {
        'Content-Type': 'application/json',
        Origin: `${process.env.NEXT_PUBLIC_FRONT_URL}`,
     },
  };

  //Fetch data from the API
  const res = await fetch('changeForYouURL', options);
like image 65
Adrian Roberto Aguilar Sandic Avatar answered Dec 16 '25 09:12

Adrian Roberto Aguilar Sandic



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!