getStaticProps doesn't work with a custom _app.js
See original GitHub issueBug 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:
- Created 3 years ago
- Comments:8 (1 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@Sletheren https://github.com/vercel/next.js/discussions/11334#discussioncomment-3895
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.