Skip to content

Commit 595e62b

Browse files
EugeneHlushkomontogeek
authored andcommitted
speed(homepage) offload images until in viewport (#2956)
* speed(homepage) offload images until in viewport * speed(homepage) refactor approach to use existing package
1 parent 2af1ec1 commit 595e62b

File tree

4 files changed

+74
-24
lines changed

4 files changed

+74
-24
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,7 @@
149149
"react-dom": "^16.2.0",
150150
"react-g-analytics": "0.4.2",
151151
"react-hot-loader": "^4.0.0-beta.12",
152+
"react-intersection-observer": "^8.22.6",
152153
"react-markdown": "4.0.4",
153154
"react-router-dom": "^4.2.2",
154155
"react-tiny-popover": "3.4.2",
Lines changed: 43 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,48 @@
11
import React from 'react';
2+
import { InView } from 'react-intersection-observer';
3+
import SmallIcon from '../../assets/icon-square-small-slack.png';
24
import './Contributors.scss';
35

4-
export default ({contributors}) => {
5-
if (!contributors.length) {
6-
return <noscript />;
6+
export default class Contributors extends React.Component {
7+
state = {
8+
inView: false
79
}
810

9-
return (
10-
<div className="contributors">
11-
<div className="contributors__list">
12-
{
13-
contributors.map(contributor => (
14-
<a key={ contributor }
15-
className="contributor"
16-
href={ `https://github.com/${contributor}` }>
17-
<img alt={ contributor } src={ `https://github.com/${contributor}.png?size=90` } />
18-
<span className="contributor__name"> {contributor}</span>
19-
</a>
20-
))
21-
}
22-
</div>
23-
</div>
24-
);
25-
};
11+
handleInView = (inView) => {
12+
if (!inView) {
13+
return;
14+
}
15+
this.setState({ inView });
16+
}
17+
18+
render() {
19+
const { inView } = this.state;
20+
const { contributors } = this.props;
21+
22+
if (!contributors.length) {
23+
return <noscript />;
24+
}
25+
26+
return (
27+
<InView as="div"
28+
onChange={ this.handleInView }
29+
threshold={ 0 }
30+
triggerOnce
31+
className="contributors">
32+
<div className="contributors__list">
33+
{
34+
contributors.map(contributor => (
35+
<a key={ contributor }
36+
className="contributor"
37+
href={ `https://github.com/${contributor}` }>
38+
<img alt={ contributor }
39+
src={ inView ? `https://github.com/${contributor}.png?size=90` : SmallIcon } />
40+
<span className="contributor__name"> {contributor}</span>
41+
</a>
42+
))
43+
}
44+
</div>
45+
</InView>
46+
);
47+
}
48+
}

src/components/Support/Support.jsx

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
// Import External Dependencies
22
import React from 'react';
3+
import { InView } from 'react-intersection-observer';
34

45
// Import Data
56
import Backers from './_supporters.json';
@@ -62,9 +63,22 @@ function formatMoney(number) {
6263
}
6364

6465
export default class Support extends React.Component {
66+
state = {
67+
inView: false
68+
}
69+
70+
handleInView = (inView) => {
71+
if (!inView) {
72+
return;
73+
}
74+
this.setState({ inView });
75+
};
76+
6577
render() {
6678
let { rank } = this.props;
6779

80+
const { inView } = this.state;
81+
6882
let supporters = SUPPORTERS;
6983
let minimum, maximum, maxAge, limit, random;
7084

@@ -108,7 +122,11 @@ export default class Support extends React.Component {
108122
}
109123

110124
return (
111-
<div className="support">
125+
<InView as="div"
126+
onChange={ this.handleInView }
127+
threshold={ 0 }
128+
triggerOnce
129+
className="support">
112130
<div className="support__description">
113131
{ rank === 'backer' ? (
114132
<p>
@@ -134,7 +152,7 @@ export default class Support extends React.Component {
134152
href={ supporter.website || `https://opencollective.com/${supporter.slug}` }>
135153
{<img
136154
className={ `support__${rank}-avatar` }
137-
src={ supporter.avatar || SmallIcon }
155+
src={ (inView && supporter.avatar) ? supporter.avatar : SmallIcon }
138156
alt={ supporter.name || supporter.slug ? `${supporter.name || supporter.slug}'s avatar` : 'avatar' }
139157
onError={ this._handleImgError } />}
140158
</a>
@@ -146,7 +164,7 @@ export default class Support extends React.Component {
146164
Become a { rank === 'backer' ? 'backer' : 'sponsor' }
147165
</a>
148166
</div>
149-
</div>
167+
</InView>
150168
);
151169
}
152170

yarn.lock

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
dependencies:
1010
regenerator-runtime "^0.12.0"
1111

12-
"@babel/runtime@^7.1.2":
12+
"@babel/runtime@^7.0.0", "@babel/runtime@^7.1.2":
1313
version "7.4.3"
1414
resolved "https://registry.yarnpkg.com/@babel/runtime/-/runtime-7.4.3.tgz#79888e452034223ad9609187a0ad1fe0d2ad4bdc"
1515
integrity sha512-9lsJwJLxDh/T3Q3SZszfWOTkk3pHbkmH+3KY+zwIDmsNlxsumuhS2TH3NIpktU4kNvfzy+k3eLT7aTJSPTo0OA==
@@ -9113,6 +9113,14 @@ react-hot-loader@^4.0.0-beta.12:
91139113
shallowequal "^1.0.2"
91149114
source-map "^0.7.3"
91159115

9116+
react-intersection-observer@^8.22.6:
9117+
version "8.22.6"
9118+
resolved "https://registry.yarnpkg.com/react-intersection-observer/-/react-intersection-observer-8.22.6.tgz#b03ad27a612bdbb5510f5fd21e997bdfc012d784"
9119+
integrity sha512-xhQK9jqU4yUUCQPne4rXt21ExnWTpbC3fhXlJlWOchFH2S9kaf0CJU5pTyCds2HKs43QE1+y4sGkBAoktnpdGg==
9120+
dependencies:
9121+
"@babel/runtime" "^7.0.0"
9122+
invariant "^2.0.0"
9123+
91169124
react-is@^16.7.0, react-is@^16.8.1:
91179125
version "16.8.6"
91189126
resolved "https://registry.yarnpkg.com/react-is/-/react-is-16.8.6.tgz#5bbc1e2d29141c9fbdfed456343fe2bc430a6a16"

0 commit comments

Comments
 (0)