minhna
December 17, 2017, 4:06pm
1
I’m trying to make 2 these awesome things work together but had no luck.
dynamic import works fine.
server-render works fine.
But when I tried to make them work together, server-render didn’t work as expected. Components loaded via dynamic import didn’t appear in source code of web page.
Hi @minhna , to get help here you have to show some codes…
minhna
December 18, 2017, 12:44am
3
@martineboh , thank you for your reply.
I use react, I created an “AsyncComponent” react component to load other component asynchronously.
class AsyncComponent extends Component {
constructor(props) {
super(props);
this.state = {
component: null,
};
}
async componentWillMount() {
let C = await import('/imports/ui/someComponent.js');
if(C){
this.setState({
component: C.default
});
}
}
render() {
const C = this.state.component;
return C ? <C {...this.props} /> : null;
}
}
Dynamic import works well. But when when I view page source on browser, there is no content of “SomeComponent” appear.
But if I don’t use dynamic import like this:
import SomeComponent from '/imports/ui/someComponent.js';
class AsyncComponent extends Component {
constructor(props) {
//nothing
}
async componentWillMount() {
//nothing
}
render() {
//just render imported component
<SomeComponent {...this.props} />
}
}
then server-render works correctly. I can now see the source of “SomeComponent” in browser.
You need to use componentDidMount
instead as componentWillMount
only runs once before the component is mounted to the DOM. So, there’s not guarantee that component will be rendered after the await import call…and async means “don’t wait”
1 Like
minhna
December 18, 2017, 4:50am
5
I changed to componentDidMount but the problem still remains.
minhna
December 18, 2017, 8:57am
6
@martineboh , do you know a solution which allows to split codes but still supports server-render? I want to reduce page load time. Thank you.
You should look into using React Loadable . I am currently on mobile…
1 Like
orloff
April 23, 2018, 5:57am
9
React loadable needs webpack settings , can you show meteor example?
minhna
April 23, 2018, 6:53am
10
orloff
April 23, 2018, 8:49am
11
Thank, but this example uses react loadable on client only.
as far I understand, problem in splitting initial bundle with ssr, no?
minhna
April 23, 2018, 8:55am
12
I think it does. In the root page of demo site, it doesn’t load the moment package. But if you go to admin page, it will load the package.
orloff
April 23, 2018, 12:14pm
13
This is client side functional
What was the solution for this? React Loadable and SSR doesn’t load the loadable content server side totally defeating the purpose of SSR.
I viewed the source of web page on the browser and the content was there. What did you mean when you say so?