Skip to content

Commit 7c4554c

Browse files
committed
Check network status for chunk error
1 parent 97247ea commit 7c4554c

File tree

8 files changed

+170
-13
lines changed

8 files changed

+170
-13
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
"@loadable/component": "^5.11.0",
1414
"@loadable/server": "^5.11.0",
1515
"@reduxjs/toolkit": "^1.2.2",
16+
"@sentry/browser": "^5.11.1",
1617
"@svgr/webpack": "4.3.3",
1718
"@testing-library/jest-dom": "^4.2.4",
1819
"@testing-library/react": "^9.3.2",
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
import React, { useState, useEffect } from 'react';
2+
import ErrorScreenTemplate from './ErrorScreenTemplate';
3+
import { undrawUpdate } from '../../static/images';
4+
import { useNetwork } from 'react-use';
5+
import apiClient from '../../lib/api/apiClient';
6+
import NetworkErrorScreen from './NetworkErrorScreen';
7+
8+
export type ChunkErrorScreenProps = {};
9+
10+
async function checkNetwork() {
11+
try {
12+
await apiClient.get('/api/v2/check', {
13+
timeout: 5000,
14+
});
15+
return true;
16+
} catch (e) {
17+
return false;
18+
}
19+
}
20+
21+
function ChunkErrorScreen(props: ChunkErrorScreenProps) {
22+
const [networkStatus, setNetworkStatus] = useState<
23+
'offline' | 'online' | null
24+
>(null);
25+
26+
const network = useNetwork();
27+
28+
console.log(networkStatus);
29+
30+
useEffect(() => {
31+
if (network && network.online !== undefined) {
32+
setNetworkStatus(network.online ? 'online' : 'offline');
33+
}
34+
}, [network]);
35+
36+
useEffect(() => {
37+
const fn = async () => {
38+
const online = await checkNetwork();
39+
setNetworkStatus(online ? 'online' : 'offline');
40+
};
41+
fn();
42+
}, [network.online]);
43+
44+
if (networkStatus === null) return null;
45+
if (networkStatus === 'online') {
46+
return (
47+
<ErrorScreenTemplate
48+
image={undrawUpdate}
49+
message={
50+
'벨로그가 업데이트 되었습니다. \n새로고침 후 다시 시도해주세요.'
51+
}
52+
onButtonClick={() => window.location.reload()}
53+
buttonText="새로고침"
54+
/>
55+
);
56+
}
57+
return <NetworkErrorScreen />;
58+
}
59+
60+
export default ChunkErrorScreen;
Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,21 @@
11
import React from 'react';
22
import styled from 'styled-components';
3+
import ErrorScreenTemplate from './ErrorScreenTemplate';
4+
import { undrawBugFixing } from '../../static/images';
5+
import { useHistory } from 'react-router-dom';
36

47
export type CrashErrorScreenProps = {};
58

69
function CrashErrorScreen(props: CrashErrorScreenProps) {
7-
return <div></div>;
10+
const history = useHistory();
11+
return (
12+
<ErrorScreenTemplate
13+
image={undrawBugFixing}
14+
message="엇! 오류가 발생했습니다."
15+
buttonText="홈으로"
16+
onButtonClick={() => history.push('/')}
17+
/>
18+
);
819
}
920

10-
const Screen = styled.div`
11-
width: 100%;
12-
height: 100%;
13-
display: flex;
14-
`;
15-
1621
export default CrashErrorScreen;

src/components/error/ErrorBoundary.tsx

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,35 @@
11
import React from 'react';
22
import useNotFound from '../../lib/hooks/useNotFound';
33
import NotFoundPage from '../../pages/NotFoundPage';
4+
import * as Sentry from '@sentry/browser';
5+
import CrashErrorScreen from './CrashErrorScreen';
6+
import ChunkErrorScreen from './ChunkErrorScreen';
47

