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.

experimental: Templates support (#3819)

Ivan Starkov02e753158573
diff --git a/apps/builder/app/builder/features/topbar/publish.tsx b/apps/builder/app/builder/features/topbar/publish.tsx
index 2668624832..bdf8fdfc77 100644
--- a/apps/builder/app/builder/features/topbar/publish.tsx
+++ b/apps/builder/app/builder/features/topbar/publish.tsx
@@ -433,6 +433,7 @@ const PublishStatic = ({ projectId }: { projectId: Project["id"] }) => {
const result = await nativeClient.domain.publish.mutate({
projectId,
destination: "static",
+ templates: ["ssg"],
});
if (result.success === false) {
diff --git a/packages/cli/package.json b/packages/cli/package.json
index bec85359cf..ff6af07aeb 100644
--- a/packages/cli/package.json
+++ b/packages/cli/package.json
@@ -70,6 +70,7 @@
"@webstudio-is/tsconfig": "workspace:*",
"react": "18.3.0-canary-14898b6a9-20240318",
"react-dom": "18.3.0-canary-14898b6a9-20240318",
+ "ts-expect": "^1.3.0",
"typescript": "5.5.2",
"vike": "^0.4.180",
"vite": "^5.3.4",
diff --git a/packages/cli/src/config.ts-expect.ts b/packages/cli/src/config.ts-expect.ts
new file mode 100644
index 0000000000..a50023917d
--- /dev/null
+++ b/packages/cli/src/config.ts-expect.ts
@@ -0,0 +1,10 @@
+import { expectType, type TypeEqual } from "ts-expect";
+import { Templates } from "@webstudio-is/sdk";
+import { PROJECT_TEMPALTES } from "./config";
+
+// We must ensure the validated template type always matches the CLI-supported templates.
+// This is crucial for security, as template names can be used in CLI/bash environments.
+// A TypeScript failure here means the Templates type is not consistent with the CLI templates.
+expectType<TypeEqual<(typeof PROJECT_TEMPALTES)[number]["value"], Templates>>(
+ true
+);
diff --git a/packages/domain/package.json b/packages/domain/package.json
index 4af5fe9037..412da2501d 100644
--- a/packages/domain/package.json
+++ b/packages/domain/package.json
@@ -13,6 +13,7 @@
"@webstudio-is/project": "workspace:*",
"@webstudio-is/project-build": "workspace:*",
"@webstudio-is/trpc-interface": "workspace:*",
+ "@webstudio-is/sdk": "workspace:*",
"nanoid": "^5.0.1",
"zod": "^3.22.4"
},
diff --git a/packages/domain/src/trpc/domain.ts b/packages/domain/src/trpc/domain.ts
index 1755b0daee..4bbebbe176 100644
--- a/packages/domain/src/trpc/domain.ts
+++ b/packages/domain/src/trpc/domain.ts
@@ -5,6 +5,7 @@ import { db } from "../db";
import { createProductionBuild } from "@webstudio-is/project-build/index.server";
import { router, procedure } from "@webstudio-is/trpc-interface/index.server";
import { nanoid } from "nanoid";
+import { Templates } from "@webstudio-is/sdk";
export const domainRouter = router({
getEntriToken: procedure.query(async ({ ctx }) => {
@@ -52,6 +53,7 @@ export const domainRouter = router({
z.object({
projectId: z.string(),
destination: z.literal("static"),
+ templates: z.array(Templates),
}),
])
)
@@ -61,6 +63,30 @@ export const domainRouter = router({
const name = `${project.id}-${nanoid()}.zip`;
+ let domains: string[] = [];
+
+ if (input.destination === "saas") {
+ const currentProjectDomainsResult = await db.findMany(
+ { projectId: input.projectId },
+ ctx
+ );
+
+ if (currentProjectDomainsResult.success === false) {
+ throw new Error(currentProjectDomainsResult.error);
+ }
+
+ const currentProjectDomains = currentProjectDomainsResult.data;
+
+ domains = input.domains.filter((domain) =>
+ currentProjectDomains.some(
+ (projectDomain) =>
+ projectDomain.domain.domain === domain &&
+ projectDomain.domain.status === "ACTIVE" &&
+ projectDomain.verified
+ )
+ );
+ }
+
const build = await createProductionBuild(
{
projectId: input.projectId,
@@ -68,14 +94,14 @@ export const domainRouter = router({
input.destination === "saas"
? {
destination: input.destination,
- domains: input.domains,
+ domains: domains,
projectDomain: project.domain,
}
: {
destination: input.destination,
name,
assetsDomain: project.domain,
- templates: ["ssg"],
+ templates: input.templates,
},
},
ctx
diff --git a/packages/sdk/src/schema/deployment.ts b/packages/sdk/src/schema/deployment.ts
index 9281215aeb..c8fe2d1e89 100644
--- a/packages/sdk/src/schema/deployment.ts
+++ b/packages/sdk/src/schema/deployment.ts
@@ -1,11 +1,22 @@
import { z } from "zod";
+export const Templates = z.enum([
+ "vanilla",
+ "vercel",
+ "netlify-functions",
+ "netlify-edge-functions",
+ "ssg",
+]);
+
+export type Templates = z.infer<typeof Templates>;
+
export const Deployment = z.union([
z.object({
destination: z.literal("static"),
name: z.string(),
assetsDomain: z.string(),
- templates: z.array(z.string()),
+ // Must be validated very strictly
+ templates: z.array(Templates),
}),
z.object({
destination: z.literal("saas").optional(),
diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml
index 3fd947b560..0e932fbcb4 100644
--- a/pnpm-lock.yaml
+++ b/pnpm-lock.yaml
@@ -1086,6 +1086,9 @@ importers:
react-dom:
specifier: 18.3.0-canary-14898b6a9-20240318
version: 18.3.0-canary-14898b6a9-20240318([email protected])
+ ts-expect:
+ specifier: ^1.3.0
+ version: 1.3.0
typescript:
specifier: 5.5.2
version: 5.5.2
@@ -1393,6 +1396,9 @@ importers:
'@webstudio-is/project-build':
specifier: workspace:*
version: link:../project-build
+ '@webstudio-is/sdk':
+ specifier: workspace:*
+ version: link:../sdk
'@webstudio-is/trpc-interface':
specifier: workspace:*
version: link:../trpc-interface
@@ -9467,6 +9473,9 @@ packages:
resolution: {integrity: sha512-q5W7tVM71e2xjHZTlgfTDoPF/SmqKG5hddq9SzR49CH2hayqRKJtQ4mtRlSxKaJlR/+9rEM+mnBHf7I2/BQcpQ==}
engines: {node: '>=6.10'}
+ resolution: {integrity: sha512-e4g0EJtAjk64xgnFPD6kTBUtpnMVzDrMb12N1YZV0VvSlhnVT3SGxiYTLdGy8Q5cYHOIC/FAHmZ10eGrAguicQ==}
+
resolution: {integrity: sha512-Y/arvbn+rrz3JCKl9C4kVNfTfSm2/mEp5FSz5EsZSANGPSlQrpRI5M4PKF+mJnE52jOO90PnPSc3Ur3bTQw0gA==}
@@ -18770,6 +18779,8 @@ snapshots:
+