-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
feat(ui): add React error boundary to Layout to recover from render errors #5539
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
q404365631
wants to merge
2
commits into
asyncapi:master
Choose a base branch
from
q404365631:chore/error-boundaries-5047
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+83
−9
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| 'asyncapi-website': patch | ||
| --- | ||
|
|
||
| feat(ui): add a top-level React `ErrorBoundary` and wrap every `Layout` return tree in it, so a render error in any descendant component (broken MDX page, future page, etc.) no longer tears down the whole site — it shows a recoverable fallback instead. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,54 @@ | ||
| import { Component, ErrorInfo, ReactNode } from 'react'; | ||
|
|
||
| interface ErrorBoundaryProps { | ||
| children: ReactNode; | ||
| fallback?: ReactNode; | ||
| } | ||
|
|
||
| interface ErrorBoundaryState { | ||
| hasError: boolean; | ||
| error: Error | null; | ||
| } | ||
|
|
||
| /** | ||
| * Top-level ErrorBoundary for the AsyncAPI website. | ||
| * | ||
| * Wrapping the whole app in <ErrorBoundary> at the layout level means a render | ||
| * error in any descendant component (e.g. a future page, a broken MDX block) | ||
| * no longer tears down the whole site — it just shows a small recoverable | ||
| * fallback so navigation still works. | ||
| * | ||
| * See: https://react.dev/reference/react/Component#catching-rendering-errors-with-an-error-boundary | ||
| */ | ||
| export default class ErrorBoundary extends Component<ErrorBoundaryProps, ErrorBoundaryState> { | ||
| constructor(props: ErrorBoundaryProps) { | ||
| super(props); | ||
| this.state = { hasError: false, error: null }; | ||
| } | ||
|
|
||
| static getDerivedStateFromError(error: Error): ErrorBoundaryState { | ||
| return { hasError: true, error }; | ||
| } | ||
|
|
||
| componentDidCatch(error: Error, info: ErrorInfo): void { | ||
| // Surface the error to the browser console; in production a real Sentry | ||
| // hook would go here. | ||
| // eslint-disable-next-line no-console | ||
| console.error('[ErrorBoundary] Caught a render error:', error, info); | ||
| } | ||
|
|
||
| render(): ReactNode { | ||
| if (this.state.hasError) { | ||
| if (this.props.fallback) { | ||
| return this.props.fallback; | ||
| } | ||
| return ( | ||
| <div role="alert" style={{ padding: '2rem', fontFamily: 'system-ui, sans-serif' }}> | ||
| <h1>Something went wrong.</h1> | ||
| <p>Please refresh the page, or use the navigation to continue.</p> | ||
| </div> | ||
| ); | ||
| } | ||
| return this.props.children; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🧩 Analysis chain
🏁 Script executed:
Repository: asyncapi/website
Length of output: 8841
🏁 Script executed:
Repository: asyncapi/website
Length of output: 1945
🏁 Script executed:
Repository: asyncapi/website
Length of output: 19431
Move
ErrorBoundaryto wrapchildren, not the whole docs/blog/post layoutIn
components/layout/Layout.tsx,ErrorBoundarywrapsDocsLayout/BlogLayout/GenericPostLayout, so a render error in descendants replaces the entire section chrome (e.g., sidebar/TOC/header) with the fallback (see around lines 32–36, 45–49, 66–68). Also,components/ErrorBoundary.tsxhas no reset logic—oncehasErroris set, it will keep showing the fallback until the boundary is unmounted/remounted (so in-branch navigation can get stuck).Move the boundary inside each layout to wrap only the
childrenslot (or add a reset mechanism keyed to the route/content).🤖 Prompt for AI Agents
🧩 Analysis chain
🏁 Script executed:
Repository: asyncapi/website
Length of output: 165
🏁 Script executed:
Repository: asyncapi/website
Length of output: 4259
🏁 Script executed:
Repository: asyncapi/website
Length of output: 2717
🏁 Script executed:
Repository: asyncapi/website
Length of output: 16700
🏁 Script executed:
Repository: asyncapi/website
Length of output: 4609
🏁 Script executed:
Repository: asyncapi/website
Length of output: 2895
🏁 Script executed:
Repository: asyncapi/website
Length of output: 3473
🏁 Script executed:
Repository: asyncapi/website
Length of output: 12231
🏁 Script executed:
Repository: asyncapi/website
Length of output: 12750
Reset the ErrorBoundary when navigation changes
components/ErrorBoundary.tsxsetshasErrorin state and never clears it, so once a boundary enters the fallback it will stay there. Sincecomponents/layout/Layout.tsxis mounted inpages/_app.tsxand the/docs/*//blog/*branches reuse the same (unkeyed)<ErrorBoundary>instance during client-side navigation, the fallback won’t clear when moving within the same branch. Key each<ErrorBoundary>by a navigation-changing value (userouter.asPath), or add aresetKey-driven reset insidecomponents/ErrorBoundary.tsx.Apply to:
components/layout/Layout.tsxline ranges 32-36, 45-49, 56-58, 66-68, and 74.🤖 Prompt for AI Agents