From dd347129e7737862b9079f7a970b112f3944a5f2 Mon Sep 17 00:00:00 2001 From: Tiago Gimenes Date: Tue, 15 Jun 2021 16:38:53 -0300 Subject: [PATCH] Handle chunkLoadError (#771) --- .../src/components/Error/ErrorBoundary.tsx | 9 ++----- .../src/components/Error/ErrorHandler.tsx | 25 ++++++++++--------- .../gatsby-theme-store/src/gatsby-browser.tsx | 18 ++++++++++++- 3 files changed, 32 insertions(+), 20 deletions(-) diff --git a/packages/gatsby-theme-store/src/components/Error/ErrorBoundary.tsx b/packages/gatsby-theme-store/src/components/Error/ErrorBoundary.tsx index 08e2de8bdd..9240224209 100644 --- a/packages/gatsby-theme-store/src/components/Error/ErrorBoundary.tsx +++ b/packages/gatsby-theme-store/src/components/Error/ErrorBoundary.tsx @@ -1,13 +1,8 @@ -import type { ReactNode } from 'react' import React, { Component } from 'react' import ErrorHandler from './ErrorHandler' -type Props = { - children: ReactNode -} - -class ErrorBoundary extends Component { +class ErrorBoundary extends Component { public state = { hasError: false, error: null } public static getDerivedStateFromError(error: any) { @@ -18,7 +13,7 @@ class ErrorBoundary extends Component { } public render() { - if (this.state.hasError) { + if (this.state.hasError && process.env.NODE_ENV === 'production') { return ( {this.props.children} diff --git a/packages/gatsby-theme-store/src/components/Error/ErrorHandler.tsx b/packages/gatsby-theme-store/src/components/Error/ErrorHandler.tsx index ba89617720..fa4da81c40 100644 --- a/packages/gatsby-theme-store/src/components/Error/ErrorHandler.tsx +++ b/packages/gatsby-theme-store/src/components/Error/ErrorHandler.tsx @@ -4,8 +4,8 @@ * style or change the error page, please shadow the `pages/500.tsx` instead. * This component is synchronously imported and has a big TBT implication */ -import type { FC } from 'react' import { useEffect } from 'react' +import type { FC } from 'react' import { uuidv4 } from '../../sdk/uuid' @@ -18,8 +18,6 @@ export const handleError = ({ error, errorId }: Props) => { console.error(error) console.error(errorId) - const isUserOffline = !window.navigator.onLine - // prevent infinite loop if ( window.location.pathname.startsWith('/404') || @@ -29,18 +27,21 @@ export const handleError = ({ error, errorId }: Props) => { return } - if (isUserOffline) { - const previousPagePath = encodeURIComponent(window.location.pathname) + const from = encodeURIComponent(window.location.pathname) - window.location.href = `/offline?from=${previousPagePath}` + const isOffline = !window.navigator.onLine + const is404 = error?.extensions?.exception?.status === 404 + const isFrameworkLevelError = error.name === 'ChunkLoadError' - return + if (isFrameworkLevelError) { + window.location.reload() + } else if (isOffline) { + window.location.href = `/offline?from=${from}` + } else if (is404) { + window.location.href = `/404?from=${from}` + } else { + window.location.href = `/500?from=${from}&errorId=${errorId ?? uuidv4()}` } - - window.location.href = - error?.extensions?.exception?.status === 404 - ? `/404?from=${window.location.pathname}` - : `/500?from=${window.location.pathname}&errorId=${errorId ?? uuidv4()}` } const ErrorHandler: FC = ({ error, errorId }) => { diff --git a/packages/gatsby-theme-store/src/gatsby-browser.tsx b/packages/gatsby-theme-store/src/gatsby-browser.tsx index 6135b4bd53..5acee391b0 100644 --- a/packages/gatsby-theme-store/src/gatsby-browser.tsx +++ b/packages/gatsby-theme-store/src/gatsby-browser.tsx @@ -6,7 +6,7 @@ import 'requestidlecallback-polyfill' import React, { StrictMode } from 'react' import { UIProvider } from '@vtex/store-sdk' import ReactDOM from 'react-dom' -import type { WrapRootElementBrowserArgs } from 'gatsby' +import type { RouteUpdateArgs, WrapRootElementBrowserArgs } from 'gatsby' import type { ReactChild } from 'react' import ErrorBoundary from './components/Error/ErrorBoundary' @@ -84,3 +84,19 @@ export const wrapPageElement = ({ export const onRouteUpdate = () => { progressOnRouteUpdate() } + +let nextRoute: string | undefined + +export const onPreRouteUpdate = ({ location }: RouteUpdateArgs) => { + nextRoute = location.pathname +} + +window.addEventListener('unhandledrejection', (event) => { + if (event.reason.name === 'ChunkLoadError') { + if (nextRoute) { + window.location.pathname = nextRoute + } else { + window.location.reload() + } + } +})