diff --git a/components/errorBoundary/errorBoundary.jsx b/components/errorBoundary/errorBoundary.jsx new file mode 100644 index 0000000..8caff0e --- /dev/null +++ b/components/errorBoundary/errorBoundary.jsx @@ -0,0 +1,34 @@ +import { Component } from 'react' + +// Adapted from https://nextjs.org/docs/pages/building-your-application/configuring/error-handling +class ErrorBoundary extends Component { + constructor(props) { + super(props) + this.state = { hasError: false } + } + static getDerivedStateFromError(error) { + return { hasError: true } + } + componentDidCatch(error, errorInfo) { + console.log({ error, errorInfo }) + } + render() { + if (!this.state.hasError) { + return this.props.children + } + return ( +

+ There was a glitch.{' '} + +

+ ) + } +} + +export default ErrorBoundary diff --git a/pages/_app.js b/pages/_app.js index a4c7dc4..ef825bd 100644 --- a/pages/_app.js +++ b/pages/_app.js @@ -1,4 +1,5 @@ import DefaultHead from '../components/defaultHead/DefaultHead' +import ErrorBoundary from '../components/errorBoundary/errorBoundary' import Footer from '../components/footer/Footer' import styles from '../css/globals.css' @@ -6,8 +7,12 @@ function App({ Component, pageProps }) { return ( <> - -