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 search params in expressions (#3030)

Bogdan Chadkina5db17328841
diff --git a/apps/builder/app/builder/features/address-bar.tsx b/apps/builder/app/builder/features/address-bar.tsx
index 56e9ec353f..0e5c4f9d03 100644
--- a/apps/builder/app/builder/features/address-bar.tsx
+++ b/apps/builder/app/builder/features/address-bar.tsx
@@ -27,7 +27,7 @@ import {
$pages,
$project,
$selectedPage,
- updateSystemParams,
+ updateSystem,
} from "~/shared/nano-states";
import env from "~/shared/env";
import {
@@ -94,7 +94,7 @@ const updatePathParam = (name: string, value: string) => {
newParams[name] = value;
const page = $selectedPage.get();
if (page) {
- updateSystemParams(page, newParams);
+ updateSystem(page, { params: newParams });
}
};
diff --git a/apps/builder/app/canvas/interceptor.ts b/apps/builder/app/canvas/interceptor.ts
index 1edde7a572..3b9103fbbd 100644
--- a/apps/builder/app/canvas/interceptor.ts
+++ b/apps/builder/app/canvas/interceptor.ts
@@ -1,10 +1,6 @@
import { findPageByIdOrPath } from "@webstudio-is/sdk";
import { matchPathnamePattern } from "~/builder/shared/url-pattern";
-import {
- $isPreviewMode,
- $pages,
- updateSystemParams,
-} from "~/shared/nano-states";
+import { $isPreviewMode, $pages, updateSystem } from "~/shared/nano-states";
import { switchPage } from "~/shared/pages";
const isAbsoluteUrl = (href: string) => {
@@ -30,10 +26,13 @@ const handleLinkClick = (element: HTMLAnchorElement) => {
const pageHref = new URL(href, "https://any-valid.url");
for (const page of [pages.homePage, ...pages.pages]) {
- const params = matchPathnamePattern(page.path, pageHref.pathname);
+ // URL always parses root page as /
+ // but webstudio stores home page as empty string
+ const params = matchPathnamePattern(page.path || "/", pageHref.pathname);
if (params) {
+ const search = Object.fromEntries(pageHref.searchParams);
switchPage(page.id, pageHref.hash);
- updateSystemParams(page, params);
+ updateSystem(page, { params, search });
break;
}
}
diff --git a/apps/builder/app/shared/nano-states/nano-states.ts b/apps/builder/app/shared/nano-states/nano-states.ts
index 4a65594a0e..37227b5796 100644
--- a/apps/builder/app/shared/nano-states/nano-states.ts
+++ b/apps/builder/app/shared/nano-states/nano-states.ts
@@ -51,7 +51,7 @@ export const $dataSourceVariables = atom<Map<DataSource["id"], unknown>>(
new Map()
);
-export const updateSystemParams = (page: Page, params: System["params"]) => {
+export const updateSystem = (page: Page, update: Partial<System>) => {
if (page.systemDataSourceId === undefined) {
return;
}
@@ -59,11 +59,13 @@ export const updateSystemParams = (page: Page, params: System["params"]) => {
const system = dataSourceVariables.get(page.systemDataSourceId) as
| undefined
| System;
- dataSourceVariables.set(page.systemDataSourceId, {
+ const newSystem: System = {
search: {},
+ params: {},
...system,
- params: params,
- } satisfies System);
+ ...update,
+ };
+ dataSourceVariables.set(page.systemDataSourceId, newSystem);
$dataSourceVariables.set(dataSourceVariables);
};
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 bb4179d599..f5341b1de6 100644
--- a/fixtures/webstudio-custom-template/app/routes/[script-test]._index.tsx
+++ b/fixtures/webstudio-custom-template/app/routes/[script-test]._index.tsx
@@ -34,10 +34,11 @@ export type PageData = {
};
export const loader = async (arg: LoaderArgs) => {
+ const url = new URL(arg.request.url);
const params = getRemixParams(arg.params);
const system = {
params,
- search: {},
+ search: Object.fromEntries(url.searchParams),
};
const resources = await loadResources({ system });
const pageMeta = getPageMeta({ system, resources });
@@ -55,7 +56,6 @@ export const loader = async (arg: LoaderArgs) => {
arg.request.headers.get("host") ||
"";
- const url = new URL(arg.request.url);
url.host = host;
url.protocol = "https";
diff --git a/fixtures/webstudio-custom-template/app/routes/[world]._index.tsx b/fixtures/webstudio-custom-template/app/routes/[world]._index.tsx
index 71396cf0a6..24fa36b1f1 100644
--- a/fixtures/webstudio-custom-template/app/routes/[world]._index.tsx
+++ b/fixtures/webstudio-custom-template/app/routes/[world]._index.tsx
@@ -34,10 +34,11 @@ export type PageData = {
};
export const loader = async (arg: LoaderArgs) => {
+ const url = new URL(arg.request.url);
const params = getRemixParams(arg.params);
const system = {
params,
- search: {},
+ search: Object.fromEntries(url.searchParams),
};
const resources = await loadResources({ system });
const pageMeta = getPageMeta({ system, resources });
@@ -55,7 +56,6 @@ export const loader = async (arg: LoaderArgs) => {
arg.request.headers.get("host") ||
"";
- const url = new URL(arg.request.url);
url.host = host;
url.protocol = "https";
diff --git a/fixtures/webstudio-custom-template/app/routes/_index.tsx b/fixtures/webstudio-custom-template/app/routes/_index.tsx
index e0e6041985..4d787752c9 100644
--- a/fixtures/webstudio-custom-template/app/routes/_index.tsx
+++ b/fixtures/webstudio-custom-template/app/routes/_index.tsx
@@ -34,10 +34,11 @@ export type PageData = {
};
export const loader = async (arg: LoaderArgs) => {
+ const url = new URL(arg.request.url);
const params = getRemixParams(arg.params);
const system = {
params,
- search: {},
+ search: Object.fromEntries(url.searchParams),
};
const resources = await loadResources({ system });
const pageMeta = getPageMeta({ system, resources });
@@ -55,7 +56,6 @@ export const loader = async (arg: LoaderArgs) => {
arg.request.headers.get("host") ||
"";
- const url = new URL(arg.request.url);
url.host = host;
url.protocol = "https";
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 e0e6041985..4d787752c9 100644
--- a/fixtures/webstudio-remix-netlify-edge-functions/app/routes/_index.tsx
+++ b/fixtures/webstudio-remix-netlify-edge-functions/app/routes/_index.tsx
@@ -34,10 +34,11 @@ export type PageData = {
};
export const loader = async (arg: LoaderArgs) => {
+ const url = new URL(arg.request.url);
const params = getRemixParams(arg.params);
const system = {
params,
- search: {},
+ search: Object.fromEntries(url.searchParams),
};
const resources = await loadResources({ system });
const pageMeta = getPageMeta({ system, resources });
@@ -55,7 +56,6 @@ export const loader = async (arg: LoaderArgs) => {
arg.request.headers.get("host") ||
"";
- const url = new URL(arg.request.url);
url.host = host;
url.protocol = "https";
diff --git a/fixtures/webstudio-remix-netlify-functions/app/routes/_index.tsx b/fixtures/webstudio-remix-netlify-functions/app/routes/_index.tsx
index e0e6041985..4d787752c9 100644
--- a/fixtures/webstudio-remix-netlify-functions/app/routes/_index.tsx
+++ b/fixtures/webstudio-remix-netlify-functions/app/routes/_index.tsx
@@ -34,10 +34,11 @@ export type PageData = {
};
export const loader = async (arg: LoaderArgs) => {
+ const url = new URL(arg.request.url);
const params = getRemixParams(arg.params);
const system = {
params,
- search: {},
+ search: Object.fromEntries(url.searchParams),
};
const resources = await loadResources({ system });
const pageMeta = getPageMeta({ system, resources });
@@ -55,7 +56,6 @@ export const loader = async (arg: LoaderArgs) => {
arg.request.headers.get("host") ||
"";
- const url = new URL(arg.request.url);
url.host = host;
url.protocol = "https";
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 a668221a0c..827c29e6f4 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
@@ -34,10 +34,11 @@ export type PageData = {
};
export const loader = async (arg: LoaderArgs) => {
+ const url = new URL(arg.request.url);
const params = getRemixParams(arg.params);
const system = {
params,
- search: {},
+ search: Object.fromEntries(url.searchParams),
};
const resources = await loadResources({ system });
const pageMeta = getPageMeta({ system, resources });
@@ -55,7 +56,6 @@ export const loader = async (arg: LoaderArgs) => {
arg.request.headers.get("host") ||
"";
- const url = new URL(arg.request.url);
url.host = host;
url.protocol = "https";
diff --git a/fixtures/webstudio-remix-vercel/app/routes/[form]._index.tsx b/fixtures/webstudio-remix-vercel/app/routes/[form]._index.tsx
index 61216b8a32..5a8e763826 100644
--- a/fixtures/webstudio-remix-vercel/app/routes/[form]._index.tsx
+++ b/fixtures/webstudio-remix-vercel/app/routes/[form]._index.tsx
@@ -34,10 +34,11 @@ export type PageData = {
};
export const loader = async (arg: LoaderArgs) => {
+ const url = new URL(arg.request.url);
const params = getRemixParams(arg.params);
const system = {
params,
- search: {},
+ search: Object.fromEntries(url.searchParams),
};
const resources = await loadResources({ system });
const pageMeta = getPageMeta({ system, resources });
@@ -55,7 +56,6 @@ export const loader = async (arg: LoaderArgs) => {
arg.request.headers.get("host") ||
"";
- const url = new URL(arg.request.url);
url.host = host;
url.protocol = "https";
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 66e1362a56..27743a39dc 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
@@ -34,10 +34,11 @@ export type PageData = {
};
export const loader = async (arg: LoaderArgs) => {
+ const url = new URL(arg.request.url);
const params = getRemixParams(arg.params);
const system = {
params,
- search: {},
+ search: Object.fromEntries(url.searchParams),
};
const resources = await loadResources({ system });
const pageMeta = getPageMeta({ system, resources });
@@ -55,7 +56,6 @@ export const loader = async (arg: LoaderArgs) => {
arg.request.headers.get("host") ||
"";
- const url = new URL(arg.request.url);
url.host = host;
url.protocol = "https";
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 9917dac5a0..82dbeeb446 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
@@ -34,10 +34,11 @@ export type PageData = {
};
export const loader = async (arg: LoaderArgs) => {
+ const url = new URL(arg.request.url);
const params = getRemixParams(arg.params);
const system = {
params,
- search: {},
+ search: Object.fromEntries(url.searchParams),
};
const resources = await loadResources({ system });
const pageMeta = getPageMeta({ system, resources });
@@ -55,7 +56,6 @@ export const loader = async (arg: LoaderArgs) => {
arg.request.headers.get("host") ||
"";
- const url = new URL(arg.request.url);
url.host = host;
url.protocol = "https";
diff --git a/fixtures/webstudio-remix-vercel/app/routes/[radix]._index.tsx b/fixtures/webstudio-remix-vercel/app/routes/[radix]._index.tsx
index 4b02f7b1ea..ad6ae81528 100644
--- a/fixtures/webstudio-remix-vercel/app/routes/[radix]._index.tsx
+++ b/fixtures/webstudio-remix-vercel/app/routes/[radix]._index.tsx
@@ -34,10 +34,11 @@ export type PageData = {
};
export const loader = async (arg: LoaderArgs) => {
+ const url = new URL(arg.request.url);
const params = getRemixParams(arg.params);
const system = {
params,
- search: {},
+ search: Object.fromEntries(url.searchParams),
};
const resources = await loadResources({ system });
const pageMeta = getPageMeta({ system, resources });
@@ -55,7 +56,6 @@ export const loader = async (arg: LoaderArgs) => {
arg.request.headers.get("host") ||
"";
- const url = new URL(arg.request.url);
url.host = host;
url.protocol = "https";
diff --git a/fixtures/webstudio-remix-vercel/app/routes/[resources]._index.tsx b/fixtures/webstudio-remix-vercel/app/routes/[resources]._index.tsx
index afd52345e7..0438f00a60 100644
--- a/fixtures/webstudio-remix-vercel/app/routes/[resources]._index.tsx
+++ b/fixtures/webstudio-remix-vercel/app/routes/[resources]._index.tsx
@@ -34,10 +34,11 @@ export type PageData = {
};
export const loader = async (arg: LoaderArgs) => {
+ const url = new URL(arg.request.url);
const params = getRemixParams(arg.params);
const system = {
params,
- search: {},
+ search: Object.fromEntries(url.searchParams),
};
const resources = await loadResources({ system });
const pageMeta = getPageMeta({ system, resources });
@@ -55,7 +56,6 @@ export const loader = async (arg: LoaderArgs) => {
arg.request.headers.get("host") ||
"";
- const url = new URL(arg.request.url);
url.host = host;
url.protocol = "https";
diff --git a/fixtures/webstudio-remix-vercel/app/routes/_index.tsx b/fixtures/webstudio-remix-vercel/app/routes/_index.tsx
index e0e6041985..4d787752c9 100644
--- a/fixtures/webstudio-remix-vercel/app/routes/_index.tsx
+++ b/fixtures/webstudio-remix-vercel/app/routes/_index.tsx
@@ -34,10 +34,11 @@ export type PageData = {
};
export const loader = async (arg: LoaderArgs) => {
+ const url = new URL(arg.request.url);
const params = getRemixParams(arg.params);
const system = {
params,
- search: {},
+ search: Object.fromEntries(url.searchParams),
};
const resources = await loadResources({ system });
const pageMeta = getPageMeta({ system, resources });
@@ -55,7 +56,6 @@ export const loader = async (arg: LoaderArgs) => {
arg.request.headers.get("host") ||
"";
- const url = new URL(arg.request.url);
url.host = host;
url.protocol = "https";
diff --git a/packages/cli/templates/defaults/__templates__/route-template.tsx b/packages/cli/templates/defaults/__templates__/route-template.tsx
index 9e6a2d8537..07760e0c59 100644
--- a/packages/cli/templates/defaults/__templates__/route-template.tsx
+++ b/packages/cli/templates/defaults/__templates__/route-template.tsx
@@ -34,10 +34,11 @@ export type PageData = {
};
export const loader = async (arg: LoaderArgs) => {
+ const url = new URL(arg.request.url);
const params = getRemixParams(arg.params);
const system = {
params,
- search: {},
+ search: Object.fromEntries(url.searchParams),
};
const resources = await loadResources({ system });
const pageMeta = getPageMeta({ system, resources });
@@ -55,7 +56,6 @@ export const loader = async (arg: LoaderArgs) => {
arg.request.headers.get("host") ||
"";
- const url = new URL(arg.request.url);
url.host = host;
url.protocol = "https";
diff --git a/packages/sdk/src/page-utilts.test.ts b/packages/sdk/src/page-utils.test.ts
similarity index 100%
rename from packages/sdk/src/page-utilts.test.ts
rename to packages/sdk/src/page-utils.test.ts