2

I am trying to get getStaticProps to return some data, but the function is never called. I looked up the official documentation and the examples and also checked various answers here at SO but I still can't get it to work.

The fact that the console.log inside the getStaticProps never runs leads me to think that there is a configuration error with my code, but I can't find the root cause of the problem.

My apollo client configuration is the following:

apollo client configuration

The constants file that has the ApolloClient options is the following

constants file with apollo configuration

My component is the following, in the useEffect the console.log commands logs undefined and I never see the console.log command from inside the gerStaticProps getting called either.

Main component

I also have a custom _app component which is the following:

custom _app component

My folder structure is the following:

folder structure

I think there is a good chance the problem lies in my _app.tsx component and that this page from the official documentation contains the solution, but even after trying it I did not solve my problem.

Prinny
  • 208
  • 6
  • 16

1 Answers1

4

From the info provided, two possible reasons are ,

  1. getStaticProps must be used in a page. Check if the file you are using is a page and not a component.
  2. The file structure may be causing the issue. Follow this thread Is this Next.JS folder structure recommended?
RobLjm
  • 399
  • 2
  • 11
  • I think it has something to do with the exports I do in my `index` files. I just created a simple component directly under the `Pages` folder (meaning a tsx file, not a folder containing other files) and it receives values from `getStaticProps`. – Prinny Sep 20 '21 at 08:31
  • 1
    Ok, most probably nextJS is not identifying your 'Main' component as a page. getStaticProps must be called on files in the pages folder and not on components imported from these pages . – RobLjm Sep 20 '21 at 11:20
  • Yeah basically my `/src/pages/index.ts` file was just export my `Main.tsx` component. I made it so it actually renders it and then placed `getStaticProps` in there and it works. – Prinny Sep 20 '21 at 17:28