Skip to content

Commit 95ebe9f

Browse files
fix: content population on server, hydrate on client
1 parent d43ac95 commit 95ebe9f

File tree

2 files changed

+25
-15
lines changed

2 files changed

+25
-15
lines changed

src/components/Page/Page.jsx

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,28 @@ import Gitter from '../Gitter/Gitter';
1111
import './Page.scss';
1212

1313
class Page extends React.Component {
14-
state = {
15-
content: this.props.content instanceof Promise ? 'Loading...' : this.props.content
14+
constructor(props) {
15+
super(props);
16+
17+
const { content } = props;
18+
19+
this.state = {
20+
content: content instanceof Promise ? 'Loading...' : content.default || content
21+
};
22+
}
23+
24+
componentDidMount() {
25+
const { content } = this.props;
26+
27+
if (content instanceof Promise) {
28+
content
29+
.then(module => this.setState({
30+
content: module.default || module
31+
}))
32+
.catch(error => this.setState({
33+
content: 'Error loading content.'
34+
}));
35+
}
1636
}
1737

1838
render() {
@@ -66,18 +86,6 @@ class Page extends React.Component {
6686
</section>
6787
);
6888
}
69-
70-
componentDidMount() {
71-
if ( this.props.content instanceof Promise ) {
72-
this.props.content
73-
.then(module => this.setState({
74-
content: module.default || module
75-
}))
76-
.catch(error => this.setState({
77-
content: 'Error loading content.'
78-
}));
79-
}
80-
}
8189
}
8290

8391
export default Page;

src/index.jsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,11 @@ import Site from './components/Site/Site';
66
// TODO: Re-integrate <GoogleAnalytics analyticsId="UA-46921629-2" />
77
// Consider `react-g-analytics` package
88

9+
const render = process.NODE_ENV === 'production' ? ReactDOM.hydrate : ReactDOM.render;
10+
911
// Client Side Rendering
1012
if ( window.document !== undefined ) {
11-
ReactDOM.render((
13+
render((
1214
<BrowserRouter>
1315
<Route
1416
path="/"

0 commit comments

Comments
 (0)