Rallly is an open-source scheduling and collaboration tool designed to make organizing events and meetings easier.
TypeScript i18nextnext-authnextjspostgresqlprismareact-emailt3-stacktailwindcsstrpcturborepotypescriptzod
🐛 Fix base URL on Vercel preview deployments (#2645)
Luke Vella2c9f507051d0
diff --git a/apps/web/src/env.ts b/apps/web/src/env.tsindex 61114d6bc..2b4322d36 100644--- a/apps/web/src/env.ts+++ b/apps/web/src/env.ts@@ -6,7 +6,10 @@ import "server-only";import { createEnv } from "@t3-oss/env-nextjs";import * as z from "zod";-const vercelUrl = process.env.NEXT_PUBLIC_VERCEL_URL;+// Base URL fallback on preview deployments, where `NEXT_PUBLIC_BASE_URL` is+// unset. Prefer the stable branch alias (the URL Vercel links to) over the+// per-deployment URL. See `getVercelUrl` in `@rallly/utils/absolute-url`.+const vercelUrl = process.env.VERCEL_BRANCH_URL || process.env.VERCEL_URL;export const env = createEnv({/*diff --git a/packages/utils/src/absolute-url.test.ts b/packages/utils/src/absolute-url.test.tsindex 95e7ee440..67e3b66ce 100644--- a/packages/utils/src/absolute-url.test.ts+++ b/packages/utils/src/absolute-url.test.ts@@ -32,9 +32,13 @@ describe("absoluteUrl", () => {);});- describe("when NEXT_PUBLIC_VERCEL_URL is set", () => {+ describe("when VERCEL_URL is set", () => {beforeAll(() => {- process.env.NEXT_PUBLIC_VERCEL_URL = "example.vercel.com";+ process.env.VERCEL_URL = "example.vercel.com";+ });++ afterAll(() => {+ delete process.env.VERCEL_URL;});it("should return the correct absolute URL with a subpath and query params", () => {@@ -42,6 +46,22 @@ describe("absoluteUrl", () => {"https://example.vercel.com/test?test=test",);});++ describe("and VERCEL_BRANCH_URL is set", () => {+ beforeAll(() => {+ process.env.VERCEL_BRANCH_URL = "branch.vercel.com";+ });++ afterAll(() => {+ delete process.env.VERCEL_BRANCH_URL;+ });++ it("should prefer VERCEL_BRANCH_URL", () => {+ expect(absoluteUrl("/test", { test: "test" })).toBe(+ "https://branch.vercel.com/test?test=test",+ );+ });+ });});});});diff --git a/packages/utils/src/absolute-url.ts b/packages/utils/src/absolute-url.tsindex abb65686d..17707adb5 100644--- a/packages/utils/src/absolute-url.ts+++ b/packages/utils/src/absolute-url.ts@@ -1,9 +1,14 @@const port = process.env.PORT || 3000;const getVercelUrl = () => {- return process.env.NEXT_PUBLIC_VERCEL_URL- ? `https://${process.env.NEXT_PUBLIC_VERCEL_URL}`- : null;+ // Prefer `VERCEL_BRANCH_URL` — the stable per-branch alias Vercel links to from+ // PR comments and the dashboard. That is the URL users actually open, so the+ // base URL must match it or auth cookies/callbacks break cross-origin. Fall+ // back to the per-deployment `VERCEL_URL`. Both are server-only (not+ // `NEXT_PUBLIC_`), so this resolves only on the server; on the client we fall+ // back to `window.location.origin` below.+ const vercelUrl = process.env.VERCEL_BRANCH_URL || process.env.VERCEL_URL;+ return vercelUrl ? `https://${vercelUrl}` : null;};function joinPath(baseUrl: string, subpath = "") {diff --git a/turbo.json b/turbo.jsonindex 7aecb5eec..3d4c71c66 100644--- a/turbo.json+++ b/turbo.json@@ -116,6 +116,9 @@"SMTP_USER","STRIPE_SECRET_KEY","STRIPE_SIGNING_SECRET",- "SUPPORT_EMAIL"+ "SUPPORT_EMAIL",+ "VERCEL_BRANCH_URL",+ "VERCEL_ENV",+ "VERCEL_URL"]}