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: Compare server and client versions and prompt for reload. (#3833)

Ivan Starkov6a930b1c0c11
diff --git a/apps/builder/app/builder/shared/sync/sync-server.ts b/apps/builder/app/builder/shared/sync/sync-server.ts
index a2dbb2d07e..11d10caa3b 100644
--- a/apps/builder/app/builder/shared/sync/sync-server.ts
+++ b/apps/builder/app/builder/shared/sync/sync-server.ts
@@ -182,9 +182,7 @@ const syncServer = async function () {
// user may cancel to copy own state before reloading
if (result.status === "version_mismatched") {
const error =
- "You are currently in single-player mode. " +
- "The project has been edited in a different tab, browser, or by another user. " +
- "Please reload the page to get the latest version.";
+ result.errors ?? "Unknown version mismatch. Please reload.";
const shouldReload = confirm(error);
if (shouldReload) {
diff --git a/apps/builder/app/env/env.static.ts b/apps/builder/app/env/env.static.ts
new file mode 100644
index 0000000000..182ab980d2
--- /dev/null
+++ b/apps/builder/app/env/env.static.ts
@@ -0,0 +1,12 @@
+/**
+ * Public Build-time Environment Variables
+ *
+ * These variables are injected into your bundle at build time based on the environment settings.
+ * - Configuration: See envPrefix in [vite.config.ts](../../vite.config.ts) (GITHUB_)
+ * - Documentation: Refer to the [Vite documentation](https://vitejs.dev/guide/env-and-mode)
+ * - Type Definitions: See [vite-env.d.ts](./vite-env.d.ts) in this directory
+ */
+
+export const publicStaticEnv = {
+ VERSION: import.meta.env.GITHUB_SHA ?? "local",
+};
diff --git a/apps/builder/app/routes/rest.patch.ts b/apps/builder/app/routes/rest.patch.ts
index 03f3dfc2cc..46db458c11 100644
--- a/apps/builder/app/routes/rest.patch.ts
+++ b/apps/builder/app/routes/rest.patch.ts
@@ -38,6 +38,7 @@ import { authorizeProject } from "@webstudio-is/trpc-interface/index.server";
import { createContext } from "~/shared/context.server";
import { db } from "@webstudio-is/project/index.server";
import type { Database } from "@webstudio-is/postrest/index.server";
+import { publicStaticEnv } from "~/env/env.static";
type PatchData = {
transactions: Array<SyncItem>;
@@ -46,7 +47,13 @@ type PatchData = {
version: number;
};
-export const action = async ({ request }: ActionFunctionArgs) => {
+export const action = async ({
+ request,
+}: ActionFunctionArgs): Promise<
+ | { status: "ok" }
+ | { status: "version_mismatched"; errors: string }
+ | { status: "error"; errors: string }
+> => {
enableMapSet();
enablePatches();
@@ -57,17 +64,30 @@ export const action = async ({ request }: ActionFunctionArgs) => {
transactions,
version: clientVersion,
}: PatchData = await request.json();
+
+ const version = new URL(request.url).searchParams.get("client-version");
+
+ if (publicStaticEnv.VERSION !== version) {
+ return {
+ status: "version_mismatched",
+ errors: `The client and server versions do not match. Please reload to continue.`,
+ };
+ }
+
if (buildId === undefined) {
- return { errors: "Build id required" };
+ return { status: "error", errors: "Build id required" };
}
if (projectId === undefined) {
- return { errors: "Project id required" };
+ return { status: "error", errors: "Project id required" };
}
const lastTransactionId = transactions.at(-1)?.transactionId;
if (lastTransactionId === undefined) {
- return { errors: "Transaction array must not be empty." };
+ return {
+ status: "error",
+ errors: "Transaction array must not be empty.",
+ };
}
const context = await createContext(request);
@@ -92,6 +112,7 @@ export const action = async ({ request }: ActionFunctionArgs) => {
return {
status: "version_mismatched",
+ errors: `You are currently in single-player mode. The project has been edited in a different tab, browser, or by another user. Please reload the page to get the latest version.`,
};
}
@@ -220,7 +241,7 @@ export const action = async ({ request }: ActionFunctionArgs) => {
continue;
}
- return { errors: `Unknown namespace "${namespace}"` };
+ return { status: "error", errors: `Unknown namespace "${namespace}"` };
}
}
@@ -323,6 +344,7 @@ export const action = async ({ request }: ActionFunctionArgs) => {
// Given the sequential nature of messages from a single client, this situation is deemed improbable.
return {
status: "version_mismatched",
+ errors: `You are currently in single-player mode. The project has been edited in a different tab, browser, or by another user. Please reload the page to get the latest version.`,
};
}
@@ -337,6 +359,7 @@ export const action = async ({ request }: ActionFunctionArgs) => {
} catch (error) {
console.error(error);
return {
+ status: "error",
errors: error instanceof Error ? error.message : JSON.stringify(error),
};
}
diff --git a/apps/builder/app/shared/router-utils/path-utils.ts b/apps/builder/app/shared/router-utils/path-utils.ts
index 6bffa420c8..260fb8c1dc 100644
--- a/apps/builder/app/shared/router-utils/path-utils.ts
+++ b/apps/builder/app/shared/router-utils/path-utils.ts
@@ -1,6 +1,7 @@
import type { AUTH_PROVIDERS } from "~/shared/session";
import type { Project } from "@webstudio-is/project";
import { $authToken } from "../nano-states";
+import { publicStaticEnv } from "~/env/env.static";
const searchParams = (params: Record<string, string | undefined | null>) => {
const searchParams = new URLSearchParams();
@@ -139,6 +140,8 @@ export const restPatchPath = (props: { authToken?: string }) => {
urlSearchParams.set("authToken", props.authToken);
}
+ urlSearchParams.set("client-version", publicStaticEnv.VERSION);
+
const urlSearchParamsString = urlSearchParams.toString();
return `/rest/patch${
diff --git a/packages/jest-config/esbuild-jest.js b/packages/jest-config/esbuild-jest.js
index 2909583371..203c7900d5 100644
--- a/packages/jest-config/esbuild-jest.js
+++ b/packages/jest-config/esbuild-jest.js
@@ -20,6 +20,9 @@ const createTransformer = () => ({
sourcemap: true,
sourcesContent: false,
sourcefile: filename,
+ define: {
+ "import.meta.env": "{}",
+ },
});
const map = {