Open source website builder and Webflow alternative. Webstudio is an advanced visual builder that connects to any headless CMS, supports all CSS properties, and can be hosted anywhere, including with us.
TypeScript accessibilityalternativecloudflarecloudflare-workersdesignfreefrontendopen-sourceradix-uireactremixweb-developmentwebflowwebsite-builderwebsite-developmentwebsiteswebstudio
fix: recover from canvas errors (#2809)
Bogdan Chadkine01000e7ad37
diff --git a/apps/builder/app/canvas/canvas.tsx b/apps/builder/app/canvas/canvas.tsxindex dbe1c6e633..21cf19d92f 100644--- a/apps/builder/app/canvas/canvas.tsx+++ b/apps/builder/app/canvas/canvas.tsx@@ -1,4 +1,5 @@import { useMemo, useEffect, useState } from "react";+import { ErrorBoundary, type FallbackProps } from "react-error-boundary";import { useStore } from "@nanostores/react";import type { Instances } from "@webstudio-is/sdk";import {@@ -18,8 +19,13 @@ import * as radixComponents from "@webstudio-is/sdk-components-react-radix";import * as radixComponentMetas from "@webstudio-is/sdk-components-react-radix/metas";import * as radixComponentPropsMetas from "@webstudio-is/sdk-components-react-radix/props";import { hooks as radixComponentHooks } from "@webstudio-is/sdk-components-react-radix/hooks";+import { ErrorMessage } from "~/shared/error";import { $publisher, publish } from "~/shared/pubsub";-import { registerContainers, useCanvasStore } from "~/shared/sync";+import {+ registerContainers,+ serverSyncStore,+ useCanvasStore,+} from "~/shared/sync";import { useManageDesignModeStyles, GlobalStyles } from "./shared/styles";import {WebstudioComponentCanvas,@@ -52,6 +58,19 @@ import { $params } from "./stores";registerContainers();+const FallbackComponent = ({ error, resetErrorBoundary }: FallbackProps) => {+ // try to recover from error when webstudio data is changed again+ useEffect(() => {+ return serverSyncStore.subscribe(resetErrorBoundary);+ }, [resetErrorBoundary]);+ return (+ // body is required to prevent breaking collapsed instances logic+ <body>+ <ErrorMessage message={error.message} />+ </body>+ );+};+const useElementsTree = (components: Components,instances: Instances,@@ -200,7 +219,10 @@ export const Canvas = ({return (<><GlobalStyles params={params} />- {elements}+ {/* catch all errors in rendered components */}+ <ErrorBoundary FallbackComponent={FallbackComponent}>+ {elements}+ </ErrorBoundary>{// Call hooks after render to ensure effects are last.// Helps improve outline calculations as all styles are then applied.diff --git a/apps/builder/package.json b/apps/builder/package.jsonindex a42099f021..850ef9faac 100644--- a/apps/builder/package.json+++ b/apps/builder/package.json@@ -104,6 +104,7 @@"react": "^18.2.0","react-color": "^2.19.3","react-dom": "^18.2.0",+ "react-error-boundary": "^4.0.12","react-hotkeys-hook": "^4.4.1","react-script-hook": "^1.7.2","react-use": "^17.3.2",diff --git a/pnpm-lock.yaml b/pnpm-lock.yamlindex 593a3e6236..0a3ac8e4e1 100644--- a/pnpm-lock.yaml+++ b/pnpm-lock.yaml@@ -313,6 +313,9 @@ importers:react-dom:specifier: ^18.2.0version: 18.2.0([email protected])+ react-error-boundary:+ specifier: ^4.0.12+ version: 4.0.12([email protected])react-hotkeys-hook:specifier: ^4.4.1version: 4.4.1([email protected])([email protected])@@ -15205,6 +15208,15 @@ packages:'@babel/runtime': 7.22.3react: 18.2.0+ resolution: {integrity: sha512-kJdxdEYlb7CPC1A0SeUY38cHpjuu6UkvzKiAmqmOFL21VRfMhOcWxTCBgLVCO0VEMh9JhFNcVaXlV4/BTpiwOA==}+ peerDependencies:+ react: '>=16.13.1'+ dependencies:+ '@babel/runtime': 7.22.3+ react: 18.2.0+ dev: false+resolution: {integrity: sha512-j8z+cQbWIM5LY37pR6uZR6D4LfseplqnuAO4co4u8917hBUvXlEqyP1ZzqVLcqoyUesZZv/ImreoCeHVDpE5pQ==}engines: {node: '>=10'}