Next.js getInitialProps compatibility
See original GitHub issueSubject of the issue
I thought that after #379, this would be possible in Next.js:
const BlogPost = () => {};
BlogPost.getInitialProps = async () => {
const data = await fetch();
return data;
};
import BlogPost from '../../components/BlogPost';
export default BlogPost;
Unfortunately, the getInitialProps
method is never called, as the BlogPost is not the most top-level component that is exported:
export default ({components, ...props}) => <MDXTag name="wrapper" Layout={({ children }) => <BlogPost meta={meta}>{children}</BlogPost>} layoutProps={props} components={components}>
Is there any way to make it possible to run a getInitialProps
function from my BlogPost
? I’m not sure what the right approach here is, as obviously the MDXTag
component needs to be wrapped around the BlogPost children… Maybe a patch in Next.js could make this possible? Not sure.
/cc @timneutkens
Issue Analytics
- State:
- Created 5 years ago
- Reactions:7
- Comments:19 (13 by maintainers)
Top Results From Across the Web
getInitialProps - Data Fetching
getInitialProps enables server-side rendering in a page and allows you to do initial data population, it means sending the page with the data...
Read more >getInitialProps vs. getServerSideProps in Next.js
Explore the differences between getInitialProps and getServerSideProps, two methods for defining props for server-side rendered pages.
Read more >reactjs - In NextJS, how do i add getInitialProps() to a HOC- ...
Yes, you're on the right track. The next. js documentation says that `getInitialProps can not be used in children components, only in the ......
Read more >How the new Next.js 9.3 Preview Mode works and why it's ...
While keeping retro compatibility with getInitialProps , Next.js 9.3 introduces new data-fetching primitives.
Read more >Getserversideprops vs Getinitialprops
While keeping retro compatibility with getInitialProps, Next.js 9.3 introduces new data-fetching primitives. These will improve both developer and user ...
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
I have implemented a fix for this, but it definitely is not ideal when it comes to user-friendliness. It is fairly simple though, a couple lines in a custom
_app.js
file.This will ensure that
getInitialProps
on your mdx layout is run and passed through the containing elements.I’m following this issue. The answer should fix the compatibility issue when using next-mdx-enhanced with next-i18next.
You have not declared a namespacesRequired array on your page-level component: MDXContent.