@progress/sitefinity-nextjs-sdk
Version:
Provides OOB widgets developed using the Next.js framework, which includes an abstraction layer for Sitefinity communication. Additionally, it offers an expanded API, typings, and tools for further development and integration.
54 lines (53 loc) • 2.13 kB
JavaScript
'use client'; // Error components must be Client Components
import { Fragment as _Fragment, jsx as _jsx } from "react/jsx-runtime";
import React from 'react';
import { usePathname } from 'next/navigation';
import { WidgetExecutionError } from '../widgets/error/widget-execution-error-component';
class ErrorBoundaryCustomHandler extends React.Component {
constructor(props) {
super(props);
this.state = { error: null, previousPathname: this.props.pathname };
}
static getDerivedStateFromError(error) {
return { error };
}
static getDerivedStateFromProps(props, state) {
/**
* Handles reset of the error boundary when a navigation happens.
* Ensures the error boundary does not stay enabled when navigating to a new page.
* Approach of setState in render is safe as it checks the previous pathname and then overrides
* it as outlined in https://react.dev/reference/react/useState#storing-information-from-previous-renders
*/
if (props.pathname !== state.previousPathname && state.error) {
return {
error: null,
previousPathname: props.pathname
};
}
return {
error: state.error,
previousPathname: props.pathname
};
}
reset = () => {
this.setState({ error: null });
};
render() {
if (this.state.error) {
if (this.props.context.requestContext.isEdit) {
const errorProps = {
context: this.props.context,
error: this.state.error.message + '\n' + this.state.error.stack
};
const element = React.createElement(WidgetExecutionError, errorProps);
return (element);
}
return (_jsx(_Fragment, {}));
}
return this.props.children;
}
}
export function ErrorBoundaryCustom({ children, context }) {
const pathname = usePathname();
return (_jsx(ErrorBoundaryCustomHandler, { context: context, pathname: pathname, children: children }));
}