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: support GET forms in preview (#3202)

Bogdan Chadkind110cce1a639
diff --git a/apps/builder/app/canvas/interceptor.ts b/apps/builder/app/canvas/interceptor.ts
index 3b9103fbbd..32e1ddf7f8 100644
--- a/apps/builder/app/canvas/interceptor.ts
+++ b/apps/builder/app/canvas/interceptor.ts
@@ -1,6 +1,16 @@
-import { findPageByIdOrPath } from "@webstudio-is/sdk";
-import { matchPathnamePattern } from "~/builder/shared/url-pattern";
-import { $isPreviewMode, $pages, updateSystem } from "~/shared/nano-states";
+import type { System } from "@webstudio-is/sdk";
+import {
+ compilePathnamePattern,
+ matchPathnamePattern,
+ tokenizePathnamePattern,
+} from "~/builder/shared/url-pattern";
+import {
+ $dataSourceVariables,
+ $isPreviewMode,
+ $pages,
+ $selectedPage,
+ updateSystem,
+} from "~/shared/nano-states";
import { switchPage } from "~/shared/pages";
const isAbsoluteUrl = (href: string) => {
@@ -12,35 +22,47 @@ const isAbsoluteUrl = (href: string) => {
}
};
-const handleLinkClick = (element: HTMLAnchorElement) => {
- const pages = $pages.get();
- const href = element.getAttribute("href");
- if (href === null || pages === undefined) {
- return;
+const getSelectedPagePathname = () => {
+ const page = $selectedPage.get();
+ const dataSourceVariables = $dataSourceVariables.get();
+ if (page) {
+ const system = dataSourceVariables.get(page.systemDataSourceId) as
+ | undefined
+ | System;
+ const tokens = tokenizePathnamePattern(page.path);
+ return compilePathnamePattern(tokens, system?.params ?? {});
}
+};
- if (isAbsoluteUrl(href)) {
- window.open(href, "_blank");
+const switchPageAndUpdateSystem = (href: string, formData?: FormData) => {
+ const pages = $pages.get();
+ if (pages === undefined) {
return;
}
-
+ if (href === "") {
+ const pathname = getSelectedPagePathname();
+ if (pathname) {
+ href = pathname;
+ }
+ }
const pageHref = new URL(href, "https://any-valid.url");
for (const page of [pages.homePage, ...pages.pages]) {
// URL always parses root page as /
// but webstudio stores home page as empty string
const params = matchPathnamePattern(page.path || "/", pageHref.pathname);
if (params) {
+ // populate search params with form data values if available
+ if (formData) {
+ for (const [key, value] of formData.entries()) {
+ pageHref.searchParams.set(key, value.toString());
+ }
+ }
const search = Object.fromEntries(pageHref.searchParams);
switchPage(page.id, pageHref.hash);
updateSystem(page, { params, search });
break;
}
}
- const page = findPageByIdOrPath(pageHref.pathname, pages);
- if (page) {
- switchPage(page.id, pageHref.hash);
- return;
- }
};
export const subscribeInterceptedEvents = () => {
@@ -53,7 +75,13 @@ export const subscribeInterceptedEvents = () => {
if (a) {
event.preventDefault();
if ($isPreviewMode.get()) {
- handleLinkClick(a);
+ // use attribute instead of a.href to get raw unresolved value
+ const href = a.getAttribute("href") ?? "";
+ if (isAbsoluteUrl(href)) {
+ window.open(href, "_blank");
+ } else {
+ switchPageAndUpdateSystem(href);
+ }
}
}
// prevent invoking submit with buttons in canvas mode
@@ -63,10 +91,27 @@ export const subscribeInterceptedEvents = () => {
}
}
};
+
const handleSubmit = (event: SubmitEvent) => {
+ if ($isPreviewMode.get()) {
+ const form =
+ event.target instanceof HTMLFormElement ? event.target : undefined;
+ if (form === undefined) {
+ return;
+ }
+ // use attribute instead of form.action to get raw unresolved value
+ // https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#dom-fs-action
+ const action = form.getAttribute("action") ?? "";
+ // lower case just for safety
+ const method = form.method.toLowerCase();
+ if (method === "get" && isAbsoluteUrl(action) === false) {
+ switchPageAndUpdateSystem(action, new FormData(form));
+ }
+ }
// prevent submitting the form when clicking a button type submit
event.preventDefault();
};
+
const handleKeydown = (event: KeyboardEvent) => {
if ($isPreviewMode.get()) {
return;
diff --git a/packages/sdk-components-react-remix/src/remix-form.tsx b/packages/sdk-components-react-remix/src/remix-form.tsx
index 2092df9beb..bd2d9ac734 100644
--- a/packages/sdk-components-react-remix/src/remix-form.tsx
+++ b/packages/sdk-components-react-remix/src/remix-form.tsx
@@ -12,7 +12,11 @@ export const RemixForm = forwardRef<
method?: Lowercase<NonNullable<FormProps["method"]>>;
}
>((props, ref) => {
- return <Form {...props} ref={ref} />;
+ // remix casts action to relative url
+ if (props.action === "" || props.action?.startsWith("/")) {
+ return <Form {...props} ref={ref} />;
+ }
+ return <form {...props} ref={ref} />;
});
RemixForm.displayName = "Form";