import App from 'next/app' import sentry from '../utils/sentry' const { Sentry, captureException } = sentry({ release: process.env.SENTRY_RELEASE }) export default class MyApp extends App { constructor () { super(...arguments) this.state = { hasError: false, errorEventId: undefined } } static async getInitialProps ({ Component, ctx }) { try { let pageProps = {} if (Component.getInitialProps) { pageProps = await Component.getInitialProps(ctx) } return { pageProps } } catch (error) { // Capture errors that happen during a page's getInitialProps. // This will work on both client and server sides. const errorEventId = captureException(error, ctx) return { hasError: true, errorEventId } } } static getDerivedStateFromProps (props, state) { // If there was an error generated within getInitialProps, and we haven't // yet seen an error, we add it to this.state here return { hasError: props.hasError || state.hasError || false, errorEventId: props.errorEventId || state.errorEventId || undefined } } static getDerivedStateFromError () { // React Error Boundary here allows us to set state flagging the error (and // later render a fallback UI). return { hasError: true } } componentDidCatch (error, errorInfo) { const errorEventId = captureException(error, { errorInfo }) // Store the event id at this point as we don't have access to it within // `getDerivedStateFromError`. this.setState({ errorEventId }) } render () { return this.state.hasError ? (

There was an error!

Sentry.showReportDialog({ eventId: this.state.errorEventId })} > 📣 Report this error

{ window.location.reload(true) }} > Or, try reloading the page

) : ( // Render the normal Next.js page super.render() ) } }