Skip to content

Commit 5ab8773

Browse files
committed
refactor: handle lazy-loaded modules for spa-like behavior
1 parent 22ab19b commit 5ab8773

File tree

2 files changed

+71
-51
lines changed

2 files changed

+71
-51
lines changed

src/components/Page/Page.jsx

Lines changed: 69 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -10,52 +10,74 @@ import Gitter from '../Gitter/Gitter';
1010
// Load Styling
1111
import './Page.scss';
1212

13-
const Page = ({
14-
title,
15-
content,
16-
contributors = [],
17-
related = [],
18-
...props
19-
}) => (
20-
<section className="page">
21-
<PageLinks page={ props } />
22-
23-
<Markdown>
24-
<h1>{ title }</h1>
25-
26-
<div dangerouslySetInnerHTML={{
27-
__html: content
28-
}} />
29-
30-
{ related && related.length > 0 && (
31-
<div>
32-
<hr />
33-
<h3>Further Reading</h3>
34-
<ul>
35-
{
36-
related.map((link, index) => (
37-
<li key={ index }>
38-
<a href={ link.url }>
39-
{ link.title }
40-
</a>
41-
</li>
42-
))
43-
}
44-
</ul>
45-
</div>
46-
)}
47-
48-
{ contributors && contributors.length > 0 && (
49-
<div>
50-
<hr />
51-
<h3>Contributors</h3>
52-
<Contributors contributors={ contributors } />
53-
</div>
54-
)}
55-
</Markdown>
56-
57-
<Gitter />
58-
</section>
59-
);
13+
class Page extends React.Component {
14+
state = {
15+
content: this.props.content instanceof Promise ? 'Loading...' : this.props.content
16+
}
17+
18+
render() {
19+
let {
20+
title,
21+
content,
22+
contributors = [],
23+
related = [],
24+
...rest
25+
} = this.props;
26+
27+
return (
28+
<section className="page">
29+
<PageLinks page={ rest } />
30+
31+
<Markdown>
32+
<h1>{ title }</h1>
33+
34+
<div dangerouslySetInnerHTML={{
35+
__html: this.state.content
36+
}} />
37+
38+
{ related && related.length > 0 && (
39+
<div>
40+
<hr />
41+
<h3>Further Reading</h3>
42+
<ul>
43+
{
44+
related.map((link, index) => (
45+
<li key={ index }>
46+
<a href={ link.url }>
47+
{ link.title }
48+
</a>
49+
</li>
50+
))
51+
}
52+
</ul>
53+
</div>
54+
)}
55+
56+
{ contributors && contributors.length > 0 && (
57+
<div>
58+
<hr />
59+
<h3>Contributors</h3>
60+
<Contributors contributors={ contributors } />
61+
</div>
62+
)}
63+
</Markdown>
64+
65+
<Gitter />
66+
</section>
67+
);
68+
}
69+
70+
componentDidMount() {
71+
if ( this.props.content instanceof Promise ) {
72+
this.props.content
73+
.then(content => this.setState({
74+
content
75+
}))
76+
.catch(error => this.setState({
77+
content: 'Error loading content.'
78+
}));
79+
}
80+
}
81+
}
6082

6183
export default Page;

src/components/Site/Site.jsx

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ class Site extends React.Component {
8787
path={ page.url }
8888
render={ props => {
8989
let path = page.path.replace('src/content/', '');
90-
let module = this.props.import(path);
90+
let content = this.props.import(path);
9191

9292
return (
9393
<React.Fragment>
@@ -100,9 +100,7 @@ class Site extends React.Component {
100100
))) } />
101101
<Page
102102
{ ...page }
103-
content={ module instanceof Promise ? (
104-
'TODO: Use `LazyLoad` component with nprogress'
105-
) : module } />
103+
content={ content } />
106104
</React.Fragment>
107105
);
108106
}} />

0 commit comments

Comments
 (0)