0

The main reason I'm using Next is the 'getStaticProps' function and it's not even running.

I tried to create a new app from scratch "npx create-next-app@latest" not working, I tried "npx create-next-app" without the "@latest" still doesn't work.

Here's how I tried to test it and didn't even return anything or console.log anything:

export async function getStaticProps() {
  return {
    props: {
      name: 'John' 
    }
  }
}

function Home({ name }) {
  return <h1>Hellos {name}!</h1> 
}

export default Home

There, when I run npm run dev it only returns: "Hellos !" and it doesn't console.log anything, I hope it's just me and the solution is simple.

On the other app I created I ran 'npm run build' and then 'npm start', still the same problem.

Hamza
  • 3
  • 1
  • Are you using the new "app" router or the old "pages" router? – Asplund May 30 '23 at 09:39
  • I think it's the new one I'm not sure though, it goes like this: the folder has the name like 'about' and the page inside it is like 'page.js'. Not sure if this is what you mean When creating the app it asked "Would you like to use the `src/ directory`" I answered no. Please elaborate if this doesn't answer your question, thanks!! – Hamza May 30 '23 at 09:49
  • Are your pages defined in the "app" directory or the "pages" directory? As you probably know Next.js from reading the [getting started](https://nextjs.org/docs/getting-started/project-structure#top-level-folders) in the docs, all pages in your application must be defined in either the "pages" or "app" directory. This has nothing to do with the "src" directory. – Asplund May 30 '23 at 10:01
  • It's in the app directory – Hamza May 30 '23 at 10:02
  • the app directory does not support getstaticprops. read the new docs or choose the old way in cmd when you use creat-next-app – abolfazl shamsollahi May 30 '23 at 10:06
  • Oh God, I was about to find an alternative to nextjs lol, Thank you guys!! – Hamza May 30 '23 at 10:10

1 Answers1

0

getStaticProps only works in the pages router. The new app router uses React server components (rsc).

Move your page files from app/ to pages/ or start using rsc instead.

Asplund
  • 2,254
  • 1
  • 8
  • 19