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
fix: Fix builder system pathname for folder pages (#5757)
Oleg Isonenac14670d9e84
diff --git a/apps/builder/app/shared/system.test.ts b/apps/builder/app/shared/system.test.tsindex 3ceeb4215f..916492449d 100644--- a/apps/builder/app/shared/system.test.ts+++ b/apps/builder/app/shared/system.test.ts@@ -1,12 +1,20 @@-import { describe, expect, test } from "vitest";+import { beforeEach, describe, expect, test } from "vitest";import type { Page, Pages } from "@webstudio-is/sdk";import { $pages } from "~/shared/sync/data-stores";import { registerContainers } from "~/shared/sync/sync-stores";-import { updateCurrentSystem } from "./system";+import {+ $currentSystem,+ $systemDataByPage,+ updateCurrentSystem,+} from "./system";import { selectPage } from "./nano-states";registerContainers();+beforeEach(() => {+ $systemDataByPage.set(new Map());+});+const getInitialPages = (page: Page): Pages => ({homePageId: "homeId",rootFolderId: "rootId",@@ -90,3 +98,86 @@ describe("history", () => {]);});});++test("system pathname includes parent folder slug", () => {+ $pages.set({+ ...getInitialPages({+ id: "dynamicId",+ path: "/post/:slug",+ name: "",+ title: "",+ meta: {},+ rootInstanceId: "",+ }),+ folders: new Map([+ [+ "rootId",+ {+ id: "rootId",+ name: "",+ slug: "",+ children: ["homeId", "folderId"],+ },+ ],+ [+ "folderId",+ {+ id: "folderId",+ name: "Blog",+ slug: "blog",+ children: ["dynamicId"],+ },+ ],+ ]),+ });+ selectPage("dynamicId");++ updateCurrentSystem({+ params: { slug: "my-post" },+ });++ expect($currentSystem.get().pathname).toBe("/blog/post/my-post");+ expect($pages.get()?.pages.get("dynamicId")?.history).toEqual([+ "/blog/post/my-post",+ ]);+});++test("system params support legacy history without parent folder slug", () => {+ $pages.set({+ ...getInitialPages({+ id: "dynamicId",+ path: "/post/:slug",+ name: "",+ title: "",+ meta: {},+ rootInstanceId: "",+ history: ["/post/my-post"],+ }),+ folders: new Map([+ [+ "rootId",+ {+ id: "rootId",+ name: "",+ slug: "",+ children: ["homeId", "folderId"],+ },+ ],+ [+ "folderId",+ {+ id: "folderId",+ name: "Blog",+ slug: "blog",+ children: ["dynamicId"],+ },+ ],+ ]),+ });+ selectPage("dynamicId");++ expect($currentSystem.get()).toMatchObject({+ params: { slug: "my-post" },+ pathname: "/blog/post/my-post",+ });+});diff --git a/apps/builder/app/shared/system.ts b/apps/builder/app/shared/system.tsindex 7af3fd5196..fe69a9d13a 100644--- a/apps/builder/app/shared/system.ts+++ b/apps/builder/app/shared/system.ts@@ -1,5 +1,10 @@import { atom, computed } from "nanostores";-import { findPageByIdOrPath, type Page, type System } from "@webstudio-is/sdk";+import {+ findPageByIdOrPath,+ getPagePath,+ type Page,+ type System,+} from "@webstudio-is/sdk";import {compilePathnamePattern,matchPathnamePattern,@@ -14,13 +19,22 @@ export const $systemDataByPage = atom(new Map<Page["id"], Pick<System, "search" | "params">>());-const extractParams = (pattern: string, path?: string) => {+const extractParams = (+ pattern: string,+ path?: string,+ fallbackPattern?: string+) => {const params: System["params"] = {};const tokens = tokenizePathnamePattern(pattern);// try to match the first item in history to let user// see the page without manually entering params// or selecting them in address bar- const matchedParams = path ? matchPathnamePattern(pattern, path) : undefined;+ const matchedParams = path+ ? (matchPathnamePattern(pattern, path) ??+ (fallbackPattern+ ? matchPathnamePattern(fallbackPattern, path)+ : undefined))+ : undefined;for (const token of tokens) {if (token.type === "param") {params[token.name] = matchedParams?.[token.name] ?? undefined;@@ -30,21 +44,26 @@ const extractParams = (pattern: string, path?: string) => {};export const $currentSystem = computed(- [$publishedOrigin, $selectedPage, $systemDataByPage],- (origin, page, systemByPage) => {+ [$publishedOrigin, $selectedPage, $pages, $systemDataByPage],+ (origin, page, pages, systemByPage) => {const system: System = {search: {},params: {},pathname: "/",origin,};- if (page === undefined) {+ if (page === undefined || pages === undefined) {return system;}const systemData = systemByPage.get(page.id);- const extractedParams = extractParams(page.path, page.history?.[0]);+ const pagePath = getPagePath(page.id, pages);+ const extractedParams = extractParams(+ pagePath,+ page.history?.[0],+ page.path+ );const params = { ...extractedParams, ...systemData?.params };- const pathname = compilePath(page.path, params) || "/";+ const pathname = compilePath(pagePath, params) || "/";return {search: { ...system.search, ...systemData?.search },params,@@ -72,8 +91,9 @@ const savePathInHistory = (pageId: string, path: string) => {if (page === undefined) {return;}+ const pagePath = getPagePath(page.id, pages);const history = Array.from(page.history ?? []);- history.unshift(path);+ history.unshift(path || pagePath);page.history = Array.from(new Set(history)).slice(0, 20);});};@@ -91,5 +111,7 @@ export const updateCurrentSystem = (const params = update.params ?? systemData?.params ?? {};systemDataByPage.set(page.id, { search, params });$systemDataByPage.set(systemDataByPage);- savePathInHistory(page.id, compilePath(page.path, params));+ const pages = $pages.get();+ const pagePath = pages ? getPagePath(page.id, pages) : page.path;+ savePathInHistory(page.id, compilePath(pagePath, params));};