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: Allow folder index page paths (#6147)

Oleg Isonen0d5c2f6ee8d0
diff --git a/packages/project/src/db/build-patch.test.ts b/packages/project/src/db/build-patch.test.ts
index 535c43f6d6..5de30f235e 100644
--- a/packages/project/src/db/build-patch.test.ts
+++ b/packages/project/src/db/build-patch.test.ts
@@ -395,6 +395,103 @@ describe("patchBuild", () => {
).toBe("/company");
});
+ test("persists folder index paths for page updates and duplicates", async () => {
+ const buildWithFolder = {
+ ...buildRow,
+ pages: JSON.stringify({
+ meta: {},
+ homePage: {
+ id: "page-1",
+ name: "Home",
+ path: "",
+ title: "Home",
+ meta: {},
+ rootInstanceId: "body-1",
+ },
+ pages: [
+ {
+ id: "page-2",
+ name: "Landing",
+ path: "/landing",
+ title: "Landing",
+ meta: {},
+ rootInstanceId: "body-2",
+ },
+ ],
+ folders: [
+ {
+ id: "root",
+ name: "Root",
+ slug: "",
+ children: ["page-1", "folder-1"],
+ },
+ {
+ id: "folder-1",
+ name: "Folder",
+ slug: "folder",
+ children: ["page-2"],
+ },
+ ],
+ }),
+ };
+
+ const updated = await createBuildPatchUpdate({
+ build: buildWithFolder,
+ clientVersion: 3,
+ transactions: [
+ transaction({
+ payload: [
+ {
+ namespace: "pages",
+ patches: [
+ {
+ op: "replace",
+ path: ["pages", "page-2", "path"],
+ value: "",
+ },
+ ],
+ },
+ ],
+ }),
+ ],
+ });
+ expect(updated.status).toBe("ok");
+
+ const duplicated = await createBuildPatchUpdate({
+ build: buildWithFolder,
+ clientVersion: 3,
+ transactions: [
+ transaction({
+ payload: [
+ {
+ namespace: "pages",
+ patches: [
+ {
+ op: "add",
+ path: ["pages", "page-copy"],
+ value: {
+ id: "page-copy",
+ name: "Copy",
+ path: "",
+ title: "Copy",
+ meta: {},
+ rootInstanceId: "body-copy",
+ },
+ },
+ {
+ op: "add",
+ path: ["folders", "folder-1", "children", 1],
+ value: "page-copy",
+ },
+ ],
+ },
+ ],
+ }),
+ ],
+ });
+ expect(duplicated.status).toBe("ok");
+ });
+
test("applies data source variable additions", async () => {
const result = await createBuildPatchUpdate({
build: buildRow,
diff --git a/packages/sdk/src/schema/pages.test.ts b/packages/sdk/src/schema/pages.test.ts
index 82038a0896..d7f1416519 100644
--- a/packages/sdk/src/schema/pages.test.ts
+++ b/packages/sdk/src/schema/pages.test.ts
@@ -154,6 +154,40 @@ test("validates non-home page path is not empty", () => {
]);
});
+test("allows an empty local path for a folder index page", () => {
+ expect(
+ pages.safeParse({
+ ...validPages,
+ pages: new Map(validPages.pages).set("folder-index", {
+ id: "folder-index",
+ name: "Folder index",
+ path: "",
+ title: `"Folder index"`,
+ meta: {},
+ rootInstanceId: "folderIndexRoot",
+ }),
+ folders: new Map([
+ [
+ "root",
+ {
+ ...validPages.folders.get("root")!,
+ children: ["home", "folder"],
+ },
+ ],
+ [
+ "folder",
+ {
+ id: "folder",
+ name: "Folder",
+ slug: "folder",
+ children: ["folder-index"],
+ },
+ ],
+ ]),
+ }).success
+ ).toBe(true);
+});
+
test("supports text document type", () => {
expect(documentTypes).toContain("text");
expect(
diff --git a/packages/sdk/src/schema/pages.ts b/packages/sdk/src/schema/pages.ts
index e8135c9904..2a237fc38d 100644
--- a/packages/sdk/src/schema/pages.ts
+++ b/packages/sdk/src/schema/pages.ts
@@ -287,6 +287,17 @@ export const pages = z
.superRefine((pages, context) => {
const homePage = pages.pages.get(pages.homePageId);
const rootFolder = pages.folders.get(pages.rootFolderId);
+ const folderIndexPageIds = new Set<Page["id"]>();
+ for (const [folderId, folder] of pages.folders) {
+ if (folderId === pages.rootFolderId) {
+ continue;
+ }
+ for (const childId of folder.children) {
+ if (pages.pages.has(childId)) {
+ folderIndexPageIds.add(childId);
+ }
+ }
+ }
if (homePage === undefined) {
context.addIssue({
code: z.ZodIssueCode.custom,
@@ -316,7 +327,11 @@ export const pages = z
message: "Page id must match its record key",
});
}
- if (pageId !== pages.homePageId && page.path === "") {
+ if (
+ pageId !== pages.homePageId &&
+ page.path === "" &&
+ folderIndexPageIds.has(pageId) === false
+ ) {
context.addIssue({
code: z.ZodIssueCode.custom,
path: ["pages", pageId, "path"],