Skip to content

Scheduling infrastructure for absolutely everyone.

fix: add ensureProtocol helper to handle URLs without protocol (#26667)

simiondolha25f7381393b5
diff --git a/packages/lib/constants.ts b/packages/lib/constants.ts
index aa4ae30d95a..20bf534c810 100644
--- a/packages/lib/constants.ts
+++ b/packages/lib/constants.ts
@@ -1,3 +1,14 @@
+/**
+ * Ensures URL has a protocol prefix. If the URL doesn't start with http:// or https://,
+ * prepends https:// to make it valid for URL parsing.
+ * This handles cases where environment variables have their protocol stripped
+ */
+function ensureProtocol(url: string | undefined): string {
+ if (!url) return "";
+ if (url.startsWith("http://") || url.startsWith("https://")) return url;
+ return `https://${url}`;
+}
+
const VERCEL_URL = process.env.NEXT_PUBLIC_VERCEL_URL ? `https://${process.env.NEXT_PUBLIC_VERCEL_URL}` : "";
const RAILWAY_STATIC_URL = process.env.RAILWAY_STATIC_URL ? `https://${process.env.RAILWAY_STATIC_URL}` : "";
const HEROKU_URL = process.env.HEROKU_APP_NAME ? `https://${process.env.HEROKU_APP_NAME}.herokuapp.com` : "";
@@ -10,7 +21,7 @@ const IS_DEV = CALCOM_ENV === "development";
export const SINGLE_ORG_SLUG = process.env.NEXT_PUBLIC_SINGLE_ORG_SLUG;
/** https://app.cal.com */
export const WEBAPP_URL =
- process.env.NEXT_PUBLIC_WEBAPP_URL ||
+ ensureProtocol(process.env.NEXT_PUBLIC_WEBAPP_URL) ||
VERCEL_URL ||
RAILWAY_STATIC_URL ||
HEROKU_URL ||
@@ -23,7 +34,7 @@ export const WEBAPP_URL_FOR_OAUTH = IS_PRODUCTION || IS_DEV ? WEBAPP_URL : "http
/** @deprecated use `WEBAPP_URL` */
export const BASE_URL = WEBAPP_URL;
-export const WEBSITE_URL = process.env.NEXT_PUBLIC_WEBSITE_URL || "https://cal.com";
+export const WEBSITE_URL = ensureProtocol(process.env.NEXT_PUBLIC_WEBSITE_URL) || "https://cal.com";
export const APP_NAME = process.env.NEXT_PUBLIC_APP_NAME || "Cal.com";
export const SUPPORT_MAIL_ADDRESS = process.env.NEXT_PUBLIC_SUPPORT_MAIL_ADDRESS || "[email protected]";
export const COMPANY_NAME = process.env.NEXT_PUBLIC_COMPANY_NAME || "Cal.com, Inc.";
@@ -38,7 +49,7 @@ export const EMAIL_FROM_NAME = process.env.EMAIL_FROM_NAME || APP_NAME;
// Else use the website url if defined and finally fallback to the webapp url
export const CAL_URL = new URL(WEBAPP_URL).hostname.endsWith(".vercel.app")
? WEBAPP_URL
- : process.env.NEXT_PUBLIC_WEBSITE_URL || WEBAPP_URL;
+ : ensureProtocol(process.env.NEXT_PUBLIC_WEBSITE_URL) || WEBAPP_URL;
export const IS_CALCOM =
WEBAPP_URL &&