Skip to content
This repository was archived by the owner on Mar 27, 2018. It is now read-only.

Commit c4a2f48

Browse files
author
Ian Wensink
committed
fix(entity-mapper): fix ssr
1 parent 62cd7f2 commit c4a2f48

File tree

2 files changed

+44
-8
lines changed

2 files changed

+44
-8
lines changed

src/DrupalPage/index.js

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ class DrupalPage extends Component {
1111
};
1212

1313
state = {
14-
ready: false,
1514
loadingData: true,
1615
dataUrl: null,
1716
pageUuid: null,
@@ -24,15 +23,18 @@ class DrupalPage extends Component {
2423
* multiple renders.
2524
*/
2625
async asyncBootstrap() {
27-
await this.loadData(this.props);
28-
this.context.hnContext.state = this.state;
26+
this.context.hnContext.state = {
27+
drupalPage: await this.loadData(this.props),
28+
entities: {},
29+
};
30+
return true;
2931
}
3032

3133
/**
3234
* The first time this element is rendered, we always make sure the component and the Drupal page is loaded.
3335
*/
3436
componentWillMount() {
35-
const state = getNested(() => this.context.hnContext.state);
37+
const state = getNested(() => this.context.hnContext.state.drupalPage);
3638
if (state) {
3739
this.setState(state);
3840
} else {
@@ -87,8 +89,12 @@ class DrupalPage extends Component {
8789
// Check if this is still the last request.
8890
if(this.lastRequest !== lastRequest) return;
8991

92+
const newState = { ...this.state, ...{ pageUuid, loadingData: false, dataUrl: url }};
93+
9094
// Mark this component as ready. This mounts the Layout and new ContentType.
91-
this.setState({ pageUuid, loadingData: false, dataUrl: url });
95+
this.setState(newState);
96+
97+
return newState;
9298
}
9399

94100
render() {

src/EntityMapper.js

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ import site from './site';
77
class EntityMapper extends Component {
88
static entityComponents = new Map();
99

10+
static contextTypes = {
11+
hnContext: PropTypes.object,
12+
};
13+
1014
/**
1115
* This makes sure the data for this url is ready to be rendered.
1216
* @param uuid
@@ -62,8 +66,30 @@ class EntityMapper extends Component {
6266
};
6367
}
6468

65-
componentDidMount() {
66-
this.loadComponent(this.props);
69+
/**
70+
* If this component exists in a tree that is invoked with the waitForHnData function, this function is invoked.
71+
* Only after the promise is resolved, the component will be mounted. To keep the data fetched here, we assign the
72+
* state to the hnContext provided by the DrupalPageContextProvider. This way, the state will be preserved trough
73+
* multiple renders.
74+
*/
75+
async asyncBootstrap() {
76+
const { mapper, asyncMapper } = this.props;
77+
const { uuid, entityProps } = this.state;
78+
this.context.hnContext.state.entities[uuid] = await this.loadComponent({ uuid, mapper, asyncMapper, entityProps });
79+
return true;
80+
}
81+
82+
/**
83+
* The first time this element is rendered, we always make sure the component and the Drupal page is loaded.
84+
*/
85+
componentWillMount() {
86+
const { uuid } = this.props;
87+
const state = getNested(() => this.context.hnContext.state.entities[uuid]);
88+
if (state) {
89+
this.setState(state);
90+
} else {
91+
this.loadComponent(this.props);
92+
}
6793
}
6894

6995
componentWillReceiveProps(nextProps) {
@@ -75,7 +101,11 @@ class EntityMapper extends Component {
75101
async loadComponent({ uuid, mapper, asyncMapper, entityProps }) {
76102
this.setState({ ready: false });
77103
const entityComponentSymbol = await EntityMapper.assureComponent({ uuid, mapper, asyncMapper });
78-
this.setState({ uuid, entityComponentSymbol, ready: true, entityProps });
104+
105+
const newState = { ...this.state, ...{ uuid, entityComponentSymbol, ready: true, entityProps } };
106+
this.setState(newState);
107+
108+
return newState;
79109
}
80110

81111
isReady() {

0 commit comments

Comments
 (0)