Skip to content

Commit 80113b8

Browse files
committed
Add cache buster code
1 parent a982afb commit 80113b8

File tree

10 files changed

+106
-33
lines changed

10 files changed

+106
-33
lines changed

generate-build-version.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/* eslint-disable */
2+
const fs = require('fs');
3+
const packageJson = require('./package.json');
4+
5+
const appVersion = packageJson.version;
6+
7+
const jsonData = {
8+
appVersion: appVersion
9+
};
10+
11+
var jsonContent = JSON.stringify(jsonData);
12+
13+
fs.writeFile('./public/version.json', jsonContent, 'utf8', function(err) {
14+
if (err) {
15+
console.log('An error occured while writing JSON Object to version.json');
16+
return console.log(err);
17+
}
18+
19+
console.log('version.json file has been saved with latest version number');
20+
});
21+
22+
/* eslint-enable */

package.json

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,21 @@
22
"name": "cache-busting-example",
33
"version": "0.1.0",
44
"private": true,
5+
"repository": {
6+
"type": "git",
7+
"url": "git@github.com:flexdinesh/cache-busting-example.git"
8+
},
9+
"license": "MIT",
10+
"author": "Dineshkumar Pandiyan <flexdinesh@gmail.com>",
511
"dependencies": {
612
"react": "^16.8.6",
713
"react-dom": "^16.8.6",
814
"react-scripts": "2.1.8"
915
},
1016
"scripts": {
17+
"clean": "rm -rf build",
18+
"prebuild": "npm run generate-build-version",
19+
"generate-build-version": "node generate-build-version",
1120
"start": "react-scripts start",
1221
"build": "react-scripts build",
1322
"test": "react-scripts test",

public/meta.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"version": "0.0.1"
3+
}

src/App.css

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,12 @@
99
}
1010

1111
.App-header {
12-
background-color: #282c34;
1312
min-height: 100vh;
1413
display: flex;
1514
flex-direction: column;
1615
align-items: center;
1716
justify-content: center;
1817
font-size: calc(10px + 2vmin);
19-
color: white;
2018
}
2119

2220
.App-link {

src/App.js

Lines changed: 11 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,20 @@
11
import React, { Component } from 'react';
2-
import logo from './logo.svg';
2+
import CacheBuster from './CacheBuster';
33
import './App.css';
44

55
class App extends Component {
66
render() {
77
return (
8-
<div className="App">
9-
<header className="App-header">
10-
<img src={logo} className="App-logo" alt="logo" />
11-
<p>
12-
Edit <code>src/App.js</code> and save to reload.
13-
</p>
14-
<a
15-
className="App-link"
16-
href="https://reactjs.org"
17-
target="_blank"
18-
rel="noopener noreferrer"
19-
>
20-
Learn React
21-
</a>
22-
</header>
23-
</div>
8+
<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>
17+
</CacheBuster>
2418
);
2519
}
2620
}

src/CacheBuster.js

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import React from 'react';
2+
import { semverGreaterThan } from './helper';
3+
import packageJson from '../package.json';
4+
global.appVersion = packageJson.version;
5+
6+
class CacheBuster extends React.Component {
7+
constructor(props) {
8+
super(props);
9+
this.state = {
10+
isAppReady: false
11+
};
12+
}
13+
14+
componentDidMount() {
15+
fetch('/meta.json')
16+
.then((response) => response.json())
17+
.then((meta) => {
18+
console.log({ meta });
19+
const latestVersion = meta.version;
20+
const currentVersion = global.appVersion;
21+
22+
const shouldForceRefresh = semverGreaterThan(latestVersion, currentVersion);
23+
if (shouldForceRefresh) {
24+
console.log(`We have a new version - ${latestVersion}. Going to force refresh`);
25+
window.location.reload(true);
26+
} else {
27+
console.log(`You already have the latest version - ${latestVersion}. No cache refresh needed.`);
28+
}
29+
30+
this.setState({ isAppReady: true });
31+
});
32+
}
33+
render() {
34+
const { isAppReady } = this.state;
35+
if (!isAppReady) return null;
36+
return this.props.children;
37+
}
38+
}
39+
40+
export default CacheBuster;

src/helper.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// version from response - first param, local version second param
2+
export const semverGreaterThan = (versionA, versionB) => {
3+
const versionsA = versionA.split(/\./g);
4+
5+
const versionsB = versionB.split(/\./g);
6+
while (versionsA.length || versionsB.length) {
7+
const a = Number(versionsA.shift());
8+
9+
const b = Number(versionsB.shift());
10+
// eslint-disable-next-line no-continue
11+
if (a === b) continue;
12+
// eslint-disable-next-line no-restricted-globals
13+
return a > b || isNaN(b);
14+
}
15+
return false;
16+
};

src/index.css

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,12 @@
11
body {
22
margin: 0;
33
padding: 0;
4-
font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "Roboto", "Oxygen",
5-
"Ubuntu", "Cantarell", "Fira Sans", "Droid Sans", "Helvetica Neue",
6-
sans-serif;
4+
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', 'Roboto', 'Oxygen', 'Ubuntu', 'Cantarell', 'Fira Sans',
5+
'Droid Sans', 'Helvetica Neue', sans-serif;
76
-webkit-font-smoothing: antialiased;
87
-moz-osx-font-smoothing: grayscale;
98
}
109

1110
code {
12-
font-family: source-code-pro, Menlo, Monaco, Consolas, "Courier New",
13-
monospace;
11+
font-family: source-code-pro, Menlo, Monaco, Consolas, 'Courier New', monospace;
1412
}

src/logo.svg

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

yarn.lock

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7925,7 +7925,7 @@ react-dev-utils@^8.0.0:
79257925
strip-ansi "5.0.0"
79267926
text-table "0.2.0"
79277927

7928-
react-dom@16.8.6:
7928+
react-dom@^16.8.6:
79297929
version "16.8.6"
79307930
resolved "https://registry.yarnpkg.com/react-dom/-/react-dom-16.8.6.tgz#71d6303f631e8b0097f56165ef608f051ff6e10f"
79317931
integrity sha512-1nL7PIq9LTL3fthPqwkvr2zY7phIPjYrT0jp4HjyEQrEROnw4dG41VVwi/wfoCneoleqrNX7iAD+pXebJZwrwA==
@@ -7999,7 +7999,7 @@ react-scripts@2.1.8:
79997999
optionalDependencies:
80008000
fsevents "1.2.4"
80018001

8002-
react@16.8.6:
8002+
react@^16.8.6:
80038003
version "16.8.6"
80048004
resolved "https://registry.yarnpkg.com/react/-/react-16.8.6.tgz#ad6c3a9614fd3a4e9ef51117f54d888da01f2bbe"
80058005
integrity sha512-pC0uMkhLaHm11ZSJULfOBqV4tIZkx87ZLvbbQYunNixAAvjnC+snJCg0XQXn9VIsttVsbZP/H/ewzgsd5fxKXw==

0 commit comments

Comments
 (0)