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.
TypeScript accessibilityalternativecloudflarecloudflare-workersdesignfreefrontendopen-sourceradix-uireactremixweb-developmentwebflowwebsite-builderwebsite-developmentwebsiteswebstudio
refactor: store webhook form action as resource (#4983)
Bogdan Chadkin8b526dc7e918
diff --git a/apps/builder/app/builder/features/settings-panel/controls/combined.tsx b/apps/builder/app/builder/features/settings-panel/controls/combined.tsxindex 418f367d2c..2d562661f2 100644--- a/apps/builder/app/builder/features/settings-panel/controls/combined.tsx+++ b/apps/builder/app/builder/features/settings-panel/controls/combined.tsx@@ -10,6 +10,7 @@ import { UrlControl } from "./url";import type { ControlProps } from "../shared";import { JsonControl } from "./json";import { TextContent } from "./text-content";+import { ResourceControl } from "./resource-control";export const renderControl = ({meta,@@ -45,6 +46,10 @@ export const renderControl = ({return <TextControl key={key} meta={meta} prop={prop} {...rest} />;}+ if (meta.control === "resource") {+ return <ResourceControl key={key} meta={meta} prop={prop} {...rest} />;+ }+if (meta.control === "code") {return <CodeControl key={key} meta={meta} prop={prop} {...rest} />;}diff --git a/apps/builder/app/builder/features/settings-panel/controls/resource-control.tsx b/apps/builder/app/builder/features/settings-panel/controls/resource-control.tsxnew file mode 100644index 0000000000..d50b8b3089--- /dev/null+++ b/apps/builder/app/builder/features/settings-panel/controls/resource-control.tsx@@ -0,0 +1,161 @@+import { useId } from "react";+import { useStore } from "@nanostores/react";+import { InputField } from "@webstudio-is/design-system";+import { isLiteralExpression, Resource, type Prop } from "@webstudio-is/sdk";+import {+ BindingControl,+ BindingPopover,+ type BindingVariant,+} from "~/builder/shared/binding-popover";+import {+ type ControlProps,+ useLocalValue,+ ResponsiveLayout,+ Label,+ humanizeAttribute,+} from "../shared";+import { $resources } from "~/shared/nano-states";+import { $selectedInstanceResourceScope } from "../resource-panel";+import { computeExpression } from "~/shared/data-variables";+import { updateWebstudioData } from "~/shared/instance-utils";+import { nanoid } from "nanoid";++export const ResourceControl = ({+ meta,+ prop,+ propName,+ instanceId,+ deletable,+}: ControlProps<"resource">) => {+ const resources = useStore($resources);+ const { variableValues, scope, aliases } = useStore(+ $selectedInstanceResourceScope+ );++ let computedValue: unknown;+ let expression: string = JSON.stringify("");+ if (prop?.type === "string") {+ expression = JSON.stringify(prop.value);+ computedValue = prop.value;+ }+ if (prop?.type === "expression") {+ expression = prop.value;+ computedValue = computeExpression(prop.value, variableValues);+ }+ if (prop?.type === "resource") {+ const resource = resources.get(prop.value);+ if (resource) {+ expression = resource.url;+ computedValue = computeExpression(resource.url, variableValues);+ }+ }++ const updateResourceUrl = (urlExpression: string) => {+ updateWebstudioData((data) => {+ if (prop?.type === "resource") {+ const resource = data.resources.get(prop.value);+ if (resource) {+ resource.url = urlExpression;+ }+ } else {+ let method: Resource["method"] = "post";+ for (const prop of data.props.values()) {+ if (+ prop.instanceId === instanceId &&+ prop.type === "string" &&+ prop.name === "method"+ ) {+ const value = prop.value.toLowerCase();+ if (+ value === "get" ||+ value === "post" ||+ value === "put" ||+ value === "delete"+ ) {+ method = value;+ }+ break;+ }+ }++ const newResource: Resource = {+ id: nanoid(),+ name: propName,+ url: urlExpression,+ method,+ headers: [{ name: "Content-Type", value: `"application/json"` }],+ };+ const newProp: Prop = {+ id: prop?.id ?? nanoid(),+ instanceId,+ name: propName,+ type: "resource",+ value: newResource.id,+ };+ data.props.set(newProp.id, newProp);+ data.resources.set(newResource.id, newResource);+ }+ });+ };++ const deletePropAndResource = () => {+ updateWebstudioData((data) => {+ if (prop?.type === "resource") {+ data.resources.delete(prop.value);+ }+ if (prop) {+ data.props.delete(prop.id);+ }+ });+ };++ const id = useId();+ const label = humanizeAttribute(meta.label || propName);+ let variant: BindingVariant = "bound";+ let readOnly = true;+ if (isLiteralExpression(expression)) {+ variant = "default";+ readOnly = false;+ }+ const localValue = useLocalValue(String(computedValue ?? ""), (value) =>+ updateResourceUrl(JSON.stringify(value))+ );++ return (+ <ResponsiveLayout+ label={+ <Label htmlFor={id} description={meta.description} readOnly={readOnly}>+ {label}+ </Label>+ }+ deletable={deletable}+ onDelete={deletePropAndResource}+ >+ <BindingControl>+ <InputField+ id={id}+ disabled={readOnly}+ value={localValue.value}+ onChange={(event) => localValue.set(event.target.value)}+ onBlur={localValue.save}+ onSubmit={localValue.save}+ />+ <BindingPopover+ scope={scope}+ aliases={aliases}+ validate={(value) => {+ if (value !== undefined && typeof value !== "string") {+ return `${label} expects a string value`;+ }+ }}+ variant={variant}+ value={expression}+ onChange={(newExpression) => updateResourceUrl(newExpression)}+ onRemove={(evaluatedValue) =>+ updateResourceUrl(JSON.stringify(String(evaluatedValue)))+ }+ />+ </BindingControl>+ </ResponsiveLayout>+ );+};diff --git a/apps/builder/app/builder/features/settings-panel/controls/text.tsx b/apps/builder/app/builder/features/settings-panel/controls/text.tsxindex 638c4975cc..adb3bc8d32 100644--- a/apps/builder/app/builder/features/settings-panel/controls/text.tsx+++ b/apps/builder/app/builder/features/settings-panel/controls/text.tsx@@ -1,4 +1,4 @@-import { useEffect, useId, useRef } from "react";+import { useId } from "react";import { useStore } from "@nanostores/react";import { TextArea } from "@webstudio-is/design-system";import {@@ -22,7 +22,6 @@ export const TextControl = ({propName,deletable,computedValue,- autoFocus,onChange,onDelete,}: ControlProps<"text">) => {@@ -35,7 +34,6 @@ export const TextControl = ({});const id = useId();const label = humanizeAttribute(meta.label || propName);- const textAreaRef = useRef<HTMLTextAreaElement>(null);const { scope, aliases } = useStore($selectedInstanceScope);const expression =prop?.type === "expression" ? prop.value : JSON.stringify(computedValue);@@ -43,16 +41,9 @@ export const TextControl = ({prop?.type === "expression" ? prop.value : undefined);- useEffect(() => {- if (autoFocus) {- textAreaRef.current?.focus();- }- }, [autoFocus]);-const input = (<BindingControl><TextArea- ref={textAreaRef}id={id}disabled={overwritable === false}autoGrowdiff --git a/apps/builder/app/builder/features/settings-panel/props-section/props-section.tsx b/apps/builder/app/builder/features/settings-panel/props-section/props-section.tsxindex f1c7719e14..e712b7a401 100644--- a/apps/builder/app/builder/features/settings-panel/props-section/props-section.tsx+++ b/apps/builder/app/builder/features/settings-panel/props-section/props-section.tsx@@ -59,10 +59,9 @@ const matchOrSuggestToCreate = (const renderProperty = ({ propsLogic: logic, propValues, component, instanceId }: PropsSectionProps,{ prop, propName, meta }: PropAndMeta,- { deletable, autoFocus }: { deletable?: boolean; autoFocus?: boolean } = {}+ { deletable }: { deletable?: boolean } = {}) =>renderControl({- autoFocus,key: propName,instanceId,meta,diff --git a/apps/builder/app/builder/features/settings-panel/resource-panel.tsx b/apps/builder/app/builder/features/settings-panel/resource-panel.tsxindex 0d045490f8..e24506ee92 100644--- a/apps/builder/app/builder/features/settings-panel/resource-panel.tsx+++ b/apps/builder/app/builder/features/settings-panel/resource-panel.tsx@@ -13,7 +13,7 @@ import {import { useStore } from "@nanostores/react";import type { DataSource, Resource } from "@webstudio-is/sdk";import {- encodeDataSourceVariable,+ encodeDataVariableId,generateObjectExpression,isLiteralExpression,parseObjectExpression,@@ -383,7 +383,7 @@ const $hiddenDataSourceIds = computed(});-const $selectedInstanceScope = computed(+export const $selectedInstanceResourceScope = computed([$selectedInstanceKeyWithRoot,$variableValuesByInstanceSelector,@@ -398,8 +398,9 @@ const $selectedInstanceScope = computed() => {const scope: Record<string, unknown> = {};const aliases = new Map<string, string>();+ const variableValues = new Map<DataSource["id"], unknown>();if (instanceKey === undefined) {- return { scope, aliases };+ return { variableValues, scope, aliases };}const values = variableValuesByInstanceSelector.get(instanceKey);if (values) {@@ -411,21 +412,21 @@ const $selectedInstanceScope = computed(if (dataSourceId === SYSTEM_VARIABLE_ID) {dataSource = systemParameter;}- if (dataSource === undefined) {- continue;+ if (dataSource) {+ const name = encodeDataVariableId(dataSourceId);+ variableValues.set(dataSourceId, value);+ scope[name] = value;+ aliases.set(name, dataSource.name);}- const name = encodeDataSourceVariable(dataSourceId);- scope[name] = value;- aliases.set(name, dataSource.name);}}- return { scope, aliases };+ return { variableValues, scope, aliases };});const useScope = ({ variable }: { variable?: DataSource }) => {const { scope: scopeWithCurrentVariable, aliases } = useStore(- $selectedInstanceScope+ $selectedInstanceResourceScope);const currentVariableId = variable?.id;// prevent showing currently edited variable in suggestions@@ -435,7 +436,7 @@ const useScope = ({ variable }: { variable?: DataSource }) => {return scopeWithCurrentVariable;}const newScope: Record<string, unknown> = { ...scopeWithCurrentVariable };- delete newScope[encodeDataSourceVariable(currentVariableId)];+ delete newScope[encodeDataVariableId(currentVariableId)];return newScope;}, [scopeWithCurrentVariable, currentVariableId]);return { scope, aliases };diff --git a/apps/builder/app/builder/features/settings-panel/shared.tsx b/apps/builder/app/builder/features/settings-panel/shared.tsxindex 1d52b92b85..14f93401b0 100644--- a/apps/builder/app/builder/features/settings-panel/shared.tsx+++ b/apps/builder/app/builder/features/settings-panel/shared.tsx@@ -72,7 +72,6 @@ export type ControlProps<Control> = {deletable: boolean;onChange: (value: PropValue) => void;onDelete: () => void;- autoFocus?: boolean;};export const RemovePropButton = (props: { onClick: () => void }) => (diff --git a/apps/builder/app/shared/nano-states/props.ts b/apps/builder/app/shared/nano-states/props.tsindex f8efa10e44..365694f6cc 100644--- a/apps/builder/app/shared/nano-states/props.ts+++ b/apps/builder/app/shared/nano-states/props.ts@@ -570,11 +570,18 @@ const computeResource = (};const $computedResources = computed(- [$resources, $loaderVariableValues],- (resources, values) => {+ [$dataSources, $resources, $loaderVariableValues],+ (dataSources, resources, values) => {const computedResources: ResourceRequest[] = [];- for (const resource of resources.values()) {- computedResources.push(computeResource(resource, values));+ // load only resources bound to variables+ // action resources should not be loaded automatically+ for (const dataSource of dataSources.values()) {+ if (dataSource.type === "resource") {+ const resource = resources.get(dataSource.resourceId);+ if (resource) {+ computedResources.push(computeResource(resource, values));+ }+ }}return computedResources;}diff --git a/fixtures/react-router-docker/app/routes/[another-page]._index.tsx b/fixtures/react-router-docker/app/routes/[another-page]._index.tsxindex 5525a2b19c..f60ab4be24 100644--- a/fixtures/react-router-docker/app/routes/[another-page]._index.tsx+++ b/fixtures/react-router-docker/app/routes/[another-page]._index.tsx@@ -231,10 +231,6 @@ export const action = async ({formData.delete(formBotFieldName);if (resource) {- resource.headers.push({- name: "Content-Type",- value: "application/json",- });resource.body = Object.fromEntries(formData);} else {if (contactEmail === undefined) {diff --git a/fixtures/react-router-docker/app/routes/_index.tsx b/fixtures/react-router-docker/app/routes/_index.tsxindex 03d6d801da..fbb3ae46b8 100644--- a/fixtures/react-router-docker/app/routes/_index.tsx+++ b/fixtures/react-router-docker/app/routes/_index.tsx@@ -231,10 +231,6 @@ export const action = async ({formData.delete(formBotFieldName);if (resource) {- resource.headers.push({- name: "Content-Type",- value: "application/json",- });resource.body = Object.fromEntries(formData);} else {if (contactEmail === undefined) {diff --git a/fixtures/react-router-netlify/app/routes/[another-page]._index.tsx b/fixtures/react-router-netlify/app/routes/[another-page]._index.tsxindex 5525a2b19c..f60ab4be24 100644--- a/fixtures/react-router-netlify/app/routes/[another-page]._index.tsx+++ b/fixtures/react-router-netlify/app/routes/[another-page]._index.tsx@@ -231,10 +231,6 @@ export const action = async ({formData.delete(formBotFieldName);if (resource) {- resource.headers.push({- name: "Content-Type",- value: "application/json",- });resource.body = Object.fromEntries(formData);} else {if (contactEmail === undefined) {diff --git a/fixtures/react-router-netlify/app/routes/_index.tsx b/fixtures/react-router-netlify/app/routes/_index.tsxindex 03d6d801da..fbb3ae46b8 100644--- a/fixtures/react-router-netlify/app/routes/_index.tsx+++ b/fixtures/react-router-netlify/app/routes/_index.tsx@@ -231,10 +231,6 @@ export const action = async ({formData.delete(formBotFieldName);if (resource) {- resource.headers.push({- name: "Content-Type",- value: "application/json",- });resource.body = Object.fromEntries(formData);} else {if (contactEmail === undefined) {diff --git a/fixtures/react-router-vercel/app/routes/[another-page]._index.tsx b/fixtures/react-router-vercel/app/routes/[another-page]._index.tsxindex 5525a2b19c..f60ab4be24 100644--- a/fixtures/react-router-vercel/app/routes/[another-page]._index.tsx+++ b/fixtures/react-router-vercel/app/routes/[another-page]._index.tsx@@ -231,10 +231,6 @@ export const action = async ({formData.delete(formBotFieldName);if (resource) {- resource.headers.push({- name: "Content-Type",- value: "application/json",- });resource.body = Object.fromEntries(formData);} else {if (contactEmail === undefined) {diff --git a/fixtures/react-router-vercel/app/routes/_index.tsx b/fixtures/react-router-vercel/app/routes/_index.tsxindex 03d6d801da..fbb3ae46b8 100644--- a/fixtures/react-router-vercel/app/routes/_index.tsx+++ b/fixtures/react-router-vercel/app/routes/_index.tsx@@ -231,10 +231,6 @@ export const action = async ({formData.delete(formBotFieldName);if (resource) {- resource.headers.push({- name: "Content-Type",- value: "application/json",- });resource.body = Object.fromEntries(formData);} else {if (contactEmail === undefined) {diff --git a/fixtures/webstudio-cloudflare-template/app/routes/[another-page]._index.tsx b/fixtures/webstudio-cloudflare-template/app/routes/[another-page]._index.tsxindex aebd63e3d9..a8e99d49bc 100644--- a/fixtures/webstudio-cloudflare-template/app/routes/[another-page]._index.tsx+++ b/fixtures/webstudio-cloudflare-template/app/routes/[another-page]._index.tsx@@ -232,10 +232,6 @@ export const action = async ({formData.delete(formBotFieldName);if (resource) {- resource.headers.push({- name: "Content-Type",- value: "application/json",- });resource.body = Object.fromEntries(formData);} else {if (contactEmail === undefined) {diff --git a/fixtures/webstudio-cloudflare-template/app/routes/_index.tsx b/fixtures/webstudio-cloudflare-template/app/routes/_index.tsxindex 9924147fb4..beb3ec3795 100644--- a/fixtures/webstudio-cloudflare-template/app/routes/_index.tsx+++ b/fixtures/webstudio-cloudflare-template/app/routes/_index.tsx@@ -232,10 +232,6 @@ export const action = async ({formData.delete(formBotFieldName);if (resource) {- resource.headers.push({- name: "Content-Type",- value: "application/json",- });resource.body = Object.fromEntries(formData);} else {if (contactEmail === undefined) {diff --git a/fixtures/webstudio-features/app/__generated__/[form]._index.server.tsx b/fixtures/webstudio-features/app/__generated__/[form]._index.server.tsxindex de88d2e4ca..7cc6ef94b5 100644--- a/fixtures/webstudio-features/app/__generated__/[form]._index.server.tsx+++ b/fixtures/webstudio-features/app/__generated__/[form]._index.server.tsx@@ -9,7 +9,7 @@ export const getResources = (_props: { system: System }) => {name: "action",url: "/custom",method: "get",- headers: [],+ headers: [{ name: "Content-Type", value: "application/json" }],};const _data = new Map<string, ResourceRequest>([]);const _action = new Map<string, ResourceRequest>([["action", action]]);diff --git a/fixtures/webstudio-features/app/routes/[_route_with_symbols_]._index.tsx b/fixtures/webstudio-features/app/routes/[_route_with_symbols_]._index.tsxindex 92e76fe481..ad1cf82b28 100644--- a/fixtures/webstudio-features/app/routes/[_route_with_symbols_]._index.tsx+++ b/fixtures/webstudio-features/app/routes/[_route_with_symbols_]._index.tsx@@ -231,10 +231,6 @@ export const action = async ({formData.delete(formBotFieldName);if (resource) {- resource.headers.push({- name: "Content-Type",- value: "application/json",- });resource.body = Object.fromEntries(formData);} else {if (contactEmail === undefined) {diff --git a/fixtures/webstudio-features/app/routes/[animations]._index.tsx b/fixtures/webstudio-features/app/routes/[animations]._index.tsxindex 0d0eb765a7..60ead01f20 100644--- a/fixtures/webstudio-features/app/routes/[animations]._index.tsx+++ b/fixtures/webstudio-features/app/routes/[animations]._index.tsx@@ -231,10 +231,6 @@ export const action = async ({formData.delete(formBotFieldName);if (resource) {- resource.headers.push({- name: "Content-Type",- value: "application/json",- });resource.body = Object.fromEntries(formData);} else {if (contactEmail === undefined) {diff --git a/fixtures/webstudio-features/app/routes/[class-names]._index.tsx b/fixtures/webstudio-features/app/routes/[class-names]._index.tsxindex e7507b8758..f935be1d30 100644--- a/fixtures/webstudio-features/app/routes/[class-names]._index.tsx+++ b/fixtures/webstudio-features/app/routes/[class-names]._index.tsx@@ -231,10 +231,6 @@ export const action = async ({formData.delete(formBotFieldName);if (resource) {- resource.headers.push({- name: "Content-Type",- value: "application/json",- });resource.body = Object.fromEntries(formData);} else {if (contactEmail === undefined) {diff --git a/fixtures/webstudio-features/app/routes/[content-block]._index.tsx b/fixtures/webstudio-features/app/routes/[content-block]._index.tsxindex 59e1f4fdf7..4059b7aaaf 100644--- a/fixtures/webstudio-features/app/routes/[content-block]._index.tsx+++ b/fixtures/webstudio-features/app/routes/[content-block]._index.tsx@@ -231,10 +231,6 @@ export const action = async ({formData.delete(formBotFieldName);if (resource) {- resource.headers.push({- name: "Content-Type",- value: "application/json",- });resource.body = Object.fromEntries(formData);} else {if (contactEmail === undefined) {diff --git a/fixtures/webstudio-features/app/routes/[expressions]._index.tsx b/fixtures/webstudio-features/app/routes/[expressions]._index.tsxindex ae5fd429f6..b5b1a9ff3f 100644--- a/fixtures/webstudio-features/app/routes/[expressions]._index.tsx+++ b/fixtures/webstudio-features/app/routes/[expressions]._index.tsx@@ -231,10 +231,6 @@ export const action = async ({formData.delete(formBotFieldName);if (resource) {- resource.headers.push({- name: "Content-Type",- value: "application/json",- });resource.body = Object.fromEntries(formData);} else {if (contactEmail === undefined) {diff --git a/fixtures/webstudio-features/app/routes/[form]._index.tsx b/fixtures/webstudio-features/app/routes/[form]._index.tsxindex 8d5d1b4766..55e7a6e02e 100644--- a/fixtures/webstudio-features/app/routes/[form]._index.tsx+++ b/fixtures/webstudio-features/app/routes/[form]._index.tsx@@ -231,10 +231,6 @@ export const action = async ({formData.delete(formBotFieldName);if (resource) {- resource.headers.push({- name: "Content-Type",- value: "application/json",- });resource.body = Object.fromEntries(formData);} else {if (contactEmail === undefined) {diff --git a/fixtures/webstudio-features/app/routes/[head-tag]._index.tsx b/fixtures/webstudio-features/app/routes/[head-tag]._index.tsxindex 549111e7bc..5ac97ec184 100644--- a/fixtures/webstudio-features/app/routes/[head-tag]._index.tsx+++ b/fixtures/webstudio-features/app/routes/[head-tag]._index.tsx@@ -231,10 +231,6 @@ export const action = async ({formData.delete(formBotFieldName);if (resource) {- resource.headers.push({- name: "Content-Type",- value: "application/json",- });resource.body = Object.fromEntries(formData);} else {if (contactEmail === undefined) {diff --git a/fixtures/webstudio-features/app/routes/[heading-with-id]._index.tsx b/fixtures/webstudio-features/app/routes/[heading-with-id]._index.tsxindex 5b3213fab3..9137ef842d 100644--- a/fixtures/webstudio-features/app/routes/[heading-with-id]._index.tsx+++ b/fixtures/webstudio-features/app/routes/[heading-with-id]._index.tsx@@ -231,10 +231,6 @@ export const action = async ({formData.delete(formBotFieldName);if (resource) {- resource.headers.push({- name: "Content-Type",- value: "application/json",- });resource.body = Object.fromEntries(formData);} else {if (contactEmail === undefined) {diff --git a/fixtures/webstudio-features/app/routes/[nested].[nested-page]._index.tsx b/fixtures/webstudio-features/app/routes/[nested].[nested-page]._index.tsxindex 45b1b01cd1..96d97fac8a 100644--- a/fixtures/webstudio-features/app/routes/[nested].[nested-page]._index.tsx+++ b/fixtures/webstudio-features/app/routes/[nested].[nested-page]._index.tsx@@ -231,10 +231,6 @@ export const action = async ({formData.delete(formBotFieldName);if (resource) {- resource.headers.push({- name: "Content-Type",- value: "application/json",- });resource.body = Object.fromEntries(formData);} else {if (contactEmail === undefined) {diff --git a/fixtures/webstudio-features/app/routes/[radix]._index.tsx b/fixtures/webstudio-features/app/routes/[radix]._index.tsxindex 34c2583827..7368832976 100644--- a/fixtures/webstudio-features/app/routes/[radix]._index.tsx+++ b/fixtures/webstudio-features/app/routes/[radix]._index.tsx@@ -231,10 +231,6 @@ export const action = async ({formData.delete(formBotFieldName);if (resource) {- resource.headers.push({- name: "Content-Type",- value: "application/json",- });resource.body = Object.fromEntries(formData);} else {if (contactEmail === undefined) {diff --git a/fixtures/webstudio-features/app/routes/[resources]._index.tsx b/fixtures/webstudio-features/app/routes/[resources]._index.tsxindex 0fbb97f533..e128e619aa 100644--- a/fixtures/webstudio-features/app/routes/[resources]._index.tsx+++ b/fixtures/webstudio-features/app/routes/[resources]._index.tsx@@ -231,10 +231,6 @@ export const action = async ({formData.delete(formBotFieldName);if (resource) {- resource.headers.push({- name: "Content-Type",- value: "application/json",- });resource.body = Object.fromEntries(formData);} else {if (contactEmail === undefined) {diff --git a/fixtures/webstudio-features/app/routes/_index.tsx b/fixtures/webstudio-features/app/routes/_index.tsxindex 03d6d801da..fbb3ae46b8 100644--- a/fixtures/webstudio-features/app/routes/_index.tsx+++ b/fixtures/webstudio-features/app/routes/_index.tsx@@ -231,10 +231,6 @@ export const action = async ({formData.delete(formBotFieldName);if (resource) {- resource.headers.push({- name: "Content-Type",- value: "application/json",- });resource.body = Object.fromEntries(formData);} else {if (contactEmail === undefined) {diff --git a/packages/cli/src/prebuild.ts b/packages/cli/src/prebuild.tsindex 0a51fd24c4..dcfc94c4a8 100644--- a/packages/cli/src/prebuild.ts+++ b/packages/cli/src/prebuild.ts@@ -301,6 +301,16 @@ export const prebuild = async (options: {const fontAssetsByPage: Record<Page["id"], string[]> = {};const backgroundImageAssetsByPage: Record<Page["id"], string[]> = {};+ // use whole project props to access id props from other pages+ const normalizedProps = normalizeProps({+ props: siteData.build.props.map(([_id, prop]) => prop),+ assetBaseUrl,+ assets: new Map(siteData.assets.map((asset) => [asset.id, asset])),+ uploadingImageAssets: [],+ pages: siteData.build.pages,+ source: "prebuild",+ });+for (const page of Object.values(siteData.pages)) {const instanceMap = new Map(siteData.build.instances);const pageInstanceSet = findTreeInstanceIds(@@ -309,33 +319,33 @@ export const prebuild = async (options: {);// support global data variablespageInstanceSet.add(ROOT_INSTANCE_ID);- const instances: [Instance["id"], Instance][] =- siteData.build.instances.filter(([id]) => pageInstanceSet.has(id));- const dataSources: [DataSource["id"], DataSource][] = [];+ // collect used instances and metas+ const instances: [Instance["id"], Instance][] = [];+ for (const [_instanceId, instance] of siteData.build.instances) {+ if (pageInstanceSet.has(instance.id)) {+ instances.push([instance.id, instance]);+ const meta = metas.get(instance.component);+ if (meta) {+ usedMetas.set(instance.component, meta);+ }+ }+ }- // use whole project props to access id props from other pages- const normalizedProps = normalizeProps({- props: siteData.build.props.map(([_id, prop]) => prop),- assetBaseUrl,- assets: new Map(siteData.assets.map((asset) => [asset.id, asset])),- uploadingImageAssets: [],- pages: siteData.build.pages,- source: "prebuild",- });+ const resourceIds = new Set<Resource["id"]>();const props: [Prop["id"], Prop][] = [];for (const prop of normalizedProps) {if (pageInstanceSet.has(prop.instanceId)) {props.push([prop.id, prop]);+ if (prop.type === "resource") {+ resourceIds.add(prop.value);+ }}}- const resourceIds = new Set<Resource["id"]>();+ const dataSources: [DataSource["id"], DataSource][] = [];for (const [dataSourceId, dataSource] of siteData.build.dataSources) {- if (- dataSource.scopeInstanceId === undefined ||- pageInstanceSet.has(dataSource.scopeInstanceId)- ) {+ if (pageInstanceSet.has(dataSource.scopeInstanceId ?? "")) {dataSources.push([dataSourceId, dataSource]);if (dataSource.type === "resource") {resourceIds.add(dataSource.resourceId);@@ -362,13 +372,6 @@ export const prebuild = async (options: {assets: siteData.assets,};- for (const [_instanceId, instance] of instances) {- const meta = metas.get(instance.component);- if (meta) {- usedMetas.set(instance.component, meta);- }- }-// Extract background SVGs and Font assetsconst styleSourceSelections = siteData.build?.styleSourceSelections ?? [];const pageStyleSourceIds = new Set(diff --git a/packages/cli/templates/defaults/app/route-templates/html.tsx b/packages/cli/templates/defaults/app/route-templates/html.tsxindex f7e85bb18b..58beeac5a3 100644--- a/packages/cli/templates/defaults/app/route-templates/html.tsx+++ b/packages/cli/templates/defaults/app/route-templates/html.tsx@@ -232,10 +232,6 @@ export const action = async ({formData.delete(formBotFieldName);if (resource) {- resource.headers.push({- name: "Content-Type",- value: "application/json",- });resource.body = Object.fromEntries(formData);} else {if (contactEmail === undefined) {diff --git a/packages/cli/templates/react-router/app/route-templates/html.tsx b/packages/cli/templates/react-router/app/route-templates/html.tsxindex 73b2cc3b11..39be97fc53 100644--- a/packages/cli/templates/react-router/app/route-templates/html.tsx+++ b/packages/cli/templates/react-router/app/route-templates/html.tsx@@ -231,10 +231,6 @@ export const action = async ({formData.delete(formBotFieldName);if (resource) {- resource.headers.push({- name: "Content-Type",- value: "application/json",- });resource.body = Object.fromEntries(formData);} else {if (contactEmail === undefined) {diff --git a/packages/sdk-components-react/src/webhook-form.ws.ts b/packages/sdk-components-react/src/webhook-form.ws.tsindex a0c2c337a2..dd618060fe 100644--- a/packages/sdk-components-react/src/webhook-form.ws.ts+++ b/packages/sdk-components-react/src/webhook-form.ws.ts@@ -21,6 +21,15 @@ export const meta: WsComponentMeta = {};export const propsMeta: WsComponentPropsMeta = {- props,+ props: {+ ...props,+ action: {+ type: "resource",+ control: "resource",+ description:+ "The URI of a program that processes the information submitted via the form.",+ required: true,+ },+ },initialProps: ["id", "className", "state", "action"],};diff --git a/packages/sdk/src/resources-generator.test.tsx b/packages/sdk/src/resources-generator.test.tsxindex 4f3dca51d9..28dc27d132 100644--- a/packages/sdk/src/resources-generator.test.tsx+++ b/packages/sdk/src/resources-generator.test.tsx@@ -385,7 +385,7 @@ test("replace form action with resource", () => {expect(data.resources).toEqual(toMap([{- headers: [],+ headers: [{ name: "Content-Type", value: `"application/json"` }],id: "formId",method: "post",name: "action",diff --git a/packages/sdk/src/resources-generator.ts b/packages/sdk/src/resources-generator.tsindex 7799e36ef0..d7871e3ac5 100644--- a/packages/sdk/src/resources-generator.ts+++ b/packages/sdk/src/resources-generator.ts@@ -184,7 +184,9 @@ export const replaceFormActionsWithResources = ({name: "action",method: getMethod(method),url: JSON.stringify(action),- headers: [],+ headers: [+ { name: "Content-Type", value: JSON.stringify("application/json") },+ ],});}}diff --git a/packages/sdk/src/schema/prop-meta.ts b/packages/sdk/src/schema/prop-meta.tsindex e19e9798df..a8bb252c6d 100644--- a/packages/sdk/src/schema/prop-meta.ts+++ b/packages/sdk/src/schema/prop-meta.ts@@ -42,6 +42,13 @@ const Text = z.object({rows: z.number().optional(),});+const Resource = z.object({+ ...common,+ control: z.literal("resource"),+ type: z.literal("resource"),+ defaultValue: z.string().optional(),+});+const Code = z.object({...common,control: z.literal("code"),@@ -178,6 +185,7 @@ export const PropMeta = z.union([Number,Range,Text,+ Resource,Code,CodeText,Color,