next.js: getInitialProps that with dynamic import, ssr is ok, but has error in client side
Bug report
getInitialProps that with dynamic import, ssr is ok, but have errors in client side
pages/index.js
import React from "react";
import dynamic from "next/dynamic";
class Index extends React.Component {
static async getInitialProps() {
return { Hello: dynamic(import("../components/hello")) };
}
constructor(props) {
super(props);
console.log("props:", props);
}
render() {
const { Hello } = this.props;
console.log(this.props);
return (
<div>
<Hello />
</div>
);
}
}
export default Index;`
components/hello.js
export default class hello extends React.Component{
render ()
{
return (<div>
<p>Hello Next.js</p>
</div>)
}
}
To Reproduce
Steps to reproduce the behavior, please provide code snippets or a repository:
- just start run the dev npm script
- http://localhost:3000
- looks fine, but you will get lots of error in the browser console
Expected behavior
i log out the props in the render method on purpose, we can see the content in compiler console, but got nothing in client side console.
Screenshots

System information
- OS:Windows10
- Browser: chrome
- Version of Next.js:7.0.2
Additional context
if we don’t use dynamic import but do some simple data api request in getInitialProps, we found that the data recorded inside props would keeped in both server and client side, hope the componens that dynamic import can also shared both server side and client side
About this issue
- Original URL
- State: closed
- Created 8 years ago
- Comments: 15 (3 by maintainers)
Hi Team, I have gone through the code of NextJS and find out that dynamic import can be implemented for production mode with following approach.
Just for example i have concidered that component name will be defined in query params as follows.
http://localhost:3000/?component=PostLink1,PostLink,Selectwhere PostLink1, PostLink, Select are individual components created in pages folder itself.Can any one provide reviews on same suggest the best appraoch and improvements.
server.js
pages/index.js
Other then this i am facing an issue in dev mode. As we are pages are build at the run time in dev enviornment. It gives error file not found if it’s not called before. Can you help me with the best approach to build the required pages before calling the index page in dev mode.
Version Used: next: 8.1.0 react: 16.8.6