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.

change return type of getCanvasRequestParams

Roman Pominov20990cd4189b
diff --git a/apps/designer/app/routes/$.tsx b/apps/designer/app/routes/$.tsx
index 2741dd5b85..21ff6aae15 100644
--- a/apps/designer/app/routes/$.tsx
+++ b/apps/designer/app/routes/$.tsx
@@ -35,12 +35,12 @@ export const loader: LoaderFunction = async ({
return redirect(config.dashboardPath);
}
- const { projectIdObject, mode, pathname } = canvasRequest;
+ const { mode, pathname } = canvasRequest;
const project =
- projectIdObject.type === "id"
- ? await db.project.loadById(projectIdObject.value)
- : await db.project.loadByDomain(projectIdObject.value);
+ "projectId" in canvasRequest
+ ? await db.project.loadById(canvasRequest.projectId)
+ : await db.project.loadByDomain(canvasRequest.projectDomain);
if (project === null) {
throw new Error("Project not found");
diff --git a/apps/designer/app/shared/router-utils/canvas-request-params.test.ts b/apps/designer/app/shared/router-utils/canvas-request-params.test.ts
index 6450791f12..c09c7af2e3 100644
--- a/apps/designer/app/shared/router-utils/canvas-request-params.test.ts
+++ b/apps/designer/app/shared/router-utils/canvas-request-params.test.ts
@@ -20,11 +20,12 @@ describe("getCanvasRequestParams", () => {
makeRequest("https://foo.localhost", {
"x-forwarded-host": "foo.localhost",
})
- )?.projectIdObject
+ )
).toMatchInlineSnapshot(`
Object {
- "type": "domain",
- "value": "foo",
+ "mode": "published",
+ "pathname": "/",
+ "projectDomain": "foo",
}
`);
});
@@ -33,11 +34,12 @@ describe("getCanvasRequestParams", () => {
expect(
getCanvasRequestParams(
makeRequest("https://foo.localhost", { host: "foo.localhost" })
- )?.projectIdObject
+ )
).toMatchInlineSnapshot(`
Object {
- "type": "domain",
- "value": "foo",
+ "mode": "published",
+ "pathname": "/",
+ "projectDomain": "foo",
}
`);
});
@@ -46,11 +48,12 @@ describe("getCanvasRequestParams", () => {
expect(
getCanvasRequestParams(
makeRequest("https://foo.localhost?projectId=bar", {})
- )?.projectIdObject
+ )
).toMatchInlineSnapshot(`
Object {
- "type": "id",
- "value": "bar",
+ "mode": "published",
+ "pathname": "/",
+ "projectId": "bar",
}
`);
});
diff --git a/apps/designer/app/shared/router-utils/canvas-request-params.ts b/apps/designer/app/shared/router-utils/canvas-request-params.ts
index b4f18eb540..e4720149c1 100644
--- a/apps/designer/app/shared/router-utils/canvas-request-params.ts
+++ b/apps/designer/app/shared/router-utils/canvas-request-params.ts
@@ -4,14 +4,17 @@ export type CanvasRouteMode = "edit" | "preview" | "published";
const modes = ["edit", "preview", "published"] as CanvasRouteMode[];
-type ProjectIdentifier = { type: "id" | "domain"; value: string };
-
export const getCanvasRequestParams = (request: {
url: string;
headers: { get: (name: string) => string | null };
}):
| {
- projectIdObject: ProjectIdentifier;
+ projectId: string;
+ mode: CanvasRouteMode;
+ pathname: string;
+ }
+ | {
+ projectDomain: string;
mode: CanvasRouteMode;
pathname: string;
}
@@ -24,29 +27,26 @@ export const getCanvasRequestParams = (request: {
return undefined;
}
- let projectIdObject: ProjectIdentifier | undefined = undefined;
+ let projectDomain = undefined;
+ let projectId = undefined;
- // @todo all this subdomain logic is very hacky
- const host =
- request.headers.get("x-forwarded-host") ||
- request.headers.get("host") ||
- "";
- const [userDomain, wstdDomain] = host.split(".");
- if (wstdDomain === "wstd" || wstdDomain?.includes("localhost")) {
- projectIdObject = { type: "domain", value: userDomain };
+ const projectIdParam = url.searchParams.get("projectId");
+ if (projectIdParam !== null) {
+ projectId = projectIdParam;
}
- if (projectIdObject === undefined) {
- const projectIdParam = url.searchParams.get("projectId");
- if (projectIdParam !== null) {
- projectIdObject = { type: "id", value: projectIdParam };
+ if (projectId === undefined) {
+ // @todo all this subdomain logic is very hacky
+ const host =
+ request.headers.get("x-forwarded-host") ||
+ request.headers.get("host") ||
+ "";
+ const [userDomain, wstdDomain] = host.split(".");
+ if (wstdDomain === "wstd" || wstdDomain?.includes("localhost")) {
+ projectDomain = userDomain;
}
}
- if (projectIdObject === undefined) {
- return undefined;
- }
-
const modeParam = url.searchParams.get("mode");
const mode =
modeParam === null ? "published" : modes.find((mode) => mode === modeParam);
@@ -54,5 +54,21 @@ export const getCanvasRequestParams = (request: {
throw new Error(`Invalid mode "${modeParam}"`);
}
- return { projectIdObject, mode, pathname };
+ if (projectId !== undefined) {
+ return {
+ projectId,
+ mode,
+ pathname,
+ };
+ }
+
+ if (projectDomain !== undefined) {
+ return {
+ projectDomain,
+ mode,
+ pathname,
+ };
+ }
+
+ return undefined;
};