-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Next.js is unable to render RichText on server side #41
- Loading branch information
1 parent
f739252
commit 0eac1ff
Showing
7 changed files
with
76 additions
and
54 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains 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,49 @@ | ||
'use client'; | ||
|
||
import React, {Component, ReactNode} from 'react'; | ||
import {ErrorComponent} from '../ErrorComponent'; | ||
|
||
interface ErrorBoundaryProps { | ||
children: ReactNode | ||
Fallback: ({error}: { error: Error }) => React.JSX.Element | ||
} | ||
|
||
interface ErrorBoundaryState { | ||
hasError: boolean | ||
error: Error | ||
} | ||
|
||
class ErrorBoundary extends Component<ErrorBoundaryProps, ErrorBoundaryState> { | ||
|
||
constructor(props: ErrorBoundaryProps) { | ||
super(props); | ||
this.state = {hasError: false, error: new Error()}; | ||
} | ||
|
||
static getDerivedStateFromError(error) { | ||
// Update state so react shows the fallback UI | ||
return {hasError: true}; | ||
} | ||
|
||
componentDidCatch(error, errorInfo) { | ||
// Log the error to an error reporting service console | ||
this.setState({error}); | ||
} | ||
|
||
render() { | ||
if (this.state.hasError) { | ||
// You can render any custom fallback UI | ||
return <this.props.Fallback error={this.state.error}/> | ||
} | ||
|
||
return this.props.children; | ||
} | ||
} | ||
|
||
export function ErrorBoundaryClient({children}: { children: React.ReactNode }) { | ||
return ( | ||
<ErrorBoundary Fallback={({error}) => <ErrorComponent>{error.message}</ErrorComponent>}> | ||
{children} | ||
</ErrorBoundary> | ||
); | ||
} |
This file contains 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,6 @@ | ||
import React from 'react'; | ||
|
||
// ErrorBoundaries are not supported on server in Next.js | ||
export function ErrorBoundaryServer({children}: { children: React.ReactNode }) { | ||
return <>{children}</>; | ||
} |
This file contains 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,12 @@ | ||
import React from 'react'; | ||
|
||
export function ErrorBoundaryWrapper({children}: { children: React.ReactNode }) { | ||
if (typeof window === 'undefined') { | ||
const {ErrorBoundaryServer} = require('./ErrorBoundaryServer'); | ||
return <ErrorBoundaryServer>{children}</ErrorBoundaryServer>; | ||
} else { | ||
const {ErrorBoundaryClient} = require('./ErrorBoundaryClient'); | ||
return <ErrorBoundaryClient>{children}</ErrorBoundaryClient>; | ||
} | ||
} | ||
|
This file contains 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
This file contains 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
This file contains 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