-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathApp.js
92 lines (83 loc) · 2.88 KB
/
App.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
import React, { lazy, useEffect, useContext } from "react";
import {
BrowserRouter as Router,
Switch,
Route,
Redirect,
} from "react-router-dom";
import AccessibleNavigationAnnouncer from "./components/AccessibleNavigationAnnouncer";
import packageJson from "../package.json";
import PrivateRoute from "./privateRoute";
import { AuthContext } from "./context/AuthContext";
global.appVersion = packageJson.version;
const Layout = lazy(() => import("./containers/Layout"));
const Login = lazy(() => import("./pages/Login"));
const CreateAccount = lazy(() => import("./pages/CreateAccount"));
const ForgotPassword = lazy(() => import("./pages/ForgotPassword"));
const semverGreaterThan = (versionA, versionB) => {
const versionsA = versionA.split(/\./g);
const versionsB = versionB.split(/\./g);
while (versionsA.length || versionsB.length) {
const a = Number(versionsA.shift());
const b = Number(versionsB.shift());
// eslint-disable-next-line no-continue
if (a === b) continue;
// eslint-disable-next-line no-restricted-globals
return a > b || isNaN(b);
}
return false;
};
function App() {
const { auth } = useContext(AuthContext);
console.log("auth >> ", auth?.data);
useEffect(() => {
fetch("/meta.json")
.then((response) => response.json())
.then((meta) => {
const latestVersion = meta.version;
const currentVersion = global.appVersion;
const shouldForceRefresh = semverGreaterThan(
latestVersion,
currentVersion
);
if (shouldForceRefresh) {
console.log(
`We have a new version - ${latestVersion}. Should force refresh`
);
if (caches) {
// Service worker cache should be cleared with caches.delete()
console.log("Invalidating cache..!");
caches.keys().then(function (names) {
for (let name of names) caches.delete(name);
});
} // delete browser cache and hard reload
// window.location.reload(true);
window.location.reload();
// setLoading(false);
// setLatestVersion(false);
} else {
console.log(
`You already have the latest version - ${latestVersion}. No cache refresh needed.`
);
// setLoading(false);
// setLatestVersion(true);
}
}, []);
});
return (
<>
<Router>
<AccessibleNavigationAnnouncer />
<Switch>
<Route path="/login" component={Login} />
<Route path="/create-account" component={CreateAccount} />
<Route path="/forgot-password" component={ForgotPassword} />
{/* <Route path="/" component={Layout} /> */}
{/* If you have an index page, you can remothis Redirect */}
<PrivateRoute path="/" component={Layout} />
</Switch>
</Router>
</>
);
}
export default App;