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: Export configured Webhook Form methods (#6192)

Oleg Isonena2e01a8ebed8
diff --git a/fixtures/webstudio-features/app/__generated__/[form]._index.tsx b/fixtures/webstudio-features/app/__generated__/[form]._index.tsx
index fc34548291..140df88b98 100644
--- a/fixtures/webstudio-features/app/__generated__/[form]._index.tsx
+++ b/fixtures/webstudio-features/app/__generated__/[form]._index.tsx
@@ -92,6 +92,7 @@ formState_1 = state
set$formState_1(formState_1)
}}
action={"action"}
+method={"get"}
className={`w-webhook-form`}>
{(formState_1 === 'initial' || formState_1 === 'error') &&
<Box
diff --git a/packages/cli/src/prebuild.test.ts b/packages/cli/src/prebuild.test.ts
index 45c049fe67..82dfa5cbb6 100644
--- a/packages/cli/src/prebuild.test.ts
+++ b/packages/cli/src/prebuild.test.ts
@@ -1992,6 +1992,68 @@ sitemap.map((page) => page.path);`
);
});
+ test("prerenders the configured Webhook Form method", async () => {
+ const siteData = createSiteData({
+ instances: [
+ [
+ "root",
+ {
+ id: "root",
+ component: "Box",
+ children: [{ type: "id", value: "form" }],
+ },
+ ],
+ [
+ "form",
+ {
+ id: "form",
+ component: "Form",
+ children: [],
+ },
+ ],
+ ],
+ props: [
+ [
+ "form-action",
+ {
+ id: "form-action",
+ instanceId: "form",
+ name: "action",
+ type: "resource",
+ value: "webhook",
+ },
+ ],
+ ],
+ });
+ siteData.build.resources = [
+ [
+ "webhook",
+ {
+ id: "webhook",
+ name: "action",
+ method: "post",
+ url: '"https://example.com/webhook"',
+ headers: [],
+ },
+ ],
+ ] as never;
+ await writeSiteData(siteData);
+
+ await prebuild({ assets: false, template: ["ssg"] });
+ await symlink(join(originalCwd, "node_modules"), "node_modules", "dir");
+ await runGeneratedCommand("vite", ["build"]);
+ await runGeneratedCommand("vike", ["prerender"]);
+
+ const html = parseHtml(await readFile("dist/client/index.html", "utf8"));
+ const [form] = findElementsByTagName(html, "form");
+ if (form === undefined) {
+ throw new Error("Expected a prerendered Webhook Form");
+ }
+ expect(
+ Object.fromEntries(form.attrs.map(({ name, value }) => [name, value]))
+ ).toMatchObject({ method: "post" });
+ }, 30_000);
+
test("ignores dynamic SSG pages without enumerable Assets query paths", async () => {
await writeSiteData(
createSiteData({
diff --git a/packages/cli/src/prebuild.ts b/packages/cli/src/prebuild.ts
index 74b3a77104..2d480ffb2d 100644
--- a/packages/cli/src/prebuild.ts
+++ b/packages/cli/src/prebuild.ts
@@ -1228,6 +1228,7 @@ export const prebuild = async (options: {
],
instances,
props,
+ resources,
dataSources,
classesMap: classes,
metas: usedMetas,
diff --git a/packages/react-sdk/src/component-generator.test.tsx b/packages/react-sdk/src/component-generator.test.tsx
index 736bd2270f..d70cf8e6ef 100644
--- a/packages/react-sdk/src/component-generator.test.tsx
+++ b/packages/react-sdk/src/component-generator.test.tsx
@@ -1005,10 +1005,10 @@ return (condition) &&
`);
});
-test("generate resource prop", () => {
+test("generate resource prop with configured form method", () => {
const myResource = new ResourceValue("myResource", {
url: expression`"https://my-url.com?with-secret"`,
- method: "get",
+ method: "post",
searchParams: [],
headers: [],
});
@@ -1025,11 +1025,26 @@ test("generate resource prop", () => {
name: "Page",
rootInstanceId: "body",
parameters: [],
- metas: new Map(),
+ metas: new Map([
+ [
+ "Form",
+ {
+ props: {
+ action: {
+ control: "resource",
+ type: "resource",
+ required: false,
+ generatedProps: ["method"],
+ },
+ },
+ },
+ ],
+ ]),
...renderData(
<$.Body ws:id="body">
<$.Form ws:id="form1" action={myResource}></$.Form>
<$.Form ws:id="form2" action={anotherResource}></$.Form>
+ <$.Form ws:id="form3" action={myResource} method="get"></$.Form>
</$.Body>
),
})
@@ -1037,9 +1052,14 @@ test("generate resource prop", () => {
"const Page = () => {
return <Body>
<Form
- action={"action"} />
+ action={"action"}
+ method={"post"} />
+ <Form
+ action={"action_1"}
+ method={"get"} />
<Form
- action={"action_1"} />
+ action={"action"}
+ method={"get"} />
</Body>
}
"
diff --git a/packages/react-sdk/src/component-generator.ts b/packages/react-sdk/src/component-generator.ts
index 19c1cf53d5..2565ca9ad5 100644
--- a/packages/react-sdk/src/component-generator.ts
+++ b/packages/react-sdk/src/component-generator.ts
@@ -8,6 +8,7 @@ import type {
DataSource,
WsComponentMeta,
IndexesWithinAncestors,
+ Resources,
} from "@webstudio-is/sdk";
import {
parseComponentName,
@@ -149,6 +150,7 @@ export const generateJsxElement = ({
tagsOverrides,
instance,
props,
+ resources,
dataSources,
usedDataSources,
indexesWithinAncestors,
@@ -164,6 +166,7 @@ export const generateJsxElement = ({
tagsOverrides?: Record<string, string>;
instance: Instance;
props: Props;
+ resources?: Resources;
dataSources: DataSources;
usedDataSources: DataSources;
indexesWithinAncestors: IndexesWithinAncestors;
@@ -194,12 +197,20 @@ export const generateJsxElement = ({
let collectionItemValue: undefined | string;
let collectionItemKeyValue: undefined | string;
let classNameValue: undefined | string;
+ const instanceProps = Array.from(props.values()).filter(
+ (prop) => prop.instanceId === instance.id
+ );
+ const instancePropNames = new Set(instanceProps.map((prop) => prop.name));
+ const projectedResourceProps = new Map<string, unknown>();
- for (const prop of props.values()) {
- if (prop.instanceId !== instance.id) {
- continue;
+ const getGeneratedPropName = (name: string) => {
+ if (hasTags && !meta?.props?.[name]) {
+ return standardAttributesToReactProps[name] ?? name;
}
+ return name;
+ };
+ for (const prop of instanceProps) {
const propValue = generatePropValue({
scope,
prop,
@@ -210,13 +221,24 @@ export const generateJsxElement = ({
if (isAttributeNameSafe(prop.name) === false) {
continue;
}
- let name = prop.name;
- // convert html attribute only when component has tags
- // and does not specify own property with this name
- if (hasTags && !meta?.props?.[prop.name]) {
- name = standardAttributesToReactProps[prop.name] ?? prop.name;
+
+ if (prop.type === "resource") {
+ const propMeta = meta?.props?.[prop.name];
+ const resource = resources?.get(prop.value);
+ if (propMeta?.type === "resource" && resource !== undefined) {
+ for (const name of propMeta.generatedProps ?? []) {
+ if (
+ instancePropNames.has(name) === false &&
+ isAttributeNameSafe(name)
+ ) {
+ projectedResourceProps.set(name, resource[name]);
+ }
+ }
+ }
}
+ const name = getGeneratedPropName(prop.name);
+
// show prop controls conditional rendering and need to be handled separately
if (prop.name === showAttribute) {
// prevent generating unnecessary condition
@@ -258,6 +280,11 @@ export const generateJsxElement = ({
}
}
+ for (const [propName, value] of projectedResourceProps) {
+ const name = getGeneratedPropName(propName);
+ generatedProps += `\n${name}={${JSON.stringify(value)}}`;
+ }
+
const classMapArray = classesMap?.get(instance.id);
if (classMapArray || classNameValue) {
let classNameTemplate = classMapArray ? classMapArray.join(" ") : "";
@@ -365,6 +392,7 @@ export const generateJsxChildren = ({
children,
instances,
props,
+ resources,
dataSources,
usedDataSources,
indexesWithinAncestors,
@@ -378,6 +406,7 @@ export const generateJsxChildren = ({
children: Instance["children"];
instances: Instances;
props: Props;
+ resources?: Resources;
dataSources: DataSources;
usedDataSources: DataSources;
indexesWithinAncestors: IndexesWithinAncestors;
@@ -421,6 +450,7 @@ export const generateJsxChildren = ({
tagsOverrides,
instance,
props,
+ resources,
dataSources,
usedDataSources,
indexesWithinAncestors,
@@ -433,6 +463,7 @@ export const generateJsxChildren = ({
children: instance.children,
instances,
props,
+ resources,
dataSources,
usedDataSources,
indexesWithinAncestors,
@@ -453,6 +484,7 @@ export const generateWebstudioComponent = ({
parameters,
instances,
props,
+ resources,
dataSources,
metas,
tagsOverrides,
@@ -464,6 +496,7 @@ export const generateWebstudioComponent = ({
parameters: Extract<Prop, { type: "parameter" }>[];
instances: Instances;
props: Props;
+ resources?: Resources;
dataSources: DataSources;
classesMap: Map<string, Array<string>>;
metas: Map<Instance["component"], WsComponentMeta>;
@@ -488,6 +521,7 @@ export const generateWebstudioComponent = ({
tagsOverrides,
instance,
props,
+ resources,
dataSources,
usedDataSources,
indexesWithinAncestors,
@@ -499,6 +533,7 @@ export const generateWebstudioComponent = ({
children: instance.children,
instances,
props,
+ resources,
dataSources,
usedDataSources,
indexesWithinAncestors,
diff --git a/packages/sdk-components-react/src/webhook-form.ws.ts b/packages/sdk-components-react/src/webhook-form.ws.ts
index 06954343fd..01412c8777 100644
--- a/packages/sdk-components-react/src/webhook-form.ws.ts
+++ b/packages/sdk-components-react/src/webhook-form.ws.ts
@@ -22,6 +22,7 @@ export const meta: WsComponentMeta = {
description:
"The URI of a program that processes the information submitted via the form.",
required: false,
+ generatedProps: ["method"],
},
},
};
diff --git a/packages/sdk/src/schema/component-meta.test.ts b/packages/sdk/src/schema/component-meta.test.ts
index 73232f6b2a..95acb4857f 100644
--- a/packages/sdk/src/schema/component-meta.test.ts
+++ b/packages/sdk/src/schema/component-meta.test.ts
@@ -2,6 +2,7 @@ import { expect, test } from "vitest";
import {
normalizeComponentCategory,
shouldFilterComponentCategory,
+ wsComponentMeta,
} from "./component-meta";
test("treats omitted component category as hidden", () => {
@@ -14,3 +15,19 @@ test("filters hidden and internal component categories", () => {
expect(shouldFilterComponentCategory("internal")).toBe(true);
expect(shouldFilterComponentCategory("general")).toBe(false);
});
+
+test("allows only safe resource fields in generated component props", () => {
+ const createMeta = (field: string) => ({
+ props: {
+ action: {
+ control: "resource",
+ type: "resource",
+ required: false,
+ generatedProps: [field],
+ },
+ },
+ });
+
+ expect(wsComponentMeta.safeParse(createMeta("method")).success).toBe(true);
+ expect(wsComponentMeta.safeParse(createMeta("url")).success).toBe(false);
+});
diff --git a/packages/sdk/src/schema/prop-meta.ts b/packages/sdk/src/schema/prop-meta.ts
index e3b78574c1..a64369f8ad 100644
--- a/packages/sdk/src/schema/prop-meta.ts
+++ b/packages/sdk/src/schema/prop-meta.ts
@@ -56,6 +56,8 @@ const resource = z.object({
control: z.literal("resource"),
type: z.literal("resource"),
defaultValue: z.string().optional(),
+ // Safe resource fields copied to same-named props in generated components.
+ generatedProps: z.array(z.enum(["method"])).optional(),
});
const code = z.object({