UNPKG

next

Version:

The React Framework

86 lines (84 loc) 2.71 kB
'use client'; "use strict"; Object.defineProperty(exports, "__esModule", { value: true }); 0 && (module.exports = { GracefulDegradeBoundary: null, default: null }); function _export(target, all) { for(var name in all)Object.defineProperty(target, name, { enumerable: true, get: all[name] }); } _export(exports, { GracefulDegradeBoundary: function() { return GracefulDegradeBoundary; }, default: function() { return _default; } }); const _jsxruntime = require("react/jsx-runtime"); const _react = require("react"); function getDomNodeAttributes(node) { const result = {}; for(let i = 0; i < node.attributes.length; i++){ const attr = node.attributes[i]; result[attr.name] = attr.value; } return result; } class GracefulDegradeBoundary extends _react.Component { constructor(props){ super(props); this.state = { hasError: false }; this.rootHtml = ''; this.htmlAttributes = {}; this.htmlRef = /*#__PURE__*/ (0, _react.createRef)(); } static getDerivedStateFromError(_) { return { hasError: true }; } componentDidMount() { const htmlNode = this.htmlRef.current; if (this.state.hasError && htmlNode) { // Reapply the cached HTML attributes to the root element Object.entries(this.htmlAttributes).forEach(([key, value])=>{ htmlNode.setAttribute(key, value); }); } } render() { const { hasError } = this.state; // Cache the root HTML content on the first render if (typeof window !== 'undefined' && !this.rootHtml) { this.rootHtml = document.documentElement.innerHTML; this.htmlAttributes = getDomNodeAttributes(document.documentElement); } if (hasError) { // Render the current HTML content without hydration return /*#__PURE__*/ (0, _jsxruntime.jsx)("html", { ref: this.htmlRef, suppressHydrationWarning: true, dangerouslySetInnerHTML: { __html: this.rootHtml } }); } return this.props.children; } } const _default = GracefulDegradeBoundary; 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=graceful-degrade-boundary.js.map