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.

Pregenerate forms properties (#2323)

Bogdan Chadkin1c7df489258a
diff --git a/fixtures/webstudio-remix-vercel/app/__generated__/[_route_with_symbols_]._index.tsx b/fixtures/webstudio-remix-vercel/app/__generated__/[_route_with_symbols_]._index.tsx
index ccc8d32e1f..e9a63f868b 100644
--- a/fixtures/webstudio-remix-vercel/app/__generated__/[_route_with_symbols_]._index.tsx
+++ b/fixtures/webstudio-remix-vercel/app/__generated__/[_route_with_symbols_]._index.tsx
@@ -126,6 +126,11 @@ const getDataSourcesLogic = (
return _output;
};
+export const formsProperties = new Map<
+ string,
+ { method?: string; action?: string }
+>([]);
+
export const utils = {
indexesWithinAncestors,
getDataSourcesLogic,
diff --git a/fixtures/webstudio-remix-vercel/app/__generated__/[radix]._index.tsx b/fixtures/webstudio-remix-vercel/app/__generated__/[radix]._index.tsx
index dc87d2fe8c..a91bb70190 100644
--- a/fixtures/webstudio-remix-vercel/app/__generated__/[radix]._index.tsx
+++ b/fixtures/webstudio-remix-vercel/app/__generated__/[radix]._index.tsx
@@ -469,6 +469,11 @@ const getDataSourcesLogic = (
return _output;
};
+export const formsProperties = new Map<
+ string,
+ { method?: string; action?: string }
+>([]);
+
export const utils = {
indexesWithinAncestors,
getDataSourcesLogic,
diff --git a/fixtures/webstudio-remix-vercel/app/__generated__/_index.tsx b/fixtures/webstudio-remix-vercel/app/__generated__/_index.tsx
index a3fc4e1f96..41faee73e3 100644
--- a/fixtures/webstudio-remix-vercel/app/__generated__/_index.tsx
+++ b/fixtures/webstudio-remix-vercel/app/__generated__/_index.tsx
@@ -262,6 +262,11 @@ const getDataSourcesLogic = (
return _output;
};
+export const formsProperties = new Map<
+ string,
+ { method?: string; action?: string }
+>([]);
+
export const utils = {
indexesWithinAncestors,
getDataSourcesLogic,
diff --git a/fixtures/webstudio-remix-vercel/app/routes/[_route_with_symbols_]._index.tsx b/fixtures/webstudio-remix-vercel/app/routes/[_route_with_symbols_]._index.tsx
index f63a540444..5ea7236eac 100644
--- a/fixtures/webstudio-remix-vercel/app/routes/[_route_with_symbols_]._index.tsx
+++ b/fixtures/webstudio-remix-vercel/app/routes/[_route_with_symbols_]._index.tsx
@@ -12,7 +12,7 @@ import {
type RootPropsData,
type Params,
} from "@webstudio-is/react-sdk";
-import { n8nHandler, getFormProperties } from "@webstudio-is/form-handlers";
+import { n8nHandler, getFormId } from "@webstudio-is/form-handlers";
import { Scripts, ScrollRestoration } from "@remix-run/react";
import {
fontAssets,
@@ -21,6 +21,7 @@ import {
user,
projectId,
utils,
+ formsProperties,
} from "../__generated__/[_route_with_symbols_]._index.tsx";
import css from "../__generated__/index.css";
import type { Data } from "@webstudio-is/http-client";
@@ -78,7 +79,9 @@ const getMethod = (value: string | undefined) => {
export const action = async ({ request, context }: ActionArgs) => {
const formData = await request.formData();
- const formProperties = getFormProperties(formData, pageData.build.props);
+ const formId = getFormId(formData);
+ const formProperties =
+ formId === undefined ? undefined : formsProperties.get(formId);
if (formProperties === undefined) {
// We're throwing rather than returning { success: false }
diff --git a/fixtures/webstudio-remix-vercel/app/routes/[radix]._index.tsx b/fixtures/webstudio-remix-vercel/app/routes/[radix]._index.tsx
index 5e7212e173..11c4b6d123 100644
--- a/fixtures/webstudio-remix-vercel/app/routes/[radix]._index.tsx
+++ b/fixtures/webstudio-remix-vercel/app/routes/[radix]._index.tsx
@@ -12,7 +12,7 @@ import {
type RootPropsData,
type Params,
} from "@webstudio-is/react-sdk";
-import { n8nHandler, getFormProperties } from "@webstudio-is/form-handlers";
+import { n8nHandler, getFormId } from "@webstudio-is/form-handlers";
import { Scripts, ScrollRestoration } from "@remix-run/react";
import {
fontAssets,
@@ -21,6 +21,7 @@ import {
user,
projectId,
utils,
+ formsProperties,
} from "../__generated__/[radix]._index.tsx";
import css from "../__generated__/index.css";
import type { Data } from "@webstudio-is/http-client";
@@ -78,7 +79,9 @@ const getMethod = (value: string | undefined) => {
export const action = async ({ request, context }: ActionArgs) => {
const formData = await request.formData();
- const formProperties = getFormProperties(formData, pageData.build.props);
+ const formId = getFormId(formData);
+ const formProperties =
+ formId === undefined ? undefined : formsProperties.get(formId);
if (formProperties === undefined) {
// We're throwing rather than returning { success: false }
diff --git a/fixtures/webstudio-remix-vercel/app/routes/_index.tsx b/fixtures/webstudio-remix-vercel/app/routes/_index.tsx
index ea3da85c98..44f8332767 100644
--- a/fixtures/webstudio-remix-vercel/app/routes/_index.tsx
+++ b/fixtures/webstudio-remix-vercel/app/routes/_index.tsx
@@ -12,7 +12,7 @@ import {
type RootPropsData,
type Params,
} from "@webstudio-is/react-sdk";
-import { n8nHandler, getFormProperties } from "@webstudio-is/form-handlers";
+import { n8nHandler, getFormId } from "@webstudio-is/form-handlers";
import { Scripts, ScrollRestoration } from "@remix-run/react";
import {
fontAssets,
@@ -21,6 +21,7 @@ import {
user,
projectId,
utils,
+ formsProperties,
} from "../__generated__/_index.tsx";
import css from "../__generated__/index.css";
import type { Data } from "@webstudio-is/http-client";
@@ -78,7 +79,9 @@ const getMethod = (value: string | undefined) => {
export const action = async ({ request, context }: ActionArgs) => {
const formData = await request.formData();
- const formProperties = getFormProperties(formData, pageData.build.props);
+ const formId = getFormId(formData);
+ const formProperties =
+ formId === undefined ? undefined : formsProperties.get(formId);
if (formProperties === undefined) {
// We're throwing rather than returning { success: false }
diff --git a/packages/cli/__generated__/index.ts b/packages/cli/__generated__/index.ts
index 18f1fd45d1..97b7773b43 100644
--- a/packages/cli/__generated__/index.ts
+++ b/packages/cli/__generated__/index.ts
@@ -39,6 +39,11 @@ const getDataSourcesLogic = () => {
return new Map();
};
+export const formsProperties = new Map<
+ string,
+ { method?: string; action?: string }
+>([]);
+
export const utils = {
indexesWithinAncestors,
getDataSourcesLogic,
diff --git a/packages/cli/src/prebuild.ts b/packages/cli/src/prebuild.ts
index 5667095a44..ec30385b07 100644
--- a/packages/cli/src/prebuild.ts
+++ b/packages/cli/src/prebuild.ts
@@ -356,6 +356,7 @@ export const prebuild = async (options: {
"pageData",
"user",
"projectId",
+ "formsProperties",
"indexesWithinAncestors",
"getDataSourcesLogic",
"utils",
diff --git a/packages/cli/templates/route-template.tsx b/packages/cli/templates/route-template.tsx
index abea941062..9e06fa89bc 100644
--- a/packages/cli/templates/route-template.tsx
+++ b/packages/cli/templates/route-template.tsx
@@ -12,7 +12,7 @@ import {
type RootPropsData,
type Params,
} from "@webstudio-is/react-sdk";
-import { n8nHandler, getFormProperties } from "@webstudio-is/form-handlers";
+import { n8nHandler, getFormId } from "@webstudio-is/form-handlers";
import { Scripts, ScrollRestoration } from "@remix-run/react";
import {
fontAssets,
@@ -21,6 +21,7 @@ import {
user,
projectId,
utils,
+ formsProperties,
} from "../__generated__/index";
import css from "../__generated__/index.css";
import type { Data } from "@webstudio-is/http-client";
@@ -78,7 +79,9 @@ const getMethod = (value: string | undefined) => {
export const action = async ({ request, context }: ActionArgs) => {
const formData = await request.formData();
- const formProperties = getFormProperties(formData, pageData.build.props);
+ const formId = getFormId(formData);
+ const formProperties =
+ formId === undefined ? undefined : formsProperties.get(formId);
if (formProperties === undefined) {
// We're throwing rather than returning { success: false }
diff --git a/packages/form-handlers/package.json b/packages/form-handlers/package.json
index 3698dada02..ee866465cb 100644
--- a/packages/form-handlers/package.json
+++ b/packages/form-handlers/package.json
@@ -12,9 +12,6 @@
"build": "rm -rf lib && esbuild src/index.ts --outdir=lib --bundle --format=esm --packages=external",
"dts": "tsc --project tsconfig.dts.json"
},
- "dependencies": {
- "@webstudio-is/sdk": "workspace:^"
- },
"devDependencies": {
"@webstudio-is/tsconfig": "workspace:^"
},
diff --git a/packages/form-handlers/src/index.ts b/packages/form-handlers/src/index.ts
index 6eba579725..01a4e0ea99 100644
--- a/packages/form-handlers/src/index.ts
+++ b/packages/form-handlers/src/index.ts
@@ -3,7 +3,6 @@ export {
formIdFieldName,
formHiddenFieldPrefix,
getFormId,
- getFormProperties,
type EmailInfo,
type FormInfo,
} from "./shared";
diff --git a/packages/form-handlers/src/shared.ts b/packages/form-handlers/src/shared.ts
index a6633d747d..a9c37cc7d5 100644
--- a/packages/form-handlers/src/shared.ts
+++ b/packages/form-handlers/src/shared.ts
@@ -1,5 +1,3 @@
-import type { Prop } from "@webstudio-is/sdk";
-
export const formHiddenFieldPrefix = "ws--form";
export const formIdFieldName = `${formHiddenFieldPrefix}-id`;
@@ -110,32 +108,3 @@ export const getErrors = (
return json.errors;
}
};
-
-export const getFormProperties = (
- formData: FormData,
- props: [Prop["id"], Prop][]
-) => {
- const formId = getFormId(formData);
-
- if (formId === undefined) {
- return undefined;
- }
-
- const objectProps = Object.fromEntries(
- props
- .filter(([_, value]) => value.instanceId === formId)
- .map(([_, value]) => [value.name, value])
- );
-
- const action =
- objectProps.action?.type === "string"
- ? objectProps.action.value
- : undefined;
-
- const method =
- objectProps.method?.type === "string"
- ? objectProps.method.value
- : undefined;
-
- return { action, method };
-};
diff --git a/packages/react-sdk/src/generator.test.ts b/packages/react-sdk/src/generator.test.ts
index d8a0a7a2fd..6a18953770 100644
--- a/packages/react-sdk/src/generator.test.ts
+++ b/packages/react-sdk/src/generator.test.ts
@@ -86,6 +86,39 @@ test("generates utils", () => {
],
},
],
+
+ [
+ "method1Id",
+ {
+ id: "method1Id",
+ instanceId: "1",
+ name: "method",
+ type: "string",
+ value: "post",
+ },
+ ],
+
+ [
+ "method2Id",
+ {
+ id: "method2Id",
+ instanceId: "2",
+ name: "method",
+ type: "string",
+ value: "get",
+ },
+ ],
+
+ [
+ "action1Id",
+ {
+ id: "action1Id",
+ instanceId: "2",
+ name: "action",
+ type: "string",
+ value: "/index.php",
+ },
+ ],
]),
dataSources: new Map([
[
@@ -127,6 +160,8 @@ test("generates utils", () => {
}
+ export const formsProperties = new Map<string, { method?: string, action?: string }>([["1",{"method":"post"}],["2",{"method":"get","action":"/index.php"}]])
+
export const utils = {
indexesWithinAncestors,
getDataSourcesLogic,
diff --git a/packages/react-sdk/src/generator.ts b/packages/react-sdk/src/generator.ts
index 65b594a8a5..0f914236d7 100644
--- a/packages/react-sdk/src/generator.ts
+++ b/packages/react-sdk/src/generator.ts
@@ -77,6 +77,26 @@ export const generateUtilsExport = (siteData: PageData) => {
generatedDataSources += `return _output\n`;
generatedDataSources += `}\n`;
+ const formsProperties = new Map<
+ string,
+ { method?: string; action?: string }
+ >();
+ for (const prop of siteData.props.values()) {
+ if (prop.type === "string") {
+ if (prop.name === "action" || prop.name === "method") {
+ let properties = formsProperties.get(prop.instanceId);
+ if (properties === undefined) {
+ properties = {};
+ }
+ properties[prop.name] = prop.value;
+ formsProperties.set(prop.instanceId, properties);
+ }
+ }
+ }
+ const generatedFormsProperties = `export const formsProperties = new Map<string, { method?: string, action?: string }>(${JSON.stringify(
+ Array.from(formsProperties.entries())
+ )})`;
+
return `
/* eslint-disable */
@@ -84,6 +104,8 @@ export const generateUtilsExport = (siteData: PageData) => {
${generatedDataSources}
+ ${generatedFormsProperties}
+
export const utils = {
indexesWithinAncestors,
getDataSourcesLogic,
diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml
index 57e525c0f9..4ee7557106 100644
--- a/pnpm-lock.yaml
+++ b/pnpm-lock.yaml
@@ -954,10 +954,6 @@ importers:
version: 3.21.4
packages/form-handlers:
- dependencies:
- '@webstudio-is/sdk':
- specifier: workspace:^
- version: link:../sdk
devDependencies:
'@webstudio-is/tsconfig':
specifier: workspace:^