Skip to content

Commit 43a9ae1

Browse files
committed
Refactor CacheBustor with render prop
1 parent 04e8b0a commit 43a9ae1

File tree

3 files changed

+40
-34
lines changed

3 files changed

+40
-34
lines changed

src/App.js

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,21 @@ class App extends Component {
66
render() {
77
return (
88
<CacheBuster>
9-
<div className="App">
10-
<header className="App-header">
11-
<h1>Cache Busting - Example</h1>
12-
<p>
13-
Bundle version - <code>v{global.appVersion}</code>.
14-
</p>
15-
</header>
16-
</div>
9+
{({ loading, isLatestVersion, refreshCacheAndReload }) => {
10+
if (loading) return null;
11+
if (!loading && !isLatestVersion) refreshCacheAndReload();
12+
13+
return (
14+
<div className="App">
15+
<header className="App-header">
16+
<h1>Cache Busting - Example</h1>
17+
<p>
18+
Bundle version - <code>v{global.appVersion}</code>.
19+
</p>
20+
</header>
21+
</div>
22+
);
23+
}}
1724
</CacheBuster>
1825
);
1926
}

src/CacheBuster.js

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,54 @@
11
import React from 'react';
2-
import { semverGreaterThan } from './helper';
32
import packageJson from '../package.json';
43
global.appVersion = packageJson.version;
54

5+
// version from response - first param, local version second param
6+
const semverGreaterThan = (versionA, versionB) => {
7+
const versionsA = versionA.split(/\./g);
8+
9+
const versionsB = versionB.split(/\./g);
10+
while (versionsA.length || versionsB.length) {
11+
const a = Number(versionsA.shift());
12+
13+
const b = Number(versionsB.shift());
14+
// eslint-disable-next-line no-continue
15+
if (a === b) continue;
16+
// eslint-disable-next-line no-restricted-globals
17+
return a > b || isNaN(b);
18+
}
19+
return false;
20+
};
21+
622
class CacheBuster extends React.Component {
723
constructor(props) {
824
super(props);
925
this.state = {
10-
isAppReady: false
26+
loading: true,
27+
isLatestVersion: false,
28+
refreshCacheAndReload: () => window.location.reload(true)
1129
};
1230
}
1331

1432
componentDidMount() {
1533
fetch('/meta.json')
1634
.then((response) => response.json())
1735
.then((meta) => {
18-
console.log({ meta });
1936
const latestVersion = meta.version;
2037
const currentVersion = global.appVersion;
2138

2239
const shouldForceRefresh = semverGreaterThan(latestVersion, currentVersion);
2340
if (shouldForceRefresh) {
24-
console.log(`We have a new version - ${latestVersion}. Going to force refresh`);
25-
window.location.reload(true);
41+
console.log(`We have a new version - ${latestVersion}. Should force refresh`);
42+
this.setState({ loading: false, isLatestVersion: false });
2643
} else {
2744
console.log(`You already have the latest version - ${latestVersion}. No cache refresh needed.`);
45+
this.setState({ loading: false, isLatestVersion: true });
2846
}
29-
30-
this.setState({ isAppReady: true });
3147
});
3248
}
3349
render() {
34-
const { isAppReady } = this.state;
35-
if (!isAppReady) return null;
36-
return this.props.children;
50+
const { loading, isLatestVersion, refreshCacheAndReload } = this.state;
51+
return this.props.children({ loading, isLatestVersion, refreshCacheAndReload });
3752
}
3853
}
3954

src/helper.js

Lines changed: 0 additions & 16 deletions
This file was deleted.

0 commit comments

Comments
 (0)