diff --git a/starters/nextjs-starter-approuter-ts/components/smart-components/client-components.ts b/starters/nextjs-starter-approuter-ts/components/smart-components/client-components.ts index 7feaa999..22fed2f0 100644 --- a/starters/nextjs-starter-approuter-ts/components/smart-components/client-components.ts +++ b/starters/nextjs-starter-approuter-ts/components/smart-components/client-components.ts @@ -1,11 +1,12 @@ import { SmartComponentMap } from "@pantheon-systems/pcc-react-sdk/components"; import { serverSmartComponentMap } from "./server-components"; import MediaPreview from "./media-preview"; +import { withSmartComponentErrorBoundary } from "./error-boundary"; const clientSmartComponentMap: SmartComponentMap = { MEDIA_PREVIEW: { ...serverSmartComponentMap.MEDIA_PREVIEW, - reactComponent: MediaPreview, + reactComponent: withSmartComponentErrorBoundary(MediaPreview), }, }; diff --git a/starters/nextjs-starter-approuter-ts/components/smart-components/error-boundary.tsx b/starters/nextjs-starter-approuter-ts/components/smart-components/error-boundary.tsx new file mode 100644 index 00000000..81ce4662 --- /dev/null +++ b/starters/nextjs-starter-approuter-ts/components/smart-components/error-boundary.tsx @@ -0,0 +1,45 @@ +import React, { Component, Suspense } from "react"; + +interface Props { + children: React.ReactNode; +} + +export class SmartComponentErrorBoundary extends Component { + state = { + hasError: false, + }; + + static getDerivedStateFromError() { + return { hasError: true }; + } + + render() { + if (this.state.hasError) { + return ( +
+ Something went wrong while rendering this smart component. +
+ ); + } + + return this.props.children; + } +} + +const SmartComponentSuspenseErrorBoundary = ({ children }: Props) => { + return ( + + Loading...}>{children} + + ); +}; + +export const withSmartComponentErrorBoundary = + // eslint-disable-next-line react/display-name + (Component: React.ComponentType) => (props: Record) => ( + + + + ); + +export default SmartComponentErrorBoundary; \ No newline at end of file diff --git a/starters/nextjs-starter-ts/components/smart-components/error-boundary.tsx b/starters/nextjs-starter-ts/components/smart-components/error-boundary.tsx new file mode 100644 index 00000000..81ce4662 --- /dev/null +++ b/starters/nextjs-starter-ts/components/smart-components/error-boundary.tsx @@ -0,0 +1,45 @@ +import React, { Component, Suspense } from "react"; + +interface Props { + children: React.ReactNode; +} + +export class SmartComponentErrorBoundary extends Component { + state = { + hasError: false, + }; + + static getDerivedStateFromError() { + return { hasError: true }; + } + + render() { + if (this.state.hasError) { + return ( +
+ Something went wrong while rendering this smart component. +
+ ); + } + + return this.props.children; + } +} + +const SmartComponentSuspenseErrorBoundary = ({ children }: Props) => { + return ( + + Loading...}>{children} + + ); +}; + +export const withSmartComponentErrorBoundary = + // eslint-disable-next-line react/display-name + (Component: React.ComponentType) => (props: Record) => ( + + + + ); + +export default SmartComponentErrorBoundary; \ No newline at end of file diff --git a/starters/nextjs-starter-ts/components/smart-components/index.ts b/starters/nextjs-starter-ts/components/smart-components/index.ts index 6223e3ad..a2eeff94 100644 --- a/starters/nextjs-starter-ts/components/smart-components/index.ts +++ b/starters/nextjs-starter-ts/components/smart-components/index.ts @@ -3,6 +3,7 @@ import { SmartComponentMap, } from "@pantheon-systems/pcc-react-sdk/components"; import MediaPreview from "./media-preview"; +import { withSmartComponentErrorBoundary } from "./error-boundary"; export const serverSmartComponentMap = { MEDIA_PREVIEW: { @@ -23,7 +24,7 @@ export const serverSmartComponentMap = { export const clientSmartComponentMap: SmartComponentMap = { MEDIA_PREVIEW: { ...serverSmartComponentMap.MEDIA_PREVIEW, - reactComponent: MediaPreview, + reactComponent: withSmartComponentErrorBoundary(MediaPreview), }, }; diff --git a/starters/nextjs-starter/components/smart-components/error-boundary.jsx b/starters/nextjs-starter/components/smart-components/error-boundary.jsx new file mode 100644 index 00000000..e68f3003 --- /dev/null +++ b/starters/nextjs-starter/components/smart-components/error-boundary.jsx @@ -0,0 +1,41 @@ +import React, { Component, Suspense } from "react"; + +export class SmartComponentErrorBoundary extends Component { + state = { + hasError: false, + }; + + static getDerivedStateFromError() { + return { hasError: true }; + } + + render() { + if (this.state.hasError) { + return ( +
+ Something went wrong while rendering this smart component. +
+ ); + } + + return this.props.children; + } +} + +const SmartComponentSuspenseErrorBoundary = ({ children }) => { + return ( + + Loading...}>{children} + + ); +}; + +export const withSmartComponentErrorBoundary = + // eslint-disable-next-line react/display-name + (Component) => (props) => ( + + + + ); + +export default SmartComponentErrorBoundary; \ No newline at end of file diff --git a/starters/nextjs-starter/components/smart-components/index.jsx b/starters/nextjs-starter/components/smart-components/index.jsx index feaaf487..d87b560d 100644 --- a/starters/nextjs-starter/components/smart-components/index.jsx +++ b/starters/nextjs-starter/components/smart-components/index.jsx @@ -1,3 +1,4 @@ +import { withSmartComponentErrorBoundary } from "./error-boundary"; import MediaPreview from "./media-preview"; export const serverSmartComponentMap = { @@ -19,7 +20,7 @@ export const serverSmartComponentMap = { export const clientSmartComponentMap = { MEDIA_PREVIEW: { ...serverSmartComponentMap.MEDIA_PREVIEW, - reactComponent: MediaPreview, + reactComponent: withSmartComponentErrorBoundary(MediaPreview), }, };