Skip to content

Rallly is an open-source scheduling and collaboration tool designed to make organizing events and meetings easier.

πŸ› Keep env validation out of the client bundle (fixes #2474) (#2476)

Luke Vella9891f54b8fab
diff --git a/apps/web/src/env.ts b/apps/web/src/env.ts
index f97b0eb81..0c40e5020 100644
--- a/apps/web/src/env.ts
+++ b/apps/web/src/env.ts
@@ -1,3 +1,8 @@
+// Keep this module out of client bundles: its runtime validation requires
+// `NEXT_PUBLIC_BASE_URL`, which self-hosted deployments set at runtime and is
+// therefore absent on the client. A client import here crashes those builds
+// (see issue #2474). Client code must read `process.env.NEXT_PUBLIC_*` directly.
+import "server-only";
import { createEnv } from "@t3-oss/env-nextjs";
import * as z from "zod";
diff --git a/apps/web/src/lib/locale/client.tsx b/apps/web/src/lib/locale/client.tsx
index 6d8783ce2..0d5aa0476 100644
--- a/apps/web/src/lib/locale/client.tsx
+++ b/apps/web/src/lib/locale/client.tsx
@@ -3,7 +3,6 @@ import { toast } from "@rallly/ui/sonner";
import Cookies from "js-cookie";
import { useParams, useRouter } from "next/navigation";
import React from "react";
-import { env } from "@/env";
import { useTranslation } from "@/i18n/client";
import { LOCALE_COOKIE_NAME } from "@/lib/locale/constants";
@@ -38,7 +37,7 @@ export function LocaleSync({ userLocale }: { userLocale?: string }) {
export function setLocaleCookie(locale: string) {
Cookies.set(LOCALE_COOKIE_NAME, locale, {
path: "/",
- domain: env.NEXT_PUBLIC_COOKIE_DOMAIN,
+ domain: process.env.NEXT_PUBLIC_COOKIE_DOMAIN,
});
}
diff --git a/apps/web/src/utils/storage.ts b/apps/web/src/utils/storage.ts
index 889b36698..59b73c4b0 100644
--- a/apps/web/src/utils/storage.ts
+++ b/apps/web/src/utils/storage.ts
@@ -1,5 +1,4 @@
import { absoluteUrl } from "@rallly/utils/absolute-url";
-import { env } from "@/env";
/**
* Resolves a storage key to an absolute URL.
@@ -7,6 +6,12 @@ import { env } from "@/env";
* 1. If the key is already an absolute URL (e.g. OAuth avatars) or a data URI β†’ returned as-is.
* 2. If `NEXT_PUBLIC_CDN_BASE_URL` is set β†’ served directly from the CDN/bucket.
* 3. Otherwise β†’ proxied through `/api/storage/`.
+ *
+ * Reads `process.env` directly rather than the validated `env` object so this
+ * stays safe to import from client components (same pattern as absolute-url.ts).
+ * Importing `@/env` here pulls its client-side validation into the browser
+ * bundle, which crashes self-hosted builds where `NEXT_PUBLIC_BASE_URL` is set
+ * at runtime and is therefore absent on the client.
*/
export function resolveStorageUrl(key: string): string {
if (
@@ -19,8 +24,9 @@ export function resolveStorageUrl(key: string): string {
const normalizedKey = key.replace(/^\/+/, "");
- if (env.NEXT_PUBLIC_CDN_BASE_URL) {
- const base = env.NEXT_PUBLIC_CDN_BASE_URL.replace(/\/+$/, "");
+ const cdnBaseUrl = process.env.NEXT_PUBLIC_CDN_BASE_URL;
+ if (cdnBaseUrl) {
+ const base = cdnBaseUrl.replace(/\/+$/, "");
return `${base}/${normalizedKey}`;
}