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
separate critical css and remix stuff on canvas and in designer
Oleg Isonenc681299fdd12
diff --git a/apps/designer/app/critical-css.ts b/apps/designer/app/critical-css.tsdeleted file mode 100644index 28145afd71..0000000000--- a/apps/designer/app/critical-css.ts+++ /dev/null@@ -1,34 +0,0 @@-import {- insertCriticalCss as insert,- flushCss as flushCanvasCss,-} from "@webstudio-is/react-sdk";-import { flushCss as flushDesignerCss } from "@webstudio-is/design-system";-import config from "./config";--const flushFunctions = {- [config.previewPath]: flushCanvasCss,- [config.canvasPath]: flushCanvasCss,- [config.designerPath]: flushDesignerCss,- [config.dashboardPath]: flushDesignerCss,- [config.loginPath]: flushDesignerCss,-};--const getFlushFunction = (url: string) => {- const { pathname } = new URL(url);- let path: keyof typeof flushFunctions;- for (path in flushFunctions) {- if (pathname.indexOf(path) === 0) {- return flushFunctions[path];- }- }- return flushCanvasCss;-};--/**- * This works around the problem with getCssTextfunction being a different one- * depending on createStitches() used in each outlet.- * @todo find a better way- */-export const insertCriticalCss = (markup: string, url: string): string => {- return insert(markup, getFlushFunction(url));-};diff --git a/apps/designer/app/entry.server.tsx b/apps/designer/app/entry.server.tsxindex 0d2bdf0cc2..2d217876da 100644--- a/apps/designer/app/entry.server.tsx+++ b/apps/designer/app/entry.server.tsx@@ -1,10 +1,10 @@-import { renderToString } from "react-dom/server";-import { RemixServer } from "@remix-run/react";-import { insertCriticalCss } from "./critical-css";import type { EntryContext } from "@remix-run/node";import * as Sentry from "@sentry/remix";import { initSentry } from "./shared/sentry";import { prisma } from "@webstudio-is/prisma-client";+import { handleRequest as handleRequestDesigner } from "./shared/remix";+import { handleRequest as handleRequestCanvas } from "@webstudio-is/react-sdk";+import config from "./config";initSentry({integrations: [new Sentry.Integrations.Prisma({ client: prisma })],@@ -16,17 +16,15 @@ export default function handleRequest(responseHeaders: Headers,remixContext: EntryContext) {- let markup = renderToString(- <RemixServer context={remixContext} url={request.url} />- );+ const { pathname } = new URL(request.url);+ let handle = handleRequestDesigner;- markup = insertCriticalCss(markup, request.url);+ if (+ pathname.indexOf(config.previewPath) === 0 ||+ pathname.indexOf(config.canvasPath) === 0+ ) {+ handle = handleRequestCanvas;+ }- responseHeaders.set("Content-Type", "text/html");- responseHeaders.set("Accept-CH", "Sec-CH-Prefers-Color-Scheme");-- return new Response("<!DOCTYPE html>" + markup, {- status: responseStatusCode,- headers: responseHeaders,- });+ return handle(request, responseStatusCode, responseHeaders, remixContext);}diff --git a/apps/designer/app/routes/dashboard.tsx b/apps/designer/app/routes/dashboard.tsxindex 20bd4e9ce0..4a0cb028ab 100644--- a/apps/designer/app/routes/dashboard.tsx+++ b/apps/designer/app/routes/dashboard.tsx@@ -1,5 +1,5 @@import type { LoaderFunction, MetaFunction } from "@remix-run/node";-import { Designer } from "~/shared/documents/designer";+import { Root } from "~/shared/remix";import env from "~/env.server";import { getThemeData } from "~/shared/theme";@@ -14,4 +14,4 @@ export const meta: MetaFunction = () => {return { title: "Webstudio Dashboard" };};-export default Designer;+export default Root;diff --git a/apps/designer/app/routes/designer.tsx b/apps/designer/app/routes/designer.tsxindex 7097e1b5cf..44e8680655 100644--- a/apps/designer/app/routes/designer.tsx+++ b/apps/designer/app/routes/designer.tsx@@ -1,5 +1,5 @@import type { LoaderFunction, MetaFunction } from "@remix-run/node";-import { Designer } from "~/shared/documents/designer";+import { Root } from "~/shared/remix";import env from "~/env.server";import { getThemeData } from "~/shared/theme";@@ -14,4 +14,4 @@ export const meta: MetaFunction = () => {return { title: "Webstudio" };};-export default Designer;+export default Root;diff --git a/apps/designer/app/routes/login.tsx b/apps/designer/app/routes/login.tsxindex 99d9febb01..a76cc559f8 100644--- a/apps/designer/app/routes/login.tsx+++ b/apps/designer/app/routes/login.tsx@@ -1,5 +1,5 @@import { LoaderFunction, MetaFunction } from "@remix-run/node";-import { Designer } from "~/shared/documents/designer";+import { Root } from "~/shared/remix";import env from "~/env.server";import { getThemeData } from "~/shared/theme";@@ -14,4 +14,4 @@ export const meta: MetaFunction = () => {return { title: "Webstudio Login" };};-export default Designer;+export default Root;diff --git a/apps/designer/app/shared/remix/constants.ts b/apps/designer/app/shared/remix/constants.tsnew file mode 100644index 0000000000..b5ddbd71f5--- /dev/null+++ b/apps/designer/app/shared/remix/constants.ts@@ -0,0 +1 @@+export const CRITICAL_CSS_MARKER = "__CRITICAL_CSS__";diff --git a/apps/designer/app/shared/remix/handle-request.server.tsx b/apps/designer/app/shared/remix/handle-request.server.tsxnew file mode 100644index 0000000000..9b72cb7cc7--- /dev/null+++ b/apps/designer/app/shared/remix/handle-request.server.tsx@@ -0,0 +1,23 @@+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";++export const handleRequest = (+ request: Request,+ responseStatusCode: number,+ responseHeaders: Headers,+ remixContext: EntryContext+) => {+ const markup = renderToString(+ <RemixServer context={remixContext} url={request.url} />+ );++ responseHeaders.set("Content-Type", "text/html");+ responseHeaders.set("Accept-CH", "Sec-CH-Prefers-Color-Scheme");++ return new Response("<!DOCTYPE html>" + insertCriticalCss(markup), {+ status: responseStatusCode,+ headers: responseHeaders,+ });+};diff --git a/apps/designer/app/shared/remix/index.ts b/apps/designer/app/shared/remix/index.tsnew file mode 100644index 0000000000..444dfb6c40--- /dev/null+++ b/apps/designer/app/shared/remix/index.ts@@ -0,0 +1,2 @@+export * from "./handle-request.server";+export * from "./root";diff --git a/apps/designer/app/shared/remix/insert-critical-css.ts b/apps/designer/app/shared/remix/insert-critical-css.tsnew file mode 100644index 0000000000..fda5504048--- /dev/null+++ b/apps/designer/app/shared/remix/insert-critical-css.ts@@ -0,0 +1,6 @@+import { flushCss } from "@webstudio-is/design-system";+import { CRITICAL_CSS_MARKER } from "./constants";++export const insertCriticalCss = (markup: string): string => {+ return markup.replace(CRITICAL_CSS_MARKER, `<style>${flushCss()}</style>`);+};diff --git a/apps/designer/app/shared/documents/designer.tsx b/apps/designer/app/shared/remix/root.tsxsimilarity index 79%rename from apps/designer/app/shared/documents/designer.tsxrename to apps/designer/app/shared/remix/root.tsxindex 4638c4919a..38a1e662c6 100644--- a/apps/designer/app/shared/documents/designer.tsx+++ b/apps/designer/app/shared/remix/root.tsx@@ -1,9 +1,9 @@import { Links, LiveReload, Meta, Outlet, Scripts } from "@remix-run/react";-import { CriticalCss } from "@webstudio-is/react-sdk";import { Env } from "~/shared/env";import { useThemeProps } from "~/shared/theme";+import { CRITICAL_CSS_MARKER } from "./constants";-export const Designer = () => {+export const Root = () => {const themeProps = useThemeProps();return (<html lang="en" {...themeProps}>@@ -12,7 +12,7 @@ export const Designer = () => {<meta name="viewport" content="width=device-width,initial-scale=1" /><Meta /><Links />- <CriticalCss />+ {typeof document === "undefined" ? CRITICAL_CSS_MARKER : null}</head><body><Outlet />diff --git a/packages/react-sdk/src/canvas/index.ts b/packages/react-sdk/src/canvas/index.tsdeleted file mode 100644index df456a3704..0000000000--- a/packages/react-sdk/src/canvas/index.ts+++ /dev/null@@ -1 +0,0 @@-export * from "./canvas";diff --git a/packages/react-sdk/src/index.ts b/packages/react-sdk/src/index.tsindex 58ede255ac..7458f83c4d 100644--- a/packages/react-sdk/src/index.ts+++ b/packages/react-sdk/src/index.ts@@ -6,4 +6,4 @@ export * as componentsMeta from "./components/meta";export * from "./user-props";export * from "./pubsub";export * from "./db";-export * from "./canvas";+export * from "./remix";diff --git a/packages/react-sdk/src/remix/constants.ts b/packages/react-sdk/src/remix/constants.tsnew file mode 100644index 0000000000..b5ddbd71f5--- /dev/null+++ b/packages/react-sdk/src/remix/constants.ts@@ -0,0 +1 @@+export const CRITICAL_CSS_MARKER = "__CRITICAL_CSS__";diff --git a/packages/react-sdk/src/remix/handle-request.server.tsx b/packages/react-sdk/src/remix/handle-request.server.tsxnew file mode 100644index 0000000000..8efe2c9943--- /dev/null+++ b/packages/react-sdk/src/remix/handle-request.server.tsx@@ -0,0 +1,22 @@+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";++export const handleRequest = (+ request: Request,+ responseStatusCode: number,+ responseHeaders: Headers,+ remixContext: EntryContext+) => {+ const markup = renderToString(+ <RemixServer context={remixContext} url={request.url} />+ );++ responseHeaders.set("Content-Type", "text/html");++ return new Response("<!DOCTYPE html>" + insertCriticalCss(markup), {+ status: responseStatusCode,+ headers: responseHeaders,+ });+};diff --git a/packages/react-sdk/src/remix/index.ts b/packages/react-sdk/src/remix/index.tsnew file mode 100644index 0000000000..1287530143--- /dev/null+++ b/packages/react-sdk/src/remix/index.ts@@ -0,0 +1,2 @@+export * from "./root";+export * from "./handle-request.server";diff --git a/packages/react-sdk/src/remix/insert-critical-css.ts b/packages/react-sdk/src/remix/insert-critical-css.tsnew file mode 100644index 0000000000..9f0bdd20ca--- /dev/null+++ b/packages/react-sdk/src/remix/insert-critical-css.ts@@ -0,0 +1,6 @@+import { flushCss } from "../stitches";+import { CRITICAL_CSS_MARKER } from "./constants";++export const insertCriticalCss = (markup: string): string => {+ return markup.replace(CRITICAL_CSS_MARKER, `<style>${flushCss()}</style>`);+};diff --git a/packages/react-sdk/src/canvas/canvas.tsx b/packages/react-sdk/src/remix/root.tsxsimilarity index 84%rename from packages/react-sdk/src/canvas/canvas.tsxrename to packages/react-sdk/src/remix/root.tsxindex b20e8fb98e..eced0b5951 100644--- a/packages/react-sdk/src/canvas/canvas.tsx+++ b/packages/react-sdk/src/remix/root.tsx@@ -1,5 +1,5 @@import { Links, Meta, Outlet as DefaultOutlet } from "@remix-run/react";-import { CriticalCss } from "..";+import { CRITICAL_CSS_MARKER } from "./constants";/*** We are using Outlet prop from index layout when user renders site from a subdomain.@@ -18,7 +18,7 @@ export const Root = ({<link rel="shortcut icon" href="/favicon.ico" type="image/x-icon" /><Meta /><Links />- <CriticalCss />+ {typeof document === "undefined" ? CRITICAL_CSS_MARKER : null}</head><Outlet /></html>diff --git a/packages/react-sdk/src/stitches/critical-css.tsx b/packages/react-sdk/src/stitches/critical-css.tsxdeleted file mode 100644index 77f6268bae..0000000000--- a/packages/react-sdk/src/stitches/critical-css.tsx+++ /dev/null@@ -1,14 +0,0 @@-import React from "react";-import { flushCss as flushCssDefault } from "./css";--const criticalCssMarker = "__critical-css__";--export const CriticalCss = (): JSX.Element | null =>- typeof document === "undefined" ? <>{criticalCssMarker}</> : null;--export const insertCriticalCss = (- markup: string,- flushCss: typeof flushCssDefault = flushCssDefault-): string => {- return markup.replace(criticalCssMarker, `<style>${flushCss()}</style>`);-};diff --git a/packages/react-sdk/src/stitches/index.ts b/packages/react-sdk/src/stitches/index.tsindex f5c9580245..cba98204fc 100644--- a/packages/react-sdk/src/stitches/index.ts+++ b/packages/react-sdk/src/stitches/index.ts@@ -1,3 +1,2 @@export * from "./to-css";-export * from "./css";-export * from "./critical-css";+export * from "./stitches";diff --git a/packages/react-sdk/src/stitches/css.ts b/packages/react-sdk/src/stitches/stitches.tssimilarity index 96%rename from packages/react-sdk/src/stitches/css.tsrename to packages/react-sdk/src/stitches/stitches.tsindex af71864036..67662b3eb3 100644--- a/packages/react-sdk/src/stitches/css.ts+++ b/packages/react-sdk/src/stitches/stitches.ts@@ -14,7 +14,7 @@ let stitches: any;export { type CSS };-export const getCachedConfig = () => {+const getCachedConfig = () => {if (stitches === undefined) {stitches = createStitches({ media });}diff --git a/packages/react-sdk/src/stitches/to-css.ts b/packages/react-sdk/src/stitches/to-css.tsindex 5c4420caa5..4cee92ba01 100644--- a/packages/react-sdk/src/stitches/to-css.ts+++ b/packages/react-sdk/src/stitches/to-css.ts@@ -1,4 +1,4 @@-import type { CSS } from "./css";+import type { CSS } from "./stitches";import type { StyleProperty, StyleValue, CssRule, Breakpoint } from "../css";export const toValue = (value: StyleValue): string => {