Skip to content

Commit

Permalink
fix(core): always treat error boundary fallback as a callback (#7492)
Browse files Browse the repository at this point in the history
Co-authored-by: sebastienlorber <[email protected]>
  • Loading branch information
Josh-Cena and slorber authored May 26, 2022
1 parent e955ae4 commit 9cf2bf1
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 23 deletions.
23 changes: 11 additions & 12 deletions packages/docusaurus-module-type-aliases/src/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,14 +77,9 @@ declare module '@theme-original/*';
declare module '@theme-init/*';

declare module '@theme/Error' {
import type {ComponentProps} from 'react';
import type ErrorBoundary from '@docusaurus/ErrorBoundary';
import type {FallbackParams} from '@docusaurus/ErrorBoundary';

type ErrorProps = ComponentProps<
NonNullable<ComponentProps<typeof ErrorBoundary>['fallback']>
>;

export interface Props extends ErrorProps {}
export interface Props extends FallbackParams {}
export default function Error(props: Props): JSX.Element;
}

Expand Down Expand Up @@ -125,13 +120,17 @@ declare module '@docusaurus/constants' {
}

declare module '@docusaurus/ErrorBoundary' {
import type {ReactNode, ComponentType} from 'react';
import type {ReactNode} from 'react';

export type FallbackParams = {
readonly error: Error;
readonly tryAgain: () => void;
};

export type FallbackFunction = (params: FallbackParams) => JSX.Element;

export interface Props {
readonly fallback?: ComponentType<{
readonly error: Error;
readonly tryAgain: () => void;
}>;
readonly fallback?: FallbackFunction;
readonly children: ReactNode;
}
export default function ErrorBoundary(props: Props): JSX.Element;
Expand Down
4 changes: 3 additions & 1 deletion packages/docusaurus-theme-classic/src/theme/Layout/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,9 @@ export default function Layout(props: Props): JSX.Element {
<Navbar />

<div className={clsx(ThemeClassNames.wrapper.main, wrapperClassName)}>
<ErrorBoundary fallback={ErrorPageContent}>{children}</ErrorBoundary>
<ErrorBoundary fallback={(params) => <ErrorPageContent {...params} />}>
{children}
</ErrorBoundary>
</div>

{!noFooter && <Footer />}
Expand Down
4 changes: 1 addition & 3 deletions packages/docusaurus/src/client/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,12 @@ import SiteMetadataDefaults from './SiteMetadataDefaults';
// TODO, quick fix for CSS insertion order
// eslint-disable-next-line import/order
import ErrorBoundary from '@docusaurus/ErrorBoundary';
// eslint-disable-next-line import/order
import Error from '@theme/Error';

export default function App(): JSX.Element {
const routeElement = renderRoutes(routes);
const location = useLocation();
return (
<ErrorBoundary fallback={Error}>
<ErrorBoundary>
<DocusaurusContextProvider>
<BrowserContextProvider>
<Root>
Expand Down
22 changes: 16 additions & 6 deletions packages/docusaurus/src/client/exports/ErrorBoundary.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,21 @@

import React, {type ReactNode} from 'react';
import ExecutionEnvironment from '@docusaurus/ExecutionEnvironment';
import DefaultFallback from '@theme/Error';
import type {Props} from '@docusaurus/ErrorBoundary';
import ThemeError from '@theme/Error';
import type {
FallbackFunction,
FallbackParams,
Props,
} from '@docusaurus/ErrorBoundary';

type State = {
error: Error | null;
};

const DefaultFallback: FallbackFunction = (params) => (
<ThemeError {...params} />
);

export default class ErrorBoundary extends React.Component<Props, State> {
constructor(props: Props) {
super(props);
Expand All @@ -32,10 +40,12 @@ export default class ErrorBoundary extends React.Component<Props, State> {
const {error} = this.state;

if (error) {
const Fallback = this.props.fallback ?? DefaultFallback;
return (
<Fallback error={error} tryAgain={() => this.setState({error: null})} />
);
const fallbackParams: FallbackParams = {
error,
tryAgain: () => this.setState({error: null}),
};
const fallback: FallbackFunction = this.props.fallback ?? DefaultFallback;
return fallback(fallbackParams);
}

// See https://github.com/facebook/docusaurus/issues/6337#issuecomment-1012913647
Expand Down
8 changes: 7 additions & 1 deletion website/docs/docusaurus-core.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,13 @@ This component doesn't catch build-time errors and only protects against client-

#### Props {#errorboundary-props}

- `fallback`: a React component. The error boundary will render the component with two props: `error`, the error that was caught, and `tryAgain`, a function (`() => void`) callback to reset the error in the component and try rendering it again.
- `fallback`: an optional render callback returning a JSX element. It will receive an object with 2 attributes: `error`, the error that was caught, and `tryAgain`, a function (`() => void`) callback to reset the error in the component and try rendering it again. If not present, `@theme/Error` will be rendered instead. `@theme/Error` is used for the error boundaries wrapping the site, above the layout.

:::caution

The `fallback` prop is a callback, and **not a React functional component**. You can't use React hooks inside this callback.

:::

### `<Head/>` {#head}

Expand Down

0 comments on commit 9cf2bf1

Please sign in to comment.