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.

feat: Remove useless variables and logic (#2589)

Ivan Starkov09fe7f222263
diff --git a/apps/builder/app/builder/builder.tsx b/apps/builder/app/builder/builder.tsx
index c20fd3402b..7027f0b4c0 100644
--- a/apps/builder/app/builder/builder.tsx
+++ b/apps/builder/app/builder/builder.tsx
@@ -223,7 +223,6 @@ export type BuilderProps = {
domains: string[];
build: Build;
assets: [Asset["id"], Asset][];
- buildOrigin: string;
authToken?: string;
authPermit: AuthPermit;
};
@@ -233,7 +232,6 @@ export const Builder = ({
domains,
build,
assets,
- buildOrigin,
authToken,
authPermit,
}: BuilderProps) => {
@@ -296,7 +294,6 @@ export const Builder = ({
const navigatorLayout = useNavigatorLayout();
const canvasUrl = getBuildUrl({
- buildOrigin,
project,
});
diff --git a/apps/builder/app/entry.server.tsx b/apps/builder/app/entry.server.tsx
index 9ab017405d..8c6f1a1078 100644
--- a/apps/builder/app/entry.server.tsx
+++ b/apps/builder/app/entry.server.tsx
@@ -5,7 +5,7 @@ import * as Sentry from "@sentry/remix";
import { prisma } from "@webstudio-is/prisma-client";
import { initSentry } from "./shared/sentry";
import { handleRequest as handleRequestBuilder } from "./shared/remix";
-import { getBuildParams } from "./shared/router-utils";
+import { isCanvas } from "./shared/router-utils";
initSentry({
integrations: [new Sentry.Integrations.Prisma({ client: prisma })],
@@ -43,7 +43,7 @@ export default function handleRequest(
remixContext.staticHandlerContext.matches.some(
(match) => match.route.id === "routes/_index"
) &&
- getBuildParams(request) !== undefined
+ isCanvas(request)
) {
handle = handleRequestCanvas;
}
diff --git a/apps/builder/app/env/env.server.ts b/apps/builder/app/env/env.server.ts
index c285358a90..83c4793a8a 100644
--- a/apps/builder/app/env/env.server.ts
+++ b/apps/builder/app/env/env.server.ts
@@ -18,10 +18,6 @@ const env = {
TRPC_SERVER_URL: process.env.TRPC_SERVER_URL,
TRPC_SERVER_API_TOKEN: process.env.TRPC_SERVER_API_TOKEN,
- // Canvas build logic
- BUILD_ORIGIN: process.env.BUILD_ORIGIN,
- BUILD_REQUIRE_SUBDOMAIN: process.env.BUILD_REQUIRE_SUBDOMAIN,
-
PORT: process.env.PORT,
// Preview support
diff --git a/apps/builder/app/routes/$.tsx b/apps/builder/app/routes/$.tsx
index de56e2d52c..6f9ce37eed 100644
--- a/apps/builder/app/routes/$.tsx
+++ b/apps/builder/app/routes/$.tsx
@@ -10,7 +10,7 @@ import env from "~/env/env.public.server";
import { sentryException } from "~/shared/sentry";
import { Canvas } from "~/canvas";
import { ErrorMessage } from "~/shared/error";
-import { getBuildParams, dashboardPath } from "~/shared/router-utils";
+import { dashboardPath, isCanvas } from "~/shared/router-utils";
import { createImageLoader } from "@webstudio-is/image";
export const loader = async ({ request }: LoaderArgs) => {
@@ -25,8 +25,7 @@ export const loader = async ({ request }: LoaderArgs) => {
});
}
- const buildParams = getBuildParams(request);
- if (buildParams === undefined) {
+ if (isCanvas(request) === false) {
throw redirect(dashboardPath());
}
diff --git a/apps/builder/app/routes/builder.$projectId.tsx b/apps/builder/app/routes/builder.$projectId.tsx
index 653502e983..fd7363abef 100644
--- a/apps/builder/app/routes/builder.$projectId.tsx
+++ b/apps/builder/app/routes/builder.$projectId.tsx
@@ -16,7 +16,7 @@ import { loadAssetsByProject } from "@webstudio-is/asset-uploader/index.server";
import { createContext } from "~/shared/context.server";
import { ErrorMessage } from "~/shared/error";
import { sentryException } from "~/shared/sentry";
-import { getBuildOrigin, loginPath } from "~/shared/router-utils";
+import { loginPath } from "~/shared/router-utils";
import { type BuilderProps, Builder, links } from "~/builder";
export { links };
@@ -83,7 +83,6 @@ export const loader = async ({
domains,
build: devBuild,
assets: assets.map((asset) => [asset.id, asset]),
- buildOrigin: getBuildOrigin(request),
authToken,
authPermit,
};
diff --git a/apps/builder/app/shared/remix/root.tsx b/apps/builder/app/shared/remix/root.tsx
index 8159943ac7..522a06aee3 100644
--- a/apps/builder/app/shared/remix/root.tsx
+++ b/apps/builder/app/shared/remix/root.tsx
@@ -6,7 +6,6 @@ import {
Scripts,
} from "@remix-run/react";
import { Env } from "~/shared/env";
-import env from "~/env/env.server";
import { createHead } from "remix-island";
export const Head = createHead(() => (
@@ -23,9 +22,7 @@ export const Root = ({ Outlet = RemixOutlet }) => {
<Outlet />
<Env />
<Scripts />
- {process.env.NODE_ENV === "development" && (
- <LiveReload port={env.BUILD_ORIGIN !== undefined ? 3010 : undefined} />
- )}
+ {process.env.NODE_ENV === "development" && <LiveReload />}
</>
);
};
diff --git a/apps/builder/app/shared/router-utils/build-params.test.ts b/apps/builder/app/shared/router-utils/build-params.test.ts
deleted file mode 100644
index d01509efc8..0000000000
--- a/apps/builder/app/shared/router-utils/build-params.test.ts
+++ /dev/null
@@ -1,113 +0,0 @@
-import { describe, test, expect } from "@jest/globals";
-import { getBuildParams, getBuildOrigin } from "./build-params";
-
-const makeRequest = (url: string) => ({
- url,
- headers: {
- get: (name: string) => {
- if (name === "host" || name === "x-forwarded-host") {
- return new URL(url).host;
- }
- return null;
- },
- },
-});
-
-describe("getBuildOrigin", () => {
- test("Normal operation", () => {
- expect(
- getBuildOrigin(makeRequest("https://bar.com"), {
- ...process.env,
- BUILD_ORIGIN: "https://foo.com",
- })
- ).toBe("https://foo.com");
- });
-
- test("Local development", () => {
- const env = {} as const;
- const saveNodeEnv = process.env.NODE_ENV;
- process.env.NODE_ENV = "development";
- expect(getBuildOrigin(makeRequest("http://localhost"), env)).toBe(
- "http://localhost"
- );
- expect(getBuildOrigin(makeRequest("http://localhost:1234"), env)).toBe(
- "http://localhost:1234"
- );
- expect(getBuildOrigin(makeRequest("http://foo.localhost:1234"), env)).toBe(
- "http://localhost:1234"
- );
- process.env.NODE_ENV = saveNodeEnv;
- });
-
- test("Vercel preview", () => {
- const env = {
- ...process.env,
- DEPLOYMENT_ENVIRONMENT: "preview",
- DEPLOYMENT_URL: "bar.foo.com",
- };
- expect(getBuildOrigin(makeRequest("https://baz.com"), env)).toBe(
- "https://bar.foo.com"
- );
- });
-});
-
-describe("getBuildParams", () => {
- const env = {
- BUILD_ORIGIN: "https://foo.com",
- };
-
- test("detects project domain", () => {
- const request = makeRequest("https://bar.foo.com");
- expect(
- (getBuildParams(request, env) as { projectDomain: string }).projectDomain
- ).toBe("bar");
- });
-
- test("doesn't detect project domain if base domain is not a user content domain", () => {
- const request = makeRequest("https://bar.baz.com");
- expect(getBuildParams(request, env)).toBeUndefined();
- });
-
- test("detects project id", () => {
- const request = makeRequest("https://foo.com?projectId=123");
- expect(
- (getBuildParams(request, env) as { projectId: string }).projectId
- ).toBe("123");
- });
-
- test("doesn't detect project id if domain is not a user content domain", () => {
- const request = makeRequest("https://baz.com?projectId=123");
- expect(getBuildParams(request, env)).toBeUndefined();
- });
-
- test("doesn't detect project id if BUILD_REQUIRE_SUBDOMAIN is true", () => {
- const request = makeRequest("https://foo.com?projectId=123");
- expect(
- getBuildParams(request, { BUILD_REQUIRE_SUBDOMAIN: "true", ...env })
- ).toBeUndefined();
- });
-
- test("detects path", () => {
- expect(
- (
- getBuildParams(makeRequest("https://bar.foo.com"), env) as {
- pagePath: string;
- }
- ).pagePath
- ).toBe("/");
- expect(
- (
- getBuildParams(makeRequest("https://bar.foo.com/abc"), env) as {
- pagePath: string;
- }
- ).pagePath
- ).toBe("/abc");
- expect(
- (
- getBuildParams(makeRequest("https://bar.foo.com/abc/123"), env) as {
- pagePath: string;
- }
- ).pagePath
- ).toBe("/abc/123");
- });
-});
diff --git a/apps/builder/app/shared/router-utils/build-params.ts b/apps/builder/app/shared/router-utils/build-params.ts
deleted file mode 100644
index fc52c2a0a2..0000000000
--- a/apps/builder/app/shared/router-utils/build-params.ts
+++ /dev/null
@@ -1,102 +0,0 @@
-// These are the utils for manipulating "build" params.
-// "Build" means user generated content — what user builds.
-
-import serverEnv from "~/env/env.server";
-
-export type BuildParams = ({ pagePath: string } | { pageId: string }) &
- ({ projectId: string } | { projectDomain: string });
-
-// A subtype of Request. To make testing easier.
-type MinimalRequest = {
- url: string;
- headers: { get: (name: string) => string | null };
-};
-
-export const getRequestHost = (request: MinimalRequest): string =>
- request.headers.get("x-forwarded-host") || request.headers.get("host") || "";
-
-const isLocalhost = (host: string) => {
- // remove port if present
- const [domain] = host.split(":");
- return domain === "localhost" || domain.endsWith(".localhost");
-};
-
-const defaultEnv = {
- BUILD_ORIGIN: serverEnv.BUILD_ORIGIN,
- DEPLOYMENT_ENVIRONMENT: serverEnv.DEPLOYMENT_ENVIRONMENT,
- DEPLOYMENT_URL: serverEnv.DEPLOYMENT_URL,
- BUILD_REQUIRE_SUBDOMAIN: serverEnv.BUILD_REQUIRE_SUBDOMAIN,
-};
-
-export const getBuildOrigin = (
- request: MinimalRequest,
- env: {
- BUILD_ORIGIN?: string;
- DEPLOYMENT_ENVIRONMENT?: string;
- DEPLOYMENT_URL?: string;
- } = defaultEnv
-): string => {
- const { BUILD_ORIGIN } = env;
- if (BUILD_ORIGIN !== undefined && BUILD_ORIGIN !== "") {
- return BUILD_ORIGIN;
- }
-
- // Local development special case
- const host = getRequestHost(request);
-
- if (isLocalhost(host)) {
- return `http://${host.split(".").pop()}`;
- }
- // If we're not on localhost, we're probably using ip address - useful for remote development
- if (
- /(\b(?:(?:25[0-5]|2[0-4]\d|[01]?\d\d?)\.){3}(?:25[0-5]|2[0-4]\d|[01]?\d\d?)(?::\d{0,4})?\b)/.test(
- host
- )
- ) {
- return `http://${host}`;
- }
-
- // Vercel preview special case
- if (
- (env.DEPLOYMENT_ENVIRONMENT === "preview" ||
- env.DEPLOYMENT_ENVIRONMENT === "development") &&
- typeof env.DEPLOYMENT_URL === "string"
- ) {
- return `https://${env.DEPLOYMENT_URL}`;
- }
-
- throw new Error("Could not determine user content host");
-};
-
-export const getBuildParams = (
- request: MinimalRequest,
- env: {
- BUILD_REQUIRE_SUBDOMAIN?: string;
- BUILD_ORIGIN?: string;
- DEPLOYMENT_ENVIRONMENT?: string;
- DEPLOYMENT_URL?: string;
- } = defaultEnv
-): BuildParams | undefined => {
- const url = new URL(request.url);
-
- const requestHost = getRequestHost(request);
- const buildHost = new URL(getBuildOrigin(request, env)).host;
- const pageId = url.searchParams.get("pageId") ?? undefined;
-
- if (env.BUILD_REQUIRE_SUBDOMAIN !== "true") {
- const projectId = url.searchParams.get("projectId");
- if (projectId !== null && buildHost === requestHost) {
- return pageId === undefined
- ? { projectId, pagePath: url.pathname }
- : { projectId, pageId };
- }
- }
-
- const [projectDomain, ...rest] = requestHost.split(".");
- const baseHost = rest.join(".");
- if (baseHost === buildHost) {
- return pageId === undefined
- ? { projectDomain, pagePath: url.pathname }
- : { projectDomain, pageId };
- }
-};
diff --git a/apps/builder/app/shared/router-utils/index.ts b/apps/builder/app/shared/router-utils/index.ts
index 26eef7723f..a87ce47b86 100644
--- a/apps/builder/app/shared/router-utils/index.ts
+++ b/apps/builder/app/shared/router-utils/index.ts
@@ -1,2 +1,2 @@
-export * from "./build-params";
+export * from "./is-canvas";
export * from "./path-utils";
diff --git a/apps/builder/app/shared/router-utils/is-canvas.ts b/apps/builder/app/shared/router-utils/is-canvas.ts
new file mode 100644
index 0000000000..245f1f1227
--- /dev/null
+++ b/apps/builder/app/shared/router-utils/is-canvas.ts
@@ -0,0 +1,18 @@
+// These are the utils for manipulating "build" params.
+// "Build" means user generated content — what user builds.
+
+export type BuildParams = ({ pagePath: string } | { pageId: string }) &
+ ({ projectId: string } | { projectDomain: string });
+
+// A subtype of Request. To make testing easier.
+type MinimalRequest = {
+ url: string;
+ headers: { get: (name: string) => string | null };
+};
+
+export const isCanvas = (request: MinimalRequest): boolean => {
+ const url = new URL(request.url);
+ const projectId = url.searchParams.get("projectId");
+
+ return projectId !== null;
+};
diff --git a/apps/builder/app/shared/router-utils/path-utils.ts b/apps/builder/app/shared/router-utils/path-utils.ts
index 4e8e082acb..ddf0a7b4c9 100644
--- a/apps/builder/app/shared/router-utils/path-utils.ts
+++ b/apps/builder/app/shared/router-utils/path-utils.ts
@@ -131,22 +131,12 @@ export const restPatchPath = (props: { authToken?: string }) => {
}`;
};
-export const getBuildUrl = ({
- buildOrigin,
- project,
-}: {
- buildOrigin: string;
- project: Project;
-}) => {
- const url = new URL(buildOrigin);
-
- if (env.BUILD_REQUIRE_SUBDOMAIN) {
- url.host = `${project.domain}.${url.host}`;
- }
-
- url.searchParams.set("projectId", project.id);
+export const getBuildUrl = ({ project }: { project: Project }) => {
+ // const url = new URL(buildOrigin);
+ const searchParams = new URLSearchParams();
+ searchParams.set("projectId", project.id);
- return url.toString();
+ return `/?${searchParams.toString()}`;
};
export const getPublishedUrl = (domain: string) => {
diff --git a/apps/builder/env-check.js b/apps/builder/env-check.js
index dd9cf9b825..ddf79e579c 100644
--- a/apps/builder/env-check.js
+++ b/apps/builder/env-check.js
@@ -44,11 +44,6 @@ if (process.env.DEPLOYMENT_ENVIRONMENT === "production") {
"👉 In production PUBLISHER_HOST is required for website functionality. Please set it to your production URL of the builder."
);
}
- if (!process.env.BUILD_ORIGIN) {
- errors.push(
- "👉 In production BUILD_ORIGIN is required for security reasons."
- );
- }
}
// check for when user has some S3 env variables but not all required