getInitialProps() missing from dynamic import
See original GitHub issueBug report
Describe the bug
When using next/dynamic for dynamic imports of a page-component that contains getInitialProps, the getInitialProps function from the imported dynamic component is missing.
To Reproduce
// src/modules/about.tsx
import React from "react";
import { NextPage, NextPageContext } from "next";
interface Props {
userAgent?: string;
test: string;
}
const About: NextPage<Props> = ({ userAgent, test }: NextPageContext) => {
return (
<p>User Agent: {userAgent}</p>
<p>Test: {test}</p>
);
};
About.getInitialProps = async ({ req }: NextPageContext) => {
const userAgent = req ? req.headers["user-agent"] : navigator.userAgent;
console.log('getInitialProps has run')
return { userAgent, test: "can you see me?" };
};
export default About;
// src/pages/about.ts
import dynamic from "next/dynamic";
const Page = dynamic(() => import(`../modules/about`));
export default Page;
The rendered page is missing the userAgent and test props, and there is no log of getInitialProps having run in the console.
Expected behavior
The dynamically imported Page component contains Page.getInitialProps from About.getInitialProps;
System information
- OS: macOS
- Browser: Chrome 79.0.3945.88
- Version of Next.js: 9.1.6
Issue Analytics
- State:
- Created 4 years ago
- Comments:7 (7 by maintainers)
Top Results From Across the Web
getInitialProps() missing from dynamic import #9857 - GitHub
Describe the bug. When using next/dynamic for dynamic imports of a page-component that contains getInitialProps , the getInitialProps function ...
Read more >getInitialProps - Data Fetching - Next.js
getInitialProps enables server-side rendering in a page and allows you to do initial data population ... import React from 'react' class Page extends...
Read more >Import a library only on the server side to use in getInitialProps
I would try using fetch in your getInitialProps and make a proxy-call to an api route instead. Inside of your api route, it...
Read more >The Next.js Handbook - The Valley of Code
This approach lets us use getInitialProps() from within our page component, with the only downside of having to write the component JSX inside...
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 Free
Top 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

dynamic()does not return the component itself, it returns a component that has an “eventual” render of the component, and there is no way to copy over static properties reliably (and would cause inconsistency if we tried).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.