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.

feat: Edge deployment to CF worker

Andras Bacsai64514fdfe16e
diff --git a/apps/designer/.env.example b/apps/designer/.env.example
index c04c47f861..3f97c8d3d5 100644
--- a/apps/designer/.env.example
+++ b/apps/designer/.env.example
@@ -17,4 +17,13 @@ S3_ACCESS_KEY_ID=
S3_SECRET_ACCESS_KEY=
# if using r2 this your worker url
-ASSET_CDN_URL=
\ No newline at end of file
+ASSET_CDN_URL=
+
+# Publish to edge
+EDGE_DEPLOYMENT=
+EDGE_DOMAIN=
+
+# Cloudflare API
+CF_ACCOUNT_ID=
+CF_NAMESPACE_ID=
+CF_TOKEN=
\ No newline at end of file
diff --git a/apps/designer/app/designer/features/topbar/publish.tsx b/apps/designer/app/designer/features/topbar/publish.tsx
index 4c429e1558..c0e35f1bf5 100644
--- a/apps/designer/app/designer/features/topbar/publish.tsx
+++ b/apps/designer/app/designer/features/topbar/publish.tsx
@@ -15,15 +15,20 @@ import {
Link,
} from "~/shared/design-system";
import { useIsPublishDialogOpen } from "../../shared/nano-states";
+import env from "~/shared/env";
type PublishButtonProps = { project: Project };
-const host =
- typeof location === "object"
- ? location.host.includes("webstudio.is")
- ? "wstd.io"
- : location.host
- : "";
+const host = () => {
+ if (env.EDGE_DEPLOYMENT && env.EDGE_DOMAIN) {
+ return env.EDGE_DOMAIN;
+ }
+ if (typeof location === "object") {
+ if (location.host.includes("webstudio.is")) return "wstd.io";
+ else return location.host;
+ }
+ return "";
+};
const Content = ({ project }: PublishButtonProps) => {
const id = useId();
@@ -35,7 +40,7 @@ const Content = ({ project }: PublishButtonProps) => {
if (typeof location !== "object" || !domain) {
return;
}
- setUrl(`${location.protocol}//${domain}.${host}`);
+ setUrl(`${location.protocol}//${domain}.${host()}`);
}, [domain]);
return (
@@ -64,7 +69,7 @@ const Content = ({ project }: PublishButtonProps) => {
textOverflow: "ellipsis",
}}
>
- {`${domain}.${host}`}{" "}
+ {`${domain}.${host()}`}{" "}
</Text>
<ExternalLinkIcon />
</Link>
diff --git a/apps/designer/app/env.server.ts b/apps/designer/app/env.server.ts
index 47aacbc299..40339349b4 100644
--- a/apps/designer/app/env.server.ts
+++ b/apps/designer/app/env.server.ts
@@ -5,6 +5,8 @@ const env = {
VERCEL_ENV: process.env.VERCEL_ENV,
DEBUG: process.env.DEBUG,
FEATURES: process.env.FEATURES,
+ EDGE_DEPLOYMENT: process.env.EDGE_DEPLOYMENT,
+ EDGE_DOMAIN: process.env.EDGE_DOMAIN,
} as const;
export default env;
diff --git a/apps/designer/app/routes/rest/publish.ts b/apps/designer/app/routes/rest/publish.ts
index 04f5bdeb34..9cfe015130 100644
--- a/apps/designer/app/routes/rest/publish.ts
+++ b/apps/designer/app/routes/rest/publish.ts
@@ -5,8 +5,41 @@ export const action: ActionFunction = async ({ request }) => {
const formData = await request.formData();
const domain = formData.get("domain") as string | null;
const projectId = formData.get("projectId") as string | null;
+ if (!domain || !projectId) {
+ throw new Error("Domain or ProjectId not provided.");
+ }
try {
const project = await db.misc.publish({ projectId, domain });
+ const projectLoad = await db.project.loadById(projectId);
+ const tree = await db.tree.loadByProject(projectLoad);
+ if (!tree) {
+ throw new Error("No tree found!");
+ }
+ const breakpoints = await db.breakpoints.getBreakpointsWithId();
+ const props = await db.props.loadByTreeId(tree.id);
+
+ if (process.env.EDGE_DEPLOYMENT) {
+ const headers = new Headers();
+ headers.append("Authorization", `Bearer ${process.env.CF_TOKEN || ""}`);
+ headers.append("Content-Type", "text/plain");
+ const response = await fetch(
+ `https://api.cloudflare.com/client/v4/accounts/${process.env.CF_ACCOUNT_ID}/storage/kv/namespaces/${process.env.CF_NAMESPACE_ID}/values/${domain}`,
+ {
+ method: "PUT",
+ headers,
+ body: JSON.stringify({
+ updated: new Date().getTime(),
+ tree,
+ breakpoints,
+ props,
+ }),
+ }
+ );
+ const text = await response.text();
+ if (!response.ok) {
+ throw new Error(text);
+ }
+ }
return { domain: project.domain };
} catch (error) {
if (error instanceof Error) {