UNPKG

next

Version:

The React Framework

134 lines (132 loc) 5.48 kB
'use client'; "use strict"; Object.defineProperty(exports, "__esModule", { value: true }); 0 && (module.exports = { ErrorBoundary: null, ErrorBoundaryHandler: null }); function _export(target, all) { for(var name in all)Object.defineProperty(target, name, { enumerable: true, get: all[name] }); } _export(exports, { ErrorBoundary: function() { return ErrorBoundary; }, ErrorBoundaryHandler: function() { return ErrorBoundaryHandler; } }); const _interop_require_default = require("@swc/helpers/_/_interop_require_default"); const _jsxruntime = require("react/jsx-runtime"); const _react = /*#__PURE__*/ _interop_require_default._(require("react")); const _navigationuntracked = require("./navigation-untracked"); const _isnextroutererror = require("./is-next-router-error"); const _navfailurehandler = require("./nav-failure-handler"); const _handleisrerror = require("./handle-isr-error"); const _isbot = require("../../shared/lib/router/utils/is-bot"); const isBotUserAgent = typeof window !== 'undefined' && (0, _isbot.isBot)(window.navigator.userAgent); class ErrorBoundaryHandler extends _react.default.Component { constructor(props){ super(props), this.reset = ()=>{ this.setState({ error: null }); }; this.state = { error: null, previousPathname: this.props.pathname }; } static getDerivedStateFromError(error) { if ((0, _isnextroutererror.isNextRouterError)(error)) { // Re-throw if an expected internal Next.js router error occurs // this means it should be handled by a different boundary (such as a NotFound boundary in a parent segment) throw error; } return { error }; } static getDerivedStateFromProps(props, state) { const { error } = state; // if we encounter an error while // a navigation is pending we shouldn't render // the error boundary and instead should fallback // to a hard navigation to attempt recovering if (process.env.__NEXT_APP_NAV_FAIL_HANDLING) { if (error && (0, _navfailurehandler.handleHardNavError)(error)) { // clear error so we don't render anything return { error: null, previousPathname: props.pathname }; } } /** * 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 }; } // Explicit type is needed to avoid the generated `.d.ts` having a wide return type that could be specific to the `@types/react` version. render() { //When it's bot request, segment level error boundary will keep rendering the children, // the final error will be caught by the root error boundary and determine wether need to apply graceful degrade. if (this.state.error && !isBotUserAgent) { return /*#__PURE__*/ (0, _jsxruntime.jsxs)(_jsxruntime.Fragment, { children: [ /*#__PURE__*/ (0, _jsxruntime.jsx)(_handleisrerror.HandleISRError, { error: this.state.error }), this.props.errorStyles, this.props.errorScripts, /*#__PURE__*/ (0, _jsxruntime.jsx)(this.props.errorComponent, { error: this.state.error, reset: this.reset }) ] }); } return this.props.children; } } function ErrorBoundary({ errorComponent, errorStyles, errorScripts, children }) { // When we're rendering the missing params shell, this will return null. This // is because we won't be rendering any not found boundaries or error // boundaries for the missing params shell. When this runs on the client // (where these errors can occur), we will get the correct pathname. const pathname = (0, _navigationuntracked.useUntrackedPathname)(); if (errorComponent) { return /*#__PURE__*/ (0, _jsxruntime.jsx)(ErrorBoundaryHandler, { pathname: pathname, errorComponent: errorComponent, errorStyles: errorStyles, errorScripts: errorScripts, children: children }); } return /*#__PURE__*/ (0, _jsxruntime.jsx)(_jsxruntime.Fragment, { children: children }); } if ((typeof exports.default === 'function' || (typeof exports.default === 'object' && exports.default !== null)) && typeof exports.default.__esModule === 'undefined') { Object.defineProperty(exports.default, '__esModule', { value: true }); Object.assign(exports.default, exports); module.exports = exports.default; } //# sourceMappingURL=error-boundary.js.map