Skip to content

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.

fix: render canvas only when data is loaded (#3496)

Bogdan Chadkin0afff55afbe2
diff --git a/apps/builder/app/builder/builder.tsx b/apps/builder/app/builder/builder.tsx
index 0c123b196c..e5e792d87c 100644
--- a/apps/builder/app/builder/builder.tsx
+++ b/apps/builder/app/builder/builder.tsx
@@ -1,4 +1,4 @@
-import { useCallback, useEffect, type ReactNode } from "react";
+import { useCallback, useEffect, useState, type ReactNode } from "react";
import { useStore } from "@nanostores/react";
import { TooltipProvider } from "@radix-ui/react-tooltip";
import { usePublish, $publisher } from "~/shared/pubsub";
@@ -228,6 +228,7 @@ export const Builder = ({
userPlanFeatures,
authTokenPermissions,
}: BuilderProps) => {
+ const [isDataLoaded, setIsDataLoaded] = useState(false);
useMount(() => {
// additional data stores
$project.set(project);
@@ -249,6 +250,10 @@ export const Builder = ({
authPermit,
authToken,
});
+ // render canvas only after all data is loaded
+ // so builder is started listening for connect event
+ // when canvas is rendered
+ setIsDataLoaded(true);
})
.catch(() => {});
return () => {
@@ -347,16 +352,18 @@ export const Builder = ({
/>
<Main>
<Workspace onTransitionEnd={onTransitionEnd}>
- <CanvasIframe
- ref={iframeRefCallback}
- src={canvasUrl}
- title={project.title}
- css={{
- height: "100%",
- width: "100%",
- backgroundColor: "#fff",
- }}
- />
+ {isDataLoaded && (
+ <CanvasIframe
+ ref={iframeRefCallback}
+ src={canvasUrl}
+ title={project.title}
+ css={{
+ height: "100%",
+ width: "100%",
+ backgroundColor: "#fff",
+ }}
+ />
+ )}
</Workspace>
<AiCommandBar isPreviewMode={isPreviewMode} />
</Main>
diff --git a/apps/builder/app/builder/features/workspace/canvas-iframe.tsx b/apps/builder/app/builder/features/workspace/canvas-iframe.tsx
index 2bcb86f576..59e098408b 100644
--- a/apps/builder/app/builder/features/workspace/canvas-iframe.tsx
+++ b/apps/builder/app/builder/features/workspace/canvas-iframe.tsx
@@ -1,4 +1,4 @@
-import { forwardRef, useEffect, useState } from "react";
+import { forwardRef } from "react";
import {
type CSS,
css,
@@ -16,17 +16,7 @@ type CanvasIframeProps = {
export const CanvasIframe = forwardRef<HTMLIFrameElement, CanvasIframeProps>(
({ css, ...rest }, ref) => {
- // initialize canvas after builder is rendered
- // and synchronizatio is initialized
- const [isInitialized, setInitialized] = useState(false);
- useEffect(() => {
- setInitialized(true);
- }, []);
- return (
- isInitialized && (
- <iframe {...rest} ref={ref} className={iframeStyle({ css })} />
- )
- );
+ return <iframe {...rest} ref={ref} className={iframeStyle({ css })} />;
}
);
diff --git a/apps/builder/app/builder/features/workspace/workspace.tsx b/apps/builder/app/builder/features/workspace/workspace.tsx
index 09630138dd..6fad4a4439 100644
--- a/apps/builder/app/builder/features/workspace/workspace.tsx
+++ b/apps/builder/app/builder/features/workspace/workspace.tsx
@@ -1,4 +1,4 @@
-import { useEffect, useRef } from "react";
+import { useEffect, useRef, type ReactNode } from "react";
import { useStore } from "@nanostores/react";
import { theme, Toaster, css } from "@webstudio-is/design-system";
import {
@@ -91,7 +91,7 @@ const useOutlineStyle = () => {
};
type WorkspaceProps = {
- children: JSX.Element;
+ children: ReactNode;
onTransitionEnd: () => void;
};