question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

getStaticProps doesn't work with a custom _app.js

See original GitHub issue

Bug report

Describe the bug

I have a project in NextJS and I upgraded lately to 9.4.2 to use the new functionalities of SSG. I’m using a custom _app with getInitialProps because I need some data that I pass from my server.js

When I tried to add a new page with getStaticProps it seems that nothing is returned in the props.

I read some issues opened lately, and it seems that it’s something with pageProps or having getInitialProps in a custom _app.js tried everything mentioned but it seems not working.

To Reproduce

My _App.js looks like this:

class MyApp extends App {
  static async getInitialProps ({ Component, ctx }) {
    let pageProps = {}
    const errorCode = null

    if (Component.getInitialProps) {
      pageProps = await Component.getInitialProps(ctx)
    }

    if (ctx.req) {
      pageProps.globalSettings = ctx.req.globalSettings
    }

    pageProps.errorCode = pageProps.errorCode || errorCode

    return { pageProps }
  }
...
}
...
export default withRedux(() => reduxStore)(appWithTranslation(MyApp))

And my Posts.js page looks like this:

function Posts ({ posts = [] }) {
  console.log(posts)
  return (
    <ul>
      {posts.map(post => (
        <li key={post.id}>{post.title}</li>
      ))}
    </ul>
  )
}
export async function getStaticProps () {
  const res = await fetch('https://jsonplaceholder.typicode.com/posts')
  const posts = await res.json()
  return {
    props: {
      posts
    }
  }
}
export default Posts

Current behavior

the prop posts is empty and nothing is returned from getStaticProps

Additional remarks

I’m using both next-i18next and next-redux-wrapper

System information

  • OS: MacOS
  • Browser: Chrome
  • Version of Next.js: 9.4.2

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:8 (1 by maintainers)

github_iconTop GitHub Comments

0reactions
balazsorban44commented, Jan 30, 2022

This issue has been automatically locked due to no recent activity. If you are running into a similar issue, please create a new issue with the steps to reproduce. Thank you.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Unable to use getStaticProps in _app.js - Stack Overflow
For that I've exported the getStaticProps from app.js file. But it doesn't seem to work. Here is my app.js. import Layout from "....
Read more >
Data Fetching: getStaticProps - Next.js
Fetch data and generate static pages with `getStaticProps`. Learn more about this API for data fetching in Next.js.
Read more >
getstaticprops not passing props - You.com | The AI Search ...
i had same problem for use getStaticProps. my problem solved with this way. you can create a lib folder in project root. and...
Read more >
Best practices to increase the speed for Next.js apps
Next.js has built-in caching so pages load faster. ... that the dependency was truly not needed and that you didn't break your application....
Read more >
How to Create Pages in Next.js with Static & Dynamic Data
A basic React application that doesn't use a framework like Next.js works by mounting to an entrypoint in an HTML document (DOM node), ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found