Skip to content

Commit 17ee6f0

Browse files
committed
Ping actual server for connectivity status
1 parent 43e257f commit 17ee6f0

File tree

3 files changed

+268
-0
lines changed

3 files changed

+268
-0
lines changed

blapp/frontend/src/commerce/index.tsx

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,10 @@ import { Provider } from 'react-redux'
44
import { applyMiddleware, compose, createStore, StoreEnhancer } from 'redux'
55
import { offline } from '@redux-offline/redux-offline'
66
import offlineConfig from '@redux-offline/redux-offline/lib/defaults'
7+
import { NetworkCallback } from '@redux-offline/redux-offline/lib/types'
78
import logger from 'redux-logger'
89
import thunk from 'redux-thunk'
10+
import isReachable from 'is-reachable'
911

1012
import { AppContainer } from 'blapp/commerce/containers/App'
1113
import reducer from 'blapp/commerce/reducers'
@@ -15,9 +17,34 @@ const persistCallback = () => {
1517
store.subscribe(() => doRender())
1618
}
1719

20+
const detectNetwork = (callback: NetworkCallback) => {
21+
const handle = (status: boolean) => {
22+
// Copy-ished from original redux-offline implementation
23+
if (window.requestAnimationFrame) {
24+
window.requestAnimationFrame(() => callback(status))
25+
} else {
26+
setTimeout(() => callback(status), 0)
27+
}
28+
}
29+
30+
const checkNetwork = () => {
31+
if (navigator.onLine === false) {
32+
handle(false)
33+
}
34+
35+
isReachable(location.origin, {timeout: 5000}).then(handle)
36+
}
37+
38+
window.addEventListener("online", checkNetwork)
39+
window.addEventListener("offline", checkNetwork)
40+
setInterval(checkNetwork, 60000)
41+
checkNetwork()
42+
}
43+
1844
const offlineConf = {
1945
...offlineConfig,
2046
persistCallback,
47+
detectNetwork,
2148
}
2249

2350
const store = createStore(

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
"@fortawesome/fontawesome-free-solid": "^5.0.13",
88
"@fortawesome/react-fontawesome": "^0.0.20",
99
"@redux-offline/redux-offline": "^2.3.3",
10+
"@types/is-reachable": "^3.0.0",
1011
"@types/luxon": "^0.5.2",
1112
"@types/ramda": "^0.25.32",
1213
"@types/react": "^16.3.16",
@@ -21,6 +22,7 @@
2122
"autoprefixer": "^8.6.3",
2223
"bootstrap": "^4.1.1",
2324
"css-loader": "^0.28.11",
25+
"is-reachable": "^3.0.0",
2426
"luxon": "^1.2.1",
2527
"node-sass": "^4.9.0",
2628
"npm-run-all": "^4.1.3",

0 commit comments

Comments
 (0)