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.

refactor: render builder only client side (#3165)

Bogdan Chadkin8dd492e70ae2
diff --git a/apps/builder/app/builder/builder.tsx b/apps/builder/app/builder/builder.tsx
index ed2e40d938..c573f8a72a 100644
--- a/apps/builder/app/builder/builder.tsx
+++ b/apps/builder/app/builder/builder.tsx
@@ -17,9 +17,6 @@ import {
} from "./features/sidebar-left";
import { Inspector } from "./features/inspector";
import { Topbar } from "./features/topbar";
-import builderStyles from "./builder.css?url";
-// eslint-disable-next-line import/no-internal-modules
-import prismStyles from "prismjs/themes/prism-solarizedlight.min.css?url";
import { Footer } from "./features/footer";
import {
CanvasIframe,
@@ -64,14 +61,6 @@ import { useToastErrors } from "~/shared/error/toast-error";
registerContainers();
-// Can cause FOUC because of remix-island, be very accurate adding anything here
-export const links = () => {
- return [
- { rel: "stylesheet", href: builderStyles },
- { rel: "stylesheet", href: prismStyles },
- ];
-};
-
const useSetWindowTitle = () => {
const project = useStore($project);
useEffect(() => {
diff --git a/apps/builder/app/builder/index.ts b/apps/builder/app/builder/index.client.ts
similarity index 100%
rename from apps/builder/app/builder/index.ts
rename to apps/builder/app/builder/index.client.ts
diff --git a/apps/builder/app/routes/builder.$projectId.tsx b/apps/builder/app/routes/builder.$projectId.tsx
index 9f520238df..b1080572b5 100644
--- a/apps/builder/app/routes/builder.$projectId.tsx
+++ b/apps/builder/app/routes/builder.$projectId.tsx
@@ -1,3 +1,4 @@
+import { lazy } from "react";
import {
useLoaderData,
useRouteError,
@@ -9,6 +10,7 @@ import {
redirect,
json,
} from "@remix-run/server-runtime";
+
import { loadBuildByProjectId } from "@webstudio-is/project-build/index.server";
import { db } from "@webstudio-is/project/index.server";
import { db as authDb } from "@webstudio-is/authorization-token/index.server";
@@ -21,11 +23,22 @@ import { loadAssetsByProject } from "@webstudio-is/asset-uploader/index.server";
import { createContext } from "~/shared/context.server";
import { ErrorMessage } from "~/shared/error";
import { loginPath } from "~/shared/router-utils";
-import { type BuilderProps, Builder, links } from "~/builder";
+import type { BuilderProps } from "~/builder/index.client";
import env from "~/env/env.server";
import { staticEnv } from "~/env/env.static.server";
-export { links };
+import builderStyles from "~/builder/builder.css?url";
+// eslint-disable-next-line import/no-internal-modules
+import prismStyles from "prismjs/themes/prism-solarizedlight.min.css?url";
+import { ClientOnly } from "~/shared/client-only";
+
+// Can cause FOUC because of remix-island, be very accurate adding anything here
+export const links = () => {
+ return [
+ { rel: "stylesheet", href: builderStyles },
+ { rel: "stylesheet", href: prismStyles },
+ ];
+};
export const loader = async ({
params,
@@ -141,10 +154,19 @@ export const ErrorBoundary = () => {
return <ErrorMessage message={message} />;
};
+const Builder = lazy(async () => {
+ const { Builder } = await import("~/builder/index.client");
+ return { default: Builder };
+});
+
export const BuilderRoute = () => {
const data = useLoaderData<BuilderProps>();
- return <Builder {...data} />;
+ return (
+ <ClientOnly>
+ <Builder {...data} />
+ </ClientOnly>
+ );
};
/**
diff --git a/apps/builder/app/shared/client-only.ts b/apps/builder/app/shared/client-only.ts
new file mode 100644
index 0000000000..4af28c9b3e
--- /dev/null
+++ b/apps/builder/app/shared/client-only.ts
@@ -0,0 +1,9 @@
+import { type ReactNode, useState, useEffect } from "react";
+
+export const ClientOnly = ({ children }: { children: ReactNode }) => {
+ const [rendered, setRendered] = useState(false);
+ useEffect(() => setRendered(true), []);
+ if (rendered) {
+ return children;
+ }
+};