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.

Next.js getInitialProps compatibility

See original GitHub issue

Subject 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:closed
  • Created 5 years ago
  • Reactions:7
  • Comments:19 (13 by maintainers)

github_iconTop GitHub Comments

5reactions
jescalancommented, Aug 20, 2019

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.

import App, { Container } from 'next/app'

class NextApp extends App {
  static async getInitialProps({ Component, ctx }) {
    let pageProps = {}

    if (Component.getInitialProps) {
      pageProps = await Component.getInitialProps(ctx)
    } else if (Component.isMDXComponent) {
      pageProps = await Component({}).props.originalType.getInitialProps(ctx)
    }

    return { pageProps }
  }

  render() {
    const { Component, pageProps } = this.props

    return (
      <Container>
        <Component {...pageProps} />
      </Container>
    )
  }
}

export default NextApp

This will ensure that getInitialProps on your mdx layout is run and passed through the containing elements.

2reactions
EarthlingDaveycommented, Dec 1, 2019

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.

Read more comments on GitHub >

github_iconTop 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 >

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