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: handle empty action in webhook form (#3827)

Bogdan Chadkin78a81a012d17
diff --git a/fixtures/webstudio-cloudflare-template/app/routes/[another-page]._index.tsx b/fixtures/webstudio-cloudflare-template/app/routes/[another-page]._index.tsx
index 3c5be4c0c0..b8a0cf4baf 100644
--- a/fixtures/webstudio-cloudflare-template/app/routes/[another-page]._index.tsx
+++ b/fixtures/webstudio-cloudflare-template/app/routes/[another-page]._index.tsx
@@ -263,12 +263,10 @@ export const action = async ({
};
const resourceName = formData.get(formIdFieldName);
-
- if (resourceName == null || typeof resourceName !== "string") {
- throw new Error("No form id in FormData");
- }
-
- const resource = getResources({ system }).action.get(resourceName);
+ const resource =
+ typeof resourceName === "string"
+ ? getResources({ system }).action.get(resourceName)
+ : undefined;
const formBotValue = formData.get(formBotFieldName);
@@ -308,7 +306,7 @@ export const action = async ({
const result = await n8nHandler({
formInfo: {
- formId: [projectId, resourceName].join("--"),
+ formId: projectId,
formData,
pageUrl: pageUrl.toString(),
toEmail: contactEmail,
diff --git a/fixtures/webstudio-cloudflare-template/app/routes/_index.tsx b/fixtures/webstudio-cloudflare-template/app/routes/_index.tsx
index d4c5d2cc24..bcc8d52b23 100644
--- a/fixtures/webstudio-cloudflare-template/app/routes/_index.tsx
+++ b/fixtures/webstudio-cloudflare-template/app/routes/_index.tsx
@@ -263,12 +263,10 @@ export const action = async ({
};
const resourceName = formData.get(formIdFieldName);
-
- if (resourceName == null || typeof resourceName !== "string") {
- throw new Error("No form id in FormData");
- }
-
- const resource = getResources({ system }).action.get(resourceName);
+ const resource =
+ typeof resourceName === "string"
+ ? getResources({ system }).action.get(resourceName)
+ : undefined;
const formBotValue = formData.get(formBotFieldName);
@@ -308,7 +306,7 @@ export const action = async ({
const result = await n8nHandler({
formInfo: {
- formId: [projectId, resourceName].join("--"),
+ formId: projectId,
formData,
pageUrl: pageUrl.toString(),
toEmail: contactEmail,
diff --git a/fixtures/webstudio-custom-template/app/routes/[script-test]._index.tsx b/fixtures/webstudio-custom-template/app/routes/[script-test]._index.tsx
index 7667d04476..96ea7ae94f 100644
--- a/fixtures/webstudio-custom-template/app/routes/[script-test]._index.tsx
+++ b/fixtures/webstudio-custom-template/app/routes/[script-test]._index.tsx
@@ -263,12 +263,10 @@ export const action = async ({
};
const resourceName = formData.get(formIdFieldName);
-
- if (resourceName == null || typeof resourceName !== "string") {
- throw new Error("No form id in FormData");
- }
-
- const resource = getResources({ system }).action.get(resourceName);
+ const resource =
+ typeof resourceName === "string"
+ ? getResources({ system }).action.get(resourceName)
+ : undefined;
const formBotValue = formData.get(formBotFieldName);
@@ -308,7 +306,7 @@ export const action = async ({
const result = await n8nHandler({
formInfo: {
- formId: [projectId, resourceName].join("--"),
+ formId: projectId,
formData,
pageUrl: pageUrl.toString(),
toEmail: contactEmail,
diff --git a/fixtures/webstudio-custom-template/app/routes/[world]._index.tsx b/fixtures/webstudio-custom-template/app/routes/[world]._index.tsx
index 0649a0f98b..8472f5fc59 100644
--- a/fixtures/webstudio-custom-template/app/routes/[world]._index.tsx
+++ b/fixtures/webstudio-custom-template/app/routes/[world]._index.tsx
@@ -263,12 +263,10 @@ export const action = async ({
};
const resourceName = formData.get(formIdFieldName);
-
- if (resourceName == null || typeof resourceName !== "string") {
- throw new Error("No form id in FormData");
- }
-
- const resource = getResources({ system }).action.get(resourceName);
+ const resource =
+ typeof resourceName === "string"
+ ? getResources({ system }).action.get(resourceName)
+ : undefined;
const formBotValue = formData.get(formBotFieldName);
@@ -308,7 +306,7 @@ export const action = async ({
const result = await n8nHandler({
formInfo: {
- formId: [projectId, resourceName].join("--"),
+ formId: projectId,
formData,
pageUrl: pageUrl.toString(),
toEmail: contactEmail,
diff --git a/fixtures/webstudio-custom-template/app/routes/_index.tsx b/fixtures/webstudio-custom-template/app/routes/_index.tsx
index d4c5d2cc24..bcc8d52b23 100644
--- a/fixtures/webstudio-custom-template/app/routes/_index.tsx
+++ b/fixtures/webstudio-custom-template/app/routes/_index.tsx
@@ -263,12 +263,10 @@ export const action = async ({
};
const resourceName = formData.get(formIdFieldName);
-
- if (resourceName == null || typeof resourceName !== "string") {
- throw new Error("No form id in FormData");
- }
-
- const resource = getResources({ system }).action.get(resourceName);
+ const resource =
+ typeof resourceName === "string"
+ ? getResources({ system }).action.get(resourceName)
+ : undefined;
const formBotValue = formData.get(formBotFieldName);
@@ -308,7 +306,7 @@ export const action = async ({
const result = await n8nHandler({
formInfo: {
- formId: [projectId, resourceName].join("--"),
+ formId: projectId,
formData,
pageUrl: pageUrl.toString(),
toEmail: contactEmail,
diff --git a/fixtures/webstudio-remix-netlify-edge-functions/app/routes/[another-page]._index.tsx b/fixtures/webstudio-remix-netlify-edge-functions/app/routes/[another-page]._index.tsx
index 3c5be4c0c0..b8a0cf4baf 100644
--- a/fixtures/webstudio-remix-netlify-edge-functions/app/routes/[another-page]._index.tsx
+++ b/fixtures/webstudio-remix-netlify-edge-functions/app/routes/[another-page]._index.tsx
@@ -263,12 +263,10 @@ export const action = async ({
};
const resourceName = formData.get(formIdFieldName);
-
- if (resourceName == null || typeof resourceName !== "string") {
- throw new Error("No form id in FormData");
- }
-
- const resource = getResources({ system }).action.get(resourceName);
+ const resource =
+ typeof resourceName === "string"
+ ? getResources({ system }).action.get(resourceName)
+ : undefined;
const formBotValue = formData.get(formBotFieldName);
@@ -308,7 +306,7 @@ export const action = async ({
const result = await n8nHandler({
formInfo: {
- formId: [projectId, resourceName].join("--"),
+ formId: projectId,
formData,
pageUrl: pageUrl.toString(),
toEmail: contactEmail,
diff --git a/fixtures/webstudio-remix-netlify-edge-functions/app/routes/_index.tsx b/fixtures/webstudio-remix-netlify-edge-functions/app/routes/_index.tsx
index d4c5d2cc24..bcc8d52b23 100644
--- a/fixtures/webstudio-remix-netlify-edge-functions/app/routes/_index.tsx
+++ b/fixtures/webstudio-remix-netlify-edge-functions/app/routes/_index.tsx
@@ -263,12 +263,10 @@ export const action = async ({
};
const resourceName = formData.get(formIdFieldName);
-
- if (resourceName == null || typeof resourceName !== "string") {
- throw new Error("No form id in FormData");
- }
-
- const resource = getResources({ system }).action.get(resourceName);
+ const resource =
+ typeof resourceName === "string"
+ ? getResources({ system }).action.get(resourceName)
+ : undefined;
const formBotValue = formData.get(formBotFieldName);
@@ -308,7 +306,7 @@ export const action = async ({
const result = await n8nHandler({
formInfo: {
- formId: [projectId, resourceName].join("--"),
+ formId: projectId,
formData,
pageUrl: pageUrl.toString(),
toEmail: contactEmail,
diff --git a/fixtures/webstudio-remix-netlify-functions/app/routes/[another-page]._index.tsx b/fixtures/webstudio-remix-netlify-functions/app/routes/[another-page]._index.tsx
index 3c5be4c0c0..b8a0cf4baf 100644
--- a/fixtures/webstudio-remix-netlify-functions/app/routes/[another-page]._index.tsx
+++ b/fixtures/webstudio-remix-netlify-functions/app/routes/[another-page]._index.tsx
@@ -263,12 +263,10 @@ export const action = async ({
};
const resourceName = formData.get(formIdFieldName);
-
- if (resourceName == null || typeof resourceName !== "string") {
- throw new Error("No form id in FormData");
- }
-
- const resource = getResources({ system }).action.get(resourceName);
+ const resource =
+ typeof resourceName === "string"
+ ? getResources({ system }).action.get(resourceName)
+ : undefined;
const formBotValue = formData.get(formBotFieldName);
@@ -308,7 +306,7 @@ export const action = async ({
const result = await n8nHandler({
formInfo: {
- formId: [projectId, resourceName].join("--"),
+ formId: projectId,
formData,
pageUrl: pageUrl.toString(),
toEmail: contactEmail,
diff --git a/fixtures/webstudio-remix-netlify-functions/app/routes/_index.tsx b/fixtures/webstudio-remix-netlify-functions/app/routes/_index.tsx
index d4c5d2cc24..bcc8d52b23 100644
--- a/fixtures/webstudio-remix-netlify-functions/app/routes/_index.tsx
+++ b/fixtures/webstudio-remix-netlify-functions/app/routes/_index.tsx
@@ -263,12 +263,10 @@ export const action = async ({
};
const resourceName = formData.get(formIdFieldName);
-
- if (resourceName == null || typeof resourceName !== "string") {
- throw new Error("No form id in FormData");
- }
-
- const resource = getResources({ system }).action.get(resourceName);
+ const resource =
+ typeof resourceName === "string"
+ ? getResources({ system }).action.get(resourceName)
+ : undefined;
const formBotValue = formData.get(formBotFieldName);
@@ -308,7 +306,7 @@ export const action = async ({
const result = await n8nHandler({
formInfo: {
- formId: [projectId, resourceName].join("--"),
+ formId: projectId,
formData,
pageUrl: pageUrl.toString(),
toEmail: contactEmail,
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 22ead94dda..769cc6a50f 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
@@ -263,12 +263,10 @@ export const action = async ({
};
const resourceName = formData.get(formIdFieldName);
-
- if (resourceName == null || typeof resourceName !== "string") {
- throw new Error("No form id in FormData");
- }
-
- const resource = getResources({ system }).action.get(resourceName);
+ const resource =
+ typeof resourceName === "string"
+ ? getResources({ system }).action.get(resourceName)
+ : undefined;
const formBotValue = formData.get(formBotFieldName);
@@ -308,7 +306,7 @@ export const action = async ({
const result = await n8nHandler({
formInfo: {
- formId: [projectId, resourceName].join("--"),
+ formId: projectId,
formData,
pageUrl: pageUrl.toString(),
toEmail: contactEmail,
diff --git a/fixtures/webstudio-remix-vercel/app/routes/[form]._index.tsx b/fixtures/webstudio-remix-vercel/app/routes/[form]._index.tsx
index 985dc537f3..aef4047d4d 100644
--- a/fixtures/webstudio-remix-vercel/app/routes/[form]._index.tsx
+++ b/fixtures/webstudio-remix-vercel/app/routes/[form]._index.tsx
@@ -263,12 +263,10 @@ export const action = async ({
};
const resourceName = formData.get(formIdFieldName);
-
- if (resourceName == null || typeof resourceName !== "string") {
- throw new Error("No form id in FormData");
- }
-
- const resource = getResources({ system }).action.get(resourceName);
+ const resource =
+ typeof resourceName === "string"
+ ? getResources({ system }).action.get(resourceName)
+ : undefined;
const formBotValue = formData.get(formBotFieldName);
@@ -308,7 +306,7 @@ export const action = async ({
const result = await n8nHandler({
formInfo: {
- formId: [projectId, resourceName].join("--"),
+ formId: projectId,
formData,
pageUrl: pageUrl.toString(),
toEmail: contactEmail,
diff --git a/fixtures/webstudio-remix-vercel/app/routes/[heading-with-id]._index.tsx b/fixtures/webstudio-remix-vercel/app/routes/[heading-with-id]._index.tsx
index d5575db771..5fa1453e95 100644
--- a/fixtures/webstudio-remix-vercel/app/routes/[heading-with-id]._index.tsx
+++ b/fixtures/webstudio-remix-vercel/app/routes/[heading-with-id]._index.tsx
@@ -263,12 +263,10 @@ export const action = async ({
};
const resourceName = formData.get(formIdFieldName);
-
- if (resourceName == null || typeof resourceName !== "string") {
- throw new Error("No form id in FormData");
- }
-
- const resource = getResources({ system }).action.get(resourceName);
+ const resource =
+ typeof resourceName === "string"
+ ? getResources({ system }).action.get(resourceName)
+ : undefined;
const formBotValue = formData.get(formBotFieldName);
@@ -308,7 +306,7 @@ export const action = async ({
const result = await n8nHandler({
formInfo: {
- formId: [projectId, resourceName].join("--"),
+ formId: projectId,
formData,
pageUrl: pageUrl.toString(),
toEmail: contactEmail,
diff --git a/fixtures/webstudio-remix-vercel/app/routes/[nested].[nested-page]._index.tsx b/fixtures/webstudio-remix-vercel/app/routes/[nested].[nested-page]._index.tsx
index 460c8a8b70..1b0d7e9b8f 100644
--- a/fixtures/webstudio-remix-vercel/app/routes/[nested].[nested-page]._index.tsx
+++ b/fixtures/webstudio-remix-vercel/app/routes/[nested].[nested-page]._index.tsx
@@ -263,12 +263,10 @@ export const action = async ({
};
const resourceName = formData.get(formIdFieldName);
-
- if (resourceName == null || typeof resourceName !== "string") {
- throw new Error("No form id in FormData");
- }
-
- const resource = getResources({ system }).action.get(resourceName);
+ const resource =
+ typeof resourceName === "string"
+ ? getResources({ system }).action.get(resourceName)
+ : undefined;
const formBotValue = formData.get(formBotFieldName);
@@ -308,7 +306,7 @@ export const action = async ({
const result = await n8nHandler({
formInfo: {
- formId: [projectId, resourceName].join("--"),
+ formId: projectId,
formData,
pageUrl: pageUrl.toString(),
toEmail: contactEmail,
diff --git a/fixtures/webstudio-remix-vercel/app/routes/[radix]._index.tsx b/fixtures/webstudio-remix-vercel/app/routes/[radix]._index.tsx
index a1eedb0fad..a76d753717 100644
--- a/fixtures/webstudio-remix-vercel/app/routes/[radix]._index.tsx
+++ b/fixtures/webstudio-remix-vercel/app/routes/[radix]._index.tsx
@@ -263,12 +263,10 @@ export const action = async ({
};
const resourceName = formData.get(formIdFieldName);
-
- if (resourceName == null || typeof resourceName !== "string") {
- throw new Error("No form id in FormData");
- }
-
- const resource = getResources({ system }).action.get(resourceName);
+ const resource =
+ typeof resourceName === "string"
+ ? getResources({ system }).action.get(resourceName)
+ : undefined;
const formBotValue = formData.get(formBotFieldName);
@@ -308,7 +306,7 @@ export const action = async ({
const result = await n8nHandler({
formInfo: {
- formId: [projectId, resourceName].join("--"),
+ formId: projectId,
formData,
pageUrl: pageUrl.toString(),
toEmail: contactEmail,
diff --git a/fixtures/webstudio-remix-vercel/app/routes/[resources]._index.tsx b/fixtures/webstudio-remix-vercel/app/routes/[resources]._index.tsx
index 5595b040c7..80e38fe638 100644
--- a/fixtures/webstudio-remix-vercel/app/routes/[resources]._index.tsx
+++ b/fixtures/webstudio-remix-vercel/app/routes/[resources]._index.tsx
@@ -263,12 +263,10 @@ export const action = async ({
};
const resourceName = formData.get(formIdFieldName);
-
- if (resourceName == null || typeof resourceName !== "string") {
- throw new Error("No form id in FormData");
- }
-
- const resource = getResources({ system }).action.get(resourceName);
+ const resource =
+ typeof resourceName === "string"
+ ? getResources({ system }).action.get(resourceName)
+ : undefined;
const formBotValue = formData.get(formBotFieldName);
@@ -308,7 +306,7 @@ export const action = async ({
const result = await n8nHandler({
formInfo: {
- formId: [projectId, resourceName].join("--"),
+ formId: projectId,
formData,
pageUrl: pageUrl.toString(),
toEmail: contactEmail,
diff --git a/fixtures/webstudio-remix-vercel/app/routes/_index.tsx b/fixtures/webstudio-remix-vercel/app/routes/_index.tsx
index d4c5d2cc24..bcc8d52b23 100644
--- a/fixtures/webstudio-remix-vercel/app/routes/_index.tsx
+++ b/fixtures/webstudio-remix-vercel/app/routes/_index.tsx
@@ -263,12 +263,10 @@ export const action = async ({
};
const resourceName = formData.get(formIdFieldName);
-
- if (resourceName == null || typeof resourceName !== "string") {
- throw new Error("No form id in FormData");
- }
-
- const resource = getResources({ system }).action.get(resourceName);
+ const resource =
+ typeof resourceName === "string"
+ ? getResources({ system }).action.get(resourceName)
+ : undefined;
const formBotValue = formData.get(formBotFieldName);
@@ -308,7 +306,7 @@ export const action = async ({
const result = await n8nHandler({
formInfo: {
- formId: [projectId, resourceName].join("--"),
+ formId: projectId,
formData,
pageUrl: pageUrl.toString(),
toEmail: contactEmail,
diff --git a/packages/cli/templates/defaults/app/route-templates/html.tsx b/packages/cli/templates/defaults/app/route-templates/html.tsx
index 78ec43a564..ea1fa5c0c2 100644
--- a/packages/cli/templates/defaults/app/route-templates/html.tsx
+++ b/packages/cli/templates/defaults/app/route-templates/html.tsx
@@ -263,12 +263,10 @@ export const action = async ({
};
const resourceName = formData.get(formIdFieldName);
-
- if (resourceName == null || typeof resourceName !== "string") {
- throw new Error("No form id in FormData");
- }
-
- const resource = getResources({ system }).action.get(resourceName);
+ const resource =
+ typeof resourceName === "string"
+ ? getResources({ system }).action.get(resourceName)
+ : undefined;
const formBotValue = formData.get(formBotFieldName);
@@ -308,7 +306,7 @@ export const action = async ({
const result = await n8nHandler({
formInfo: {
- formId: [projectId, resourceName].join("--"),
+ formId: projectId,
formData,
pageUrl: pageUrl.toString(),
toEmail: contactEmail,
diff --git a/packages/sdk/src/resources-generator.test.ts b/packages/sdk/src/resources-generator.test.tsx
similarity index 87%
rename from packages/sdk/src/resources-generator.test.ts
rename to packages/sdk/src/resources-generator.test.tsx
index 7d085c7f6e..f948c98254 100644
--- a/packages/sdk/src/resources-generator.test.ts
+++ b/packages/sdk/src/resources-generator.test.tsx
@@ -1,7 +1,12 @@
import { expect, test } from "@jest/globals";
+import { renderJsx, $ } from "./jsx";
import type { Page } from "./schema/pages";
import { createScope } from "./scope";
-import { generateResources } from "./resources-generator";
+import {
+ generateResources,
+ replaceFormActionsWithResources,
+} from "./resources-generator";
+import type { Resource } from "./schema/resources";
const toMap = <T extends { id: string }>(list: T[]) =>
new Map(list.map((item) => [item.id, item] as const));
@@ -315,3 +320,53 @@ test("generate action resource", () => {
"
`);
});
+
+test("replace form action with resource", () => {
+ const data = {
+ resources: toMap<Resource>([]),
+ ...renderJsx(<$.Form ws:id="formId" action="https://my-url.com"></$.Form>),
+ };
+ replaceFormActionsWithResources(data);
+ expect(data.props).toEqual(
+ toMap([
+ {
+ id: "formId:action",
+ instanceId: "formId",
+ name: "action",
+ type: "resource",
+ value: "formId",
+ },
+ ])
+ );
+ expect(data.resources).toEqual(
+ toMap([
+ {
+ headers: [],
+ id: "formId",
+ method: "post",
+ name: "action",
+ url: `"https://my-url.com"`,
+ },
+ ])
+ );
+});
+
+test("ignore empty form action", () => {
+ const data = {
+ resources: toMap<Resource>([]),
+ ...renderJsx(<$.Form ws:id="formId" action=""></$.Form>),
+ };
+ replaceFormActionsWithResources(data);
+ expect(data.props).toEqual(
+ toMap([
+ {
+ id: "formId:action",
+ instanceId: "formId",
+ name: "action",
+ type: "string",
+ value: "",
+ },
+ ])
+ );
+ expect(data.resources).toEqual(new Map());
+});
diff --git a/packages/sdk/src/resources-generator.ts b/packages/sdk/src/resources-generator.ts
index ed70e172f5..35d4929012 100644
--- a/packages/sdk/src/resources-generator.ts
+++ b/packages/sdk/src/resources-generator.ts
@@ -157,6 +157,7 @@ export const replaceFormActionsWithResources = ({
if (
prop.name === "action" &&
prop.type === "string" &&
+ prop.value &&
instances.get(prop.instanceId)?.component === "Form"
) {
let data = formProps.get(prop.instanceId);