58
class ErrorBoundary extends React.Component {
69
state = {
710
hasError: false,
11+
chunkError: false,
812
};
913
static getDerivedStateFromError(error: Error) {
14+
if (error.name === 'ChunkLoadError') {
15+
return {
16+
chunkError: true,
17+
};
18+
}
1019
return { hasError: true };
1120
}
1221
componentDidCatch(error: Error, errorInfo: any) {
13-
console.log({
14-
error,
15-
errorInfo,
16-
stringifiedError: JSON.stringify(error || {}),
17-
});
22+
if (process.env.NODE_ENV === 'production') {
23+
Sentry.captureException(error);
24+
}
1825
}
1926
render() {
27+
if (this.state.chunkError) {
28+
return <ChunkErrorScreen />;
29+
}
30+
if (this.state.hasError) {
31+
return <CrashErrorScreen />;
32+
}
2033
return (
2134
<ErrorBoundaryWrapper hasError={this.state.hasError}>
2235
{this.props.children}

src/components/error/ErrorScreenTemplate.tsx

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,11 +49,14 @@ const Screen = styled.div`
4949
.message {
5050
padding-left: 1rem;
5151
padding-right: 1rem;
52-
52+
white-space: pre;
53+
text-align: center;
54+
line-height: 1.5;
5355
font-size: 2.5rem;
5456
color: ${palette.gray8};
5557
margin-top: 2rem;
5658
${media.small} {
59+
font-size: 1.5rem;
5760
margin-top: 1rem;
5861
}
5962
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import React from 'react';
2+
import ErrorScreenTemplate from './ErrorScreenTemplate';
3+
import { undrawServerDown } from '../../static/images';
4+
5+
export type NetworkErrorScreenProps = {};
6+
7+
function NetworkErrorScreen(props: NetworkErrorScreenProps) {
8+
return (
9+
<ErrorScreenTemplate
10+
image={undrawServerDown}
11+
message={'서버와의 연결이 불안정합니다.\n잠시 후 시도해주세요.'}
12+
buttonText="새로고침"
13+
onButtonClick={() => window.location.reload()}
14+
/>
15+
);
16+
}
17+
18+
export default NetworkErrorScreen;

src/index.tsx

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,11 @@ import client from './lib/graphql/client';
1414
import rootReducer from './modules';
1515
import storage from './lib/storage';
1616
import { setUser } from './modules/core';
17+
import * as Sentry from '@sentry/browser';
18+
19+
Sentry.init({
20+
dsn: 'https://[email protected]/1886813',
21+
});
1722

1823
const store = createStore(
1924
rootReducer,

yarn.lock

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1204,6 +1204,58 @@
12041204
redux-thunk "^2.3.0"
12051205
reselect "^4.0.0"
12061206

1207+
"@sentry/browser@^5.11.1":
1208+
version "5.11.1"
1209+
resolved "https://registry.yarnpkg.com/@sentry/browser/-/browser-5.11.1.tgz#337ffcb52711b23064c847a07629e966f54a5ebb"
1210+
integrity sha512-oqOX/otmuP92DEGRyZeBuQokXdeT9HQRxH73oqIURXXNLMP3PWJALSb4HtT4AftEt/2ROGobZLuA4TaID6My/Q==
1211+
dependencies:
1212+
"@sentry/core" "5.11.1"
1213+
"@sentry/types" "5.11.0"
1214+
"@sentry/utils" "5.11.1"
1215+
tslib "^1.9.3"
1216+
1217+
1218+
version "5.11.1"
1219+
resolved "https://registry.yarnpkg.com/@sentry/core/-/core-5.11.1.tgz#9e2da485e196ae32971545c1c49ee6fe719930e2"
1220+
integrity sha512-BpvPosVNT20Xso4gAV54Lu3KqDmD20vO63HYwbNdST5LUi8oYV4JhvOkoBraPEM2cbBwQvwVcFdeEYKk4tin9A==
1221+
dependencies:
1222+
"@sentry/hub" "5.11.1"
1223+
"@sentry/minimal" "5.11.1"
1224+
"@sentry/types" "5.11.0"
1225+
"@sentry/utils" "5.11.1"
1226+
tslib "^1.9.3"
1227+
1228+
1229+
version "5.11.1"
1230+
resolved "https://registry.yarnpkg.com/@sentry/hub/-/hub-5.11.1.tgz#ddcb865563fae53852d405885c46b4c6de68a91b"
1231+
integrity sha512-ucKprYCbGGLLjVz4hWUqHN9KH0WKUkGf5ZYfD8LUhksuobRkYVyig0ZGbshECZxW5jcDTzip4Q9Qimq/PkkXBg==
1232+
dependencies:
1233+
"@sentry/types" "5.11.0"
1234+
"@sentry/utils" "5.11.1"
1235+
tslib "^1.9.3"
1236+
1237+
1238+
version "5.11.1"
1239+
resolved "https://registry.yarnpkg.com/@sentry/minimal/-/minimal-5.11.1.tgz#0e705d01a567282d8fbbda2aed848b4974cc3cec"
1240+
integrity sha512-HK8zs7Pgdq7DsbZQTThrhQPrJsVWzz7MaluAbQA0rTIAJ3TvHKQpsVRu17xDpjZXypqWcKCRsthDrC4LxDM1Bg==
1241+
dependencies:
1242+
"@sentry/hub" "5.11.1"
1243+
"@sentry/types" "5.11.0"
1244+
tslib "^1.9.3"
1245+
1246+
1247+
version "5.11.0"
1248+
resolved "https://registry.yarnpkg.com/@sentry/types/-/types-5.11.0.tgz#40f0f3174362928e033ddd9725d55e7c5cb7c5b6"
1249+
integrity sha512-1Uhycpmeo1ZK2GLvrtwZhTwIodJHcyIS6bn+t4IMkN9MFoo6ktbAfhvexBDW/IDtdLlCGJbfm8nIZerxy0QUpg==
1250+
1251+
1252+
version "5.11.1"
1253+
resolved "https://registry.yarnpkg.com/@sentry/utils/-/utils-5.11.1.tgz#aa19fcc234cf632257b2281261651d2fac967607"
1254+
integrity sha512-O0Zl4R2JJh8cTkQ8ZL2cDqGCmQdpA5VeXpuBbEl1v78LQPkBDISi35wH4mKmLwMsLBtTVpx2UeUHBj0KO5aLlA==
1255+
dependencies:
1256+
"@sentry/types" "5.11.0"
1257+
tslib "^1.9.3"
1258+
12071259
"@sheerun/mutationobserver-shim@^0.3.2":
12081260
version "0.3.2"
12091261
resolved "https://registry.yarnpkg.com/@sheerun/mutationobserver-shim/-/mutationobserver-shim-0.3.2.tgz#8013f2af54a2b7d735f71560ff360d3a8176a87b"

0 commit comments

Comments
 (0)