Skip to content

Open-source manufacturing ERP, MES and QMS. Quoting, MRP, inventory, shop floor, quality and lot/serial traceability on one Postgres schema, with a REST API and MCP server. Self-host or use Carbon Cloud.

fix: load Vercel Web Analytics only in builds Vercel ran (#1973)

Naveen Kashyapd29d84c07a9c
diff --git a/apps/erp/app/root.tsx b/apps/erp/app/root.tsx
index 59c659325d..eac3a886c6 100644
--- a/apps/erp/app/root.tsx
+++ b/apps/erp/app/root.tsx
@@ -317,7 +317,9 @@ export function Document({
<Toaster position="bottom-right" visibleToasts={5} />
<ScrollRestoration />
<Scripts />
- {!CONTROLLED_ENVIRONMENT && import.meta.env.PROD && <Analytics />}
+ {!CONTROLLED_ENVIRONMENT &&
+ import.meta.env.PROD &&
+ import.meta.env.VITE_VERCEL_ENV && <Analytics />}
</body>
</html>
);
diff --git a/apps/erp/vite.config.ts b/apps/erp/vite.config.ts
index 011aaee0d6..f453d418e7 100644
--- a/apps/erp/vite.config.ts
+++ b/apps/erp/vite.config.ts
@@ -21,6 +21,13 @@ export default defineConfig(({ command, isSsrBuild, mode }) => {
process.env.VERCEL_GIT_COMMIT_SHA?.slice(0, 12) ??
Date.now().toString(36);
+ // root.tsx loads Vercel Web Analytics only in a build Vercel ran: its script
+ // is served nowhere else. Vercel's React Router preset exposes no VITE_ copy
+ // of its system variables, so make the one Vercel's Vite preset would.
+ if (process.env.VERCEL === "1") {
+ process.env.VITE_VERCEL_ENV ??= process.env.VERCEL_ENV;
+ }
+
/**
* Dev-server SSR dependencies that must be inlined rather than left as bare
* `import`s. A production build inlines every dependency (`ssrBuild` below).
diff --git a/apps/mes/app/root.tsx b/apps/mes/app/root.tsx
index 4da73f0b7d..b32fb66c55 100644
--- a/apps/mes/app/root.tsx
+++ b/apps/mes/app/root.tsx
@@ -296,7 +296,9 @@ function Document({
<Toaster position="bottom-left" visibleToasts={5} />
<ScrollRestoration />
<Scripts />
- {!CONTROLLED_ENVIRONMENT && import.meta.env.PROD && <Analytics />}
+ {!CONTROLLED_ENVIRONMENT &&
+ import.meta.env.PROD &&
+ import.meta.env.VITE_VERCEL_ENV && <Analytics />}
</body>
</html>
);
diff --git a/apps/mes/vite.config.ts b/apps/mes/vite.config.ts
index e005da9a3b..2e89158d24 100644
--- a/apps/mes/vite.config.ts
+++ b/apps/mes/vite.config.ts
@@ -11,6 +11,13 @@ import { defineConfig } from "vite";
export default defineConfig(({ command, mode, isSsrBuild }) => {
applyDotenvToProcessEnv(mode, import.meta.dirname);
+ // root.tsx loads Vercel Web Analytics only in a build Vercel ran: its script
+ // is served nowhere else. Vercel's React Router preset exposes no VITE_ copy
+ // of its system variables, so make the one Vercel's Vite preset would.
+ if (process.env.VERCEL === "1") {
+ process.env.VITE_VERCEL_ENV ??= process.env.VERCEL_ENV;
+ }
+
/**
* Dev-server SSR dependencies that must be inlined rather than left as bare
* `import`s. A production build inlines every dependency (see the ERP's