Skip to content

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

🐛 Use next-runtime-env to read runtime env values (#2006)

Luke Vella2090ba7fcf9e
diff --git a/apps/web/package.json b/apps/web/package.json
index aa03535fb..267699126 100644
--- a/apps/web/package.json
+++ b/apps/web/package.json
@@ -84,6 +84,7 @@
"nanoid": "^5.0.9",
"next": "^15.4.6",
"next-auth": "^5.0.0-beta.25",
+ "next-runtime-env": "^3.3.0",
"next-safe-action": "^8.0.7",
"php-serialize": "^4.1.1",
"postcss": "^8.4.31",
diff --git a/apps/web/src/app/[locale]/layout.tsx b/apps/web/src/app/[locale]/layout.tsx
index ec9993b63..a385ed69c 100644
--- a/apps/web/src/app/[locale]/layout.tsx
+++ b/apps/web/src/app/[locale]/layout.tsx
@@ -6,6 +6,7 @@ import { TooltipProvider } from "@rallly/ui/tooltip";
import { domAnimation, LazyMotion } from "motion/react";
import type { Metadata, Viewport } from "next";
import { Inter } from "next/font/google";
+import { PublicEnvScript } from "next-runtime-env";
import type React from "react";
import { TimeZoneChangeDetector } from "@/app/[locale]/timezone-change-detector";
@@ -72,6 +73,9 @@ export default async function Root({
return (
<html lang={locale} className={inter.className}>
+ <head>
+ <PublicEnvScript />
+ </head>
<body>
<FeatureFlagsProvider value={featureFlagConfig}>
<Toaster />
diff --git a/apps/web/src/env.ts b/apps/web/src/env.ts
index cf0f882b8..ec17b633c 100644
--- a/apps/web/src/env.ts
+++ b/apps/web/src/env.ts
@@ -1,6 +1,9 @@
import { createEnv } from "@t3-oss/env-nextjs";
+import { env as runtimeEnv } from "next-runtime-env";
import { z } from "zod";
+const vercelUrl = process.env.NEXT_PUBLIC_VERCEL_URL;
+
export const env = createEnv({
/*
* Serverside Environment variables, not available on the client.
@@ -106,7 +109,7 @@ export const env = createEnv({
* You'll get type errors if these are not prefixed with NEXT_PUBLIC_.
*/
client: {
- NEXT_PUBLIC_BASE_URL: z.url().optional(),
+ NEXT_PUBLIC_BASE_URL: z.url(),
NEXT_PUBLIC_POSTHOG_API_KEY: z.string().optional(),
NEXT_PUBLIC_POSTHOG_API_HOST: z.url().optional(),
NEXT_PUBLIC_SELF_HOSTED: z.enum(["true", "false"]).optional(),
@@ -149,10 +152,12 @@ export const env = createEnv({
S3_ACCESS_KEY_ID: process.env.S3_ACCESS_KEY_ID,
S3_SECRET_ACCESS_KEY: process.env.S3_SECRET_ACCESS_KEY,
S3_REGION: process.env.S3_REGION,
- NEXT_PUBLIC_BASE_URL: process.env.NEXT_PUBLIC_BASE_URL,
- NEXT_PUBLIC_POSTHOG_API_KEY: process.env.NEXT_PUBLIC_POSTHOG_API_KEY,
- NEXT_PUBLIC_POSTHOG_API_HOST: process.env.NEXT_PUBLIC_POSTHOG_API_HOST,
- NEXT_PUBLIC_SELF_HOSTED: process.env.NEXT_PUBLIC_SELF_HOSTED,
+ NEXT_PUBLIC_BASE_URL:
+ runtimeEnv("NEXT_PUBLIC_BASE_URL") ??
+ (vercelUrl ? `https://${vercelUrl}` : undefined),
+ NEXT_PUBLIC_POSTHOG_API_KEY: runtimeEnv("NEXT_PUBLIC_POSTHOG_API_KEY"),
+ NEXT_PUBLIC_POSTHOG_API_HOST: runtimeEnv("NEXT_PUBLIC_POSTHOG_API_HOST"),
+ NEXT_PUBLIC_SELF_HOSTED: runtimeEnv("NEXT_PUBLIC_SELF_HOSTED"),
SUPPORT_EMAIL: process.env.SUPPORT_EMAIL,
NOREPLY_EMAIL: process.env.NOREPLY_EMAIL,
NOREPLY_EMAIL_NAME: process.env.NOREPLY_EMAIL_NAME,
diff --git a/apps/web/src/lib/auth-client.ts b/apps/web/src/lib/auth-client.ts
index ae5194750..1daf236cd 100644
--- a/apps/web/src/lib/auth-client.ts
+++ b/apps/web/src/lib/auth-client.ts
@@ -1,4 +1,3 @@
-import { absoluteUrl } from "@rallly/utils/absolute-url";
import {
anonymousClient,
emailOTPClient,
@@ -8,10 +7,11 @@ import {
} from "better-auth/client/plugins";
import { createAuthClient } from "better-auth/react";
import { signOut as nextAuthSignOut } from "next-auth/react";
+import { env } from "@/env";
import type { Auth } from "@/lib/auth";
export const authClient = createAuthClient({
- baseURL: absoluteUrl("/api/better-auth"),
+ baseURL: `${env.NEXT_PUBLIC_BASE_URL}/api/better-auth`,
plugins: [
inferAdditionalFields<Auth>(),
emailOTPClient(),
diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml
index 6adf5f075..848a52971 100644
--- a/pnpm-lock.yaml
+++ b/pnpm-lock.yaml
@@ -80,7 +80,7 @@ importers:
version: 8.1.0([email protected])
'@vercel/analytics':
specifier: ^1.5.0
dayjs:
specifier: ^1.11.13
version: 1.11.13
@@ -119,7 +119,7 @@ importers:
next-seo:
specifier: ^6.1.0
react:
specifier: ^19.1.0
version: 19.1.0
@@ -367,6 +367,9 @@ importers:
next-auth:
specifier: ^5.0.0-beta.25
+ next-runtime-env:
+ specifier: ^3.3.0
next-safe-action:
specifier: ^8.0.7
@@ -7726,6 +7729,12 @@ packages:
peerDependencies:
react: '>=16'
+ resolution: {integrity: sha512-JgKVnog9mNbjbjH9csVpMnz2tB2cT5sLF+7O47i6Ze/s/GoiKdV7dHhJHk1gwXpo6h5qPj5PTzryldtSjvrHuQ==}
+ peerDependencies:
+ next: ^14
+ react: ^18
+
resolution: {integrity: sha512-clQRxsvf4nZktCYkP1seeNH5rOZjsx6VYSAlpphvXvW+INIBvGySEStKutwtAPFV0/uZFJmNPt77ankoepUYXQ==}
engines: {node: '>=18.17'}
@@ -14698,7 +14707,7 @@ snapshots:
dependencies:
crypto-js: 4.2.0
optionalDependencies:
react: 19.1.0
@@ -18075,13 +18084,18 @@ snapshots:
- acorn
- supports-color
+ dependencies:
+ react: 19.1.0
+
dependencies:
react: 19.1.0
react-dom: 19.1.0([email protected])
dependencies:
react: 19.1.0