Skip to content

Commit 1f07d15

Browse files
author
sw-yx
committed
error boundary
1 parent e8cd070 commit 1f07d15

File tree

3 files changed

+41
-18
lines changed

3 files changed

+41
-18
lines changed

.size-snapshot.json

+15-15
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,28 @@
11
{
22
"/Users/swyx/Netlify/react-netlify-identity-widget/dist/reactNetlifyIdentityWidget.cjs.development.js": {
3-
"bundled": 12030,
4-
"minified": 7820,
5-
"gzipped": 1999
3+
"bundled": 12909,
4+
"minified": 8132,
5+
"gzipped": 2130
66
},
77
"/Users/swyx/Netlify/react-netlify-identity-widget/dist/reactNetlifyIdentityWidget.cjs.production.js": {
8-
"bundled": 12030,
9-
"minified": 7820,
10-
"gzipped": 1999
8+
"bundled": 12909,
9+
"minified": 8132,
10+
"gzipped": 2130
1111
},
1212
"/Users/swyx/Netlify/react-netlify-identity-widget/dist/reactNetlifyIdentityWidget.umd.development.js": {
13-
"bundled": 12961,
14-
"minified": 6790,
15-
"gzipped": 1950
13+
"bundled": 13890,
14+
"minified": 7066,
15+
"gzipped": 2077
1616
},
1717
"/Users/swyx/Netlify/react-netlify-identity-widget/dist/reactNetlifyIdentityWidget.umd.production.js": {
18-
"bundled": 12961,
19-
"minified": 6790,
20-
"gzipped": 1950
18+
"bundled": 13890,
19+
"minified": 7066,
20+
"gzipped": 2077
2121
},
2222
"/Users/swyx/Netlify/react-netlify-identity-widget/dist/reactNetlifyIdentityWidget.es.production.js": {
23-
"bundled": 11795,
24-
"minified": 7588,
25-
"gzipped": 1939,
23+
"bundled": 12674,
24+
"minified": 7900,
25+
"gzipped": 2070,
2626
"treeshaked": {
2727
"rollup": {
2828
"code": 5599,

src/errorBoundary.tsx

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import React from "react"
2+
3+
export class ErrorBoundary extends React.Component<{}, { hasError: boolean }> {
4+
constructor(props: {}) {
5+
super(props)
6+
this.state = { hasError: false }
7+
}
8+
9+
static getDerivedStateFromError() {
10+
// Update state so the next render will show the fallback UI.
11+
return { hasError: true }
12+
}
13+
render() {
14+
if (this.state.hasError) {
15+
// You can render any custom fallback UI
16+
return <h1>Something went loading the widget.</h1>
17+
}
18+
return this.props.children
19+
}
20+
}

src/index.tsx

+6-3
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import VisuallyHidden from "@reach/visually-hidden"
88
const Widget = React.lazy(() => import("./app"))
99

1010
import { FormStateContextProvider, SettingContext } from "./context"
11+
import { ErrorBoundary } from "./errorBoundary"
1112

1213
import { Settings, useNetlifyIdentity as _useNetlifyIdentity } from "react-netlify-identity"
1314

@@ -52,9 +53,11 @@ export function IdentityModal({ showDialog, onCloseDialog }: ModalProps) {
5253
<VisuallyHidden>Close</VisuallyHidden>
5354
</button>
5455
<FormStateContextProvider>
55-
<React.Suspense fallback={<div>Loading...</div>}>
56-
<Widget onCloseDialog={onCloseDialog} />
57-
</React.Suspense>
56+
<ErrorBoundary>
57+
<React.Suspense fallback={<div>Loading...</div>}>
58+
<Widget onCloseDialog={onCloseDialog} />
59+
</React.Suspense>
60+
</ErrorBoundary>
5861
</FormStateContextProvider>
5962
</Dialog>
6063
</SettingContext.Provider>

0 commit comments

Comments
 (0)