2

I'm working with next.js, in development mode everything works fine, but in production mode I have a problem when rendering the pages dynamically. I have the following path inside the pages folder pages/user/[id], and this component is where I call the getServerSideProps function.


import headers from '../../headers';

export async function getServerSideProps(context) {

  const URL = 'https://somewhere...';

  let { id } = context.params;

  const apiResponse = await fetch(
    `${URL}/${id}/detail`,
    {
      headers: headers,
    }
  ); 
  if (apiResponse.ok) {
    
    const data = await apiResponse.json();
    return {
      props: data, // will be passed to the page component as props
    };
  } else {   
    return { props: {} };
  }
  
}

My problem is the following, I need to send in headers the authentication token that I only get when I login and I get the 2FA code, so in build time, that info does not exist and I get a 401 error no authorizate when execute npm run build and when I access to /user/34 for example I get a 404 error.

I have checked these questions at stackoverflow:

I have some parts in my app that are statics and works fine, but the problem is with the dynamic paths, as next.js is not creating those paths.

EDIT: I'll include a image with other problem, if after the fetch in the if I just say :

if(apiResponse){ //without 'ok'

}

I'll recieve this errror: response error

Raul
  • 659
  • 1
  • 6
  • 11
  • Where's `headers` coming from in your code? Also, `getServerSideProps` doesn't get called at build-time, it's called on each request to the page. – juliomalves Feb 26 '21 at 11:47
  • ```headers``` are imported from other file and it's working fine, and if ```getServerSideProps``` it`s called on each request I always recieve a 404 response and I don't know why. – Raul Feb 26 '21 at 11:55

1 Answers1

0
return {
      props: data, // will be passed to the page component as props
    }

props should be object

return {
          props: {data} // or props: {data:data}
        }
Yilmaz
  • 35,338
  • 10
  • 157
  • 202
  • I did that change too, finally we change a server with node.js and no continue usin nginx server and everything is working fine. I really don't want to say that problem was the server nginx because I suppose it's possbile configure it but I don't know how to do it. In resume with a server based i node.js all is working. – Raul Mar 08 '21 at 07:47