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 hydration (#1621)

Ivan Starkovd14c07f190b6
diff --git a/apps/builder/app/auth/login.tsx b/apps/builder/app/auth/login.tsx
index 9160582839..3995e7c085 100644
--- a/apps/builder/app/auth/login.tsx
+++ b/apps/builder/app/auth/login.tsx
@@ -8,20 +8,11 @@ import {
theme,
} from "@webstudio-is/design-system";
import { GithubIcon, GoogleIcon, WebstudioIcon } from "@webstudio-is/icons";
-// eslint-disable-next-line import/no-internal-modules
-import interFont from "@fontsource/inter/variable.css";
-// eslint-disable-next-line import/no-internal-modules
-import manropeVariableFont from "@fontsource/manrope/variable.css";
import { LoginButton } from "./login-button";
import { Form } from "@remix-run/react";
import { authPath } from "~/shared/router-utils";
import { SecretLogin } from "./secret-login";
-export const links = () => [
- { rel: "stylesheet", href: interFont },
- { rel: "stylesheet", href: manropeVariableFont },
-];
-
const globalStyles = globalCss({
body: {
margin: 0,
@@ -42,10 +33,10 @@ const sidebarStyle = css({
flexBasis: "35%",
"@tablet": {
background: `
- radial-gradient(65.88% 47.48% at 50% 50%, #FFFFFF 0%, rgba(255, 255, 255, 0) 100%),
- linear-gradient(0deg, rgba(255, 255, 255, 0) 49.46%, rgba(255, 255, 255, 0.33) 100%),
- linear-gradient(180deg, rgba(255, 174, 60, 0) 0%, rgba(230, 60, 254, 0.33) 100%),
- radial-gradient(211.58% 161.63% at 3.13% 100%, rgba(255, 174, 60, 0.3) 0%, rgba(227, 53, 255, 0) 100%),
+ radial-gradient(65.88% 47.48% at 50% 50%, #FFFFFF 0%, rgba(255, 255, 255, 0) 100%),
+ linear-gradient(0deg, rgba(255, 255, 255, 0) 49.46%, rgba(255, 255, 255, 0.33) 100%),
+ linear-gradient(180deg, rgba(255, 174, 60, 0) 0%, rgba(230, 60, 254, 0.33) 100%),
+ radial-gradient(211.58% 161.63% at 3.13% 100%, rgba(255, 174, 60, 0.3) 0%, rgba(227, 53, 255, 0) 100%),
radial-gradient(107.1% 32.15% at 92.96% 5.04%, rgba(53, 255, 182, 0.2) 0%, rgba(74, 78, 250, 0.2) 100%), #EBFFFC;
`,
},
diff --git a/apps/builder/app/builder/builder.css b/apps/builder/app/builder/builder.css
index d3f1ac9457..5d3f249334 100644
--- a/apps/builder/app/builder/builder.css
+++ b/apps/builder/app/builder/builder.css
@@ -1,7 +1,4 @@
body {
- margin: 0;
overflow: hidden;
overscroll-behavior: contain;
- font-family: var(--fonts-sans);
- font-size: var(--fontSizes-1);
}
diff --git a/apps/builder/app/builder/builder.tsx b/apps/builder/app/builder/builder.tsx
index ee424028d6..2acc901263 100644
--- a/apps/builder/app/builder/builder.tsx
+++ b/apps/builder/app/builder/builder.tsx
@@ -6,10 +6,6 @@ import { theme, Box, type CSS, Flex, Grid } from "@webstudio-is/design-system";
import type { AuthPermit } from "@webstudio-is/trpc-interface/index.server";
import { registerContainers, useBuilderStore } from "~/shared/sync";
import { useSyncServer } from "./shared/sync/sync-server";
-// eslint-disable-next-line import/no-internal-modules
-import interFont from "@fontsource/inter/variable.css";
-// eslint-disable-next-line import/no-internal-modules
-import robotoMonoFont from "@fontsource/roboto-mono/index.css";
import { useSharedShortcuts } from "~/shared/shortcuts";
import { SidebarLeft, Navigator } from "./features/sidebar-left";
import { Inspector } from "./features/inspector";
@@ -58,12 +54,9 @@ import {
registerContainers();
+// Can cause FOUC because of remix-island, be very accurate adding anything here
export const links = () => {
- return [
- { rel: "stylesheet", href: interFont },
- { rel: "stylesheet", href: robotoMonoFont },
- { rel: "stylesheet", href: builderStyles },
- ];
+ return [{ rel: "stylesheet", href: builderStyles }];
};
const useSetProject = (project: Project) => {
diff --git a/apps/builder/app/dashboard/dashboard.tsx b/apps/builder/app/dashboard/dashboard.tsx
index 5602f7f6c4..5ab464cc77 100644
--- a/apps/builder/app/dashboard/dashboard.tsx
+++ b/apps/builder/app/dashboard/dashboard.tsx
@@ -1,18 +1,9 @@
import { Flex, globalCss, theme } from "@webstudio-is/design-system";
import { Header } from "./header";
-// eslint-disable-next-line import/no-internal-modules
-import interFont from "@fontsource/inter/variable.css";
-// eslint-disable-next-line import/no-internal-modules
-import manropeVariableFont from "@fontsource/manrope/variable.css";
import { Projects } from "./projects";
import type { User } from "~/shared/db/user.server";
import type { DashboardProject } from "@webstudio-is/dashboard";
-export const links = () => [
- { rel: "stylesheet", href: interFont },
- { rel: "stylesheet", href: manropeVariableFont },
-];
-
const globalStyles = globalCss({
body: {
margin: 0,
diff --git a/apps/builder/app/entry.client.tsx b/apps/builder/app/entry.client.tsx
index 2147ee7ffe..9ba4bc8208 100644
--- a/apps/builder/app/entry.client.tsx
+++ b/apps/builder/app/entry.client.tsx
@@ -1,6 +1,6 @@
import { RemixBrowser, useLocation, useMatches } from "@remix-run/react";
import { startTransition, StrictMode, useEffect } from "react";
-import { hydrateRoot } from "react-dom/client";
+import { createRoot, hydrateRoot } from "react-dom/client";
import { BrowserTracing, remixRouterInstrumentation } from "@sentry/remix";
import { initSentry } from "./shared/sentry";
import env from "./shared/env";
@@ -34,8 +34,21 @@ try {
}
startTransition(() => {
- hydrateRoot(
- document,
+ if (document.documentElement.dataset.builder === "true") {
+ hydrateRoot(
+ // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
+ document.getElementById("root")!,
+ <StrictMode>
+ <RemixBrowser />
+ </StrictMode>
+ );
+ return;
+ }
+
+ // Do not hydrate canvas, to avoid hydration Errors in case of 3rd party plugins.
+ // Just render it from scratch. (We don't have data on the Canvas at the server so no FOUC is possible)
+ const root = createRoot(document as never);
+ root.render(
<StrictMode>
<RemixBrowser />
</StrictMode>
diff --git a/apps/builder/app/entry.server.tsx b/apps/builder/app/entry.server.tsx
index 3b0f585185..9ab017405d 100644
--- a/apps/builder/app/entry.server.tsx
+++ b/apps/builder/app/entry.server.tsx
@@ -41,8 +41,7 @@ export default function handleRequest(
// and separate request handler will not be necessary anymore
if (
remixContext.staticHandlerContext.matches.some(
- (match) =>
- match.route.id === "routes/$" || match.route.id === "routes/index"
+ (match) => match.route.id === "routes/_index"
) &&
getBuildParams(request) !== undefined
) {
diff --git a/apps/builder/app/routes/dashboard._index.tsx b/apps/builder/app/routes/dashboard._index.tsx
index 609e96c6e6..c3cd31f5c8 100644
--- a/apps/builder/app/routes/dashboard._index.tsx
+++ b/apps/builder/app/routes/dashboard._index.tsx
@@ -10,8 +10,6 @@ import { sentryException } from "~/shared/sentry";
import { ErrorMessage } from "~/shared/error";
import { createContext } from "~/shared/context.server";
-export { links } from "~/dashboard";
-
export const loader = async ({
request,
}: LoaderArgs): Promise<ComponentProps<typeof Dashboard>> => {
diff --git a/apps/builder/app/routes/login._index.tsx b/apps/builder/app/routes/login._index.tsx
index a8de799daf..7c0a61a669 100644
--- a/apps/builder/app/routes/login._index.tsx
+++ b/apps/builder/app/routes/login._index.tsx
@@ -8,13 +8,11 @@ import {
import { useLoaderData } from "@remix-run/react";
import { findAuthenticatedUser } from "~/services/auth.server";
import env from "~/env/env.server";
-import { Login, links } from "~/auth";
+import { Login } from "~/auth";
import { useLoginErrorMessage } from "~/shared/session";
import { dashboardPath } from "~/shared/router-utils";
import { returnToCookie } from "~/services/cookie.server";
-export { links };
-
const comparePathnames = (pathnameOrUrlA: string, pathnameOrUrlB: string) => {
const aPathname = new URL(pathnameOrUrlA, "http://localhost").pathname;
const bPathname = new URL(pathnameOrUrlB, "http://localhost").pathname;
diff --git a/apps/builder/app/shared/app.css b/apps/builder/app/shared/app.css
new file mode 100644
index 0000000000..2a1488ce39
--- /dev/null
+++ b/apps/builder/app/shared/app.css
@@ -0,0 +1,5 @@
+body {
+ margin: 0;
+ font-family: var(--fonts-sans);
+ font-size: var(--fontSizes-1);
+}
diff --git a/apps/builder/app/shared/remix/handle-request.server.tsx b/apps/builder/app/shared/remix/handle-request.server.tsx
index 9b72cb7cc7..d33c6d2f2a 100644
--- a/apps/builder/app/shared/remix/handle-request.server.tsx
+++ b/apps/builder/app/shared/remix/handle-request.server.tsx
@@ -1,7 +1,16 @@
import { renderToString } from "react-dom/server";
import { RemixServer } from "@remix-run/react";
import type { EntryContext } from "@remix-run/node";
-import { insertCriticalCss } from "./insert-critical-css";
+import { flushCss } from "@webstudio-is/design-system";
+import { renderHeadToString } from "remix-island";
+import { Head } from "./root";
+// eslint-disable-next-line import/no-internal-modules
+import interFont from "@fontsource/inter/variable.css";
+// eslint-disable-next-line import/no-internal-modules
+import manropeVariableFont from "@fontsource/manrope/variable.css";
+// eslint-disable-next-line import/no-internal-modules
+import robotoMonoFont from "@fontsource/roboto-mono/index.css";
+import appCss from "../app.css";
export const handleRequest = (
request: Request,
@@ -13,10 +22,33 @@ export const handleRequest = (
<RemixServer context={remixContext} url={request.url} />
);
+ const head = renderHeadToString({ request, remixContext, Head });
+
responseHeaders.set("Content-Type", "text/html");
responseHeaders.set("Accept-CH", "Sec-CH-Prefers-Color-Scheme");
- return new Response("<!DOCTYPE html>" + insertCriticalCss(markup), {
+ // data-builder="true" to distinguish between builder and canvas at the entry.client.tsx
+ // inject fonts and base body styles here instead of `export links = ` to avoid FOUC
+ const body = `
+ <!DOCTYPE html>
+ <html lang="en" data-builder="true">
+ <head>
+ <meta charSet="utf-8" />
+ <meta name="viewport" content="width=device-width,initial-scale=1" />
+ <link rel="stylesheet" href="${interFont}" />
+ <link rel="stylesheet" href="${manropeVariableFont}" />
+ <link rel="stylesheet" href="${robotoMonoFont}" />
+ <link rel="stylesheet" href="${appCss}" />
+ ${head}
+ <style id="stitches">${flushCss()}</style>
+ </head>
+ <body>
+ <div id="root">${markup}</div>
+ </body>
+ </html>
+ `;
+
+ return new Response(body, {
status: responseStatusCode,
headers: responseHeaders,
});
diff --git a/apps/builder/app/shared/remix/root.tsx b/apps/builder/app/shared/remix/root.tsx
index 134d8b3faa..8159943ac7 100644
--- a/apps/builder/app/shared/remix/root.tsx
+++ b/apps/builder/app/shared/remix/root.tsx
@@ -1,30 +1,31 @@
-import { Links, LiveReload, Meta, Outlet, Scripts } from "@remix-run/react";
+import {
+ Links,
+ LiveReload,
+ Meta,
+ Outlet as RemixOutlet,
+ Scripts,
+} from "@remix-run/react";
import { Env } from "~/shared/env";
-import { useThemeProps } from "~/shared/theme";
-import { CRITICAL_CSS_MARKER } from "./constants";
import env from "~/env/env.server";
+import { createHead } from "remix-island";
-export const Root = () => {
- const themeProps = useThemeProps();
+export const Head = createHead(() => (
+ <>
+ <Meta />
+ <Links />
+ </>
+));
+
+export const Root = ({ Outlet = RemixOutlet }) => {
return (
- <html lang="en" {...themeProps}>
- <head>
- <meta charSet="utf-8" />
- <meta name="viewport" content="width=device-width,initial-scale=1" />
- <Meta />
- <Links />
- {typeof document === "undefined" ? CRITICAL_CSS_MARKER : null}
- </head>
- <body>
- <Outlet />
- <Env />
- <Scripts />
- {process.env.NODE_ENV === "development" && (
- <LiveReload
- port={env.BUILD_ORIGIN !== undefined ? 3010 : undefined}
- />
- )}
- </body>
- </html>
+ <>
+ <Head />
+ <Outlet />
+ <Env />
+ <Scripts />
+ {process.env.NODE_ENV === "development" && (
+ <LiveReload port={env.BUILD_ORIGIN !== undefined ? 3010 : undefined} />
+ )}
+ </>
);
};
diff --git a/apps/builder/package.json b/apps/builder/package.json
index a1834c4639..334808c233 100644
--- a/apps/builder/package.json
+++ b/apps/builder/package.json
@@ -84,6 +84,7 @@
"remix-auth-form": "^1.1.2",
"remix-auth-github": "^1.1.1",
"remix-auth-google": "^1.1.0",
+ "remix-island": "^0.1.2",
"shallow-equal": "^3.1.0",
"slugify": "^1.6.5",
"title-case": "^3.0.3",
diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml
index 1b6797677a..c768bfb076 100644
--- a/pnpm-lock.yaml
+++ b/pnpm-lock.yaml
@@ -248,6 +248,9 @@ importers:
remix-auth-google:
specifier: ^1.1.0
+ remix-island:
+ specifier: ^0.1.2
shallow-equal:
specifier: ^3.1.0
version: 3.1.0
@@ -17731,6 +17734,20 @@ packages:
uuid: 8.3.2
dev: false
+ resolution: {integrity: sha512-1vXF56y7ZKb8caUduCKdkbxObAxZnTO+arQ94QlwKK6a7wVozGCfiOOk3lr919rm7cy54eeqOVI0hud3cfHlLg==}
+ peerDependencies:
+ '@remix-run/react': '>= 1'
+ '@remix-run/server-runtime': '>= 1'
+ react: '>= 16.8'
+ react-dom: '>= 16.8'
+ dependencies:
+ '@remix-run/react': 1.15.0([email protected])([email protected])
+ '@remix-run/server-runtime': 1.15.0
+ react: 18.2.0
+ react-dom: 18.2.0([email protected])
+ dev: false
+
resolution: {integrity: sha512-7pXIJqJOq5tFgG1A2Zxti3Ht8jJF337m4sowbuHsW30ZnkQFnDzy9qBNhgzX8ZLW4+UBcXiiR7SwR6pokHsxiA==}
dev: